Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Misuse of _POSIX_C_SOURCE feature macro #318

Closed
RhodiumToad opened this issue Jul 1, 2023 · 3 comments
Closed

Misuse of _POSIX_C_SOURCE feature macro #318

RhodiumToad opened this issue Jul 1, 2023 · 3 comments

Comments

@RhodiumToad
Copy link

As a general rule, it's not desirable for a library's public header file(s) to try and define feature macros such as _POSIX_C_SOURCE. The place to do that is either in the compiler flags, or in a private header file.

The reason why it's a problem is that it potentially messes with the namespace/feature choices of clients of the library - but only if they happen to #include "maxminddb.h" before including any standard header. If instead they include it after some other header, that's even worse: the result is undefined.

(I've seen #158 and #267 for some context for why you're defining this, but it remains that you're doing it in the wrong place, in a way that can cause build failures for client code - an example of which I've just spent an hour or two assisting someone to fix.)

@horgh
Copy link
Contributor

horgh commented Jul 4, 2023

Thanks! Do you know of a good reference stating practices around this? From what I've found searching around, it does sound like it is not an ideal spot for it though.

It's been there since the first version of this library, so I'm hesitant to remove it now. I suspect it would cause different breakages for existing users.

@RhodiumToad
Copy link
Author

I'm guessing that most existing clients don't see a problem (and wouldn't see any effect from fixing it) because the include of maxminddb.h is done after including some system header. While this is undefined in the spec, in practice what it means is that the definition is simply ignored (the usual implementation is to have a header included first by all system headers that checks all feature macros and defines internal visibility flags based on them, so after this point the feature macros themselves are ignored).

(In fact this is the workaround I suggested to the guy with the original problem.)

I can't find a good "best practices" reference. The spec itself says that the values must be defined before including any header (see: https://pubs.opengroup.org/onlinepubs/9699919799.2018edition/functions/V2_chap02.html#tag_15_02 etc.), which implies defining it either directly in every source file (ugh) or in the compiler options. (Some references discourage using the compiler options on the grounds that the sources should be self-describing.) In practice I see no way that using a private header file (included first in all source files within the project) could fail to work correctly: i.e.

maxminddb_private.h

/* include guards, etc. */
#ifndef _POSIX_C_SOURCE
#define _POSIX_C_SOURCE 200809L
#endif

#include "maxminddb.h"
...

@horgh
Copy link
Contributor

horgh commented Jul 5, 2023

Thank you! Yeah, it sounds like removing it would probably be fine then.

horgh added a commit that referenced this issue Aug 4, 2023
We believe that this should be set by applications rather than by the
library. The spec suggests that having it in this spot is not correct as
it should be set prior to any header being included. See
https://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#tag_15_02.

Fixes #318
horgh added a commit that referenced this issue Aug 4, 2023
We believe that this should be set by applications rather than by the
library. The spec suggests that having it in this spot is not correct as
it should be set prior to any header being included. See
https://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#tag_15_02.

Fixes #318
horgh added a commit that referenced this issue Aug 4, 2023
We believe that this should be set by applications rather than by the
library. The spec suggests that having it in this spot is not correct as
it should be set prior to any header being included. See
https://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#tag_15_02.

Fixes #318
freebsd-git pushed a commit to freebsd/freebsd-ports that referenced this issue Aug 16, 2023
- Bump PORTREVISION for package change

PR:		272349
Reported by:	Mina Galic <freebsd@igalic.co>
Reference:	maxmind/libmaxminddb#318
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants