LWN: Comments on "Glibc feature test macros" https://lwn.net/Articles/590381/ This is a special feed containing comments posted to the individual LWN article titled "Glibc feature test macros". en-us Sat, 13 Sep 2025 16:35:40 +0000 Sat, 13 Sep 2025 16:35:40 +0000 https://www.rssboard.org/rss-specification lwn@lwn.net Glibc feature test macros https://lwn.net/Articles/611377/ https://lwn.net/Articles/611377/ troglobit <div class="FormattedComment"> At first when I read this, I thought <a href="http://xkcd.com/927/">http://xkcd.com/927/</a> ... now I hope<br> I've interpreted it correctly, the old -D_BSD_SOURCE will still work right?<br> </div> Wed, 10 Sep 2014 05:56:47 +0000 _FILE_OFFSET_BITS default https://lwn.net/Articles/611245/ https://lwn.net/Articles/611245/ wahern <div class="FormattedComment"> The compiler will complain loudly about the narrowing.<br> <p> And 64-bit programs have been around since the early 1990s. The first full-blown Linux distribution supporting 64-bit Alpha came out in 1995.<br> <p> Compared to Windows, 64-bit issues on Linux have been few and far between. Part of that is because back then FOSS developers tended to be battle-hardened portability experts who took these issues seriously. And at least with 64-bit, that cautionary attitude still lingers.<br> <p> In today's FOSS world where portability is looked down upon as a needless chore, where there's x86 and ARM and nothing else, and where IBM actually ships a Power chip in little-endian mode because it's apparently too difficult for their Linux customers to not conflate representation and value, I can't imagine we'd be able to make such a transition so smoothly again.<br> <p> </div> Mon, 08 Sep 2014 22:22:28 +0000 Best practice? https://lwn.net/Articles/592882/ https://lwn.net/Articles/592882/ wahern <div class="FormattedComment"> I compile and test all my projects on Linux, NetBSD, FreeBSD, OpenBSD, OS X, and Solaris. And I can say without a doubt that the last thing you should do, if you're writing portable programs, is to fiddle with POSIX or XOPEN feature macros!<br> <p> Why?<br> <p> Because most portable code actually uses one or more non-standard interfaces, and good luck making those routines visible again if you did something like -D_POSIX_C_SOURCE=200809L. FTM systems are wildly different on various platforms.<br> <p> Also, many systems don't support the latest POSIX standard. For example, Solaris only supports POSIX up to 2001, even though it actually implements many of the new interfaces. So if you define _POSIX_C_SOURCE (to any value), you paradoxically _lose_ a ton of "POSIX" interfaces.<br> <p> Or look at calls like mkstemp. It's in POSIX 2008, but previously it was only an X/Open extension. On Solaris there's no easy way to get it; if you define _POSIX_C_SOURCE, it disappears. If you define _XOPEN_SOURCE, you get it, but you lose the POSIX API. If you try to define both _POSIX_C_SOURCE and _XOPEN_SOURCE, the system headers barf.<br> <p> Nope. In my experience, you define _GNU_SOURCE on Linux, and on every other system, leave well enough alone. Linux is the only widely used system that has a strict environment by default, AFAIK. Every other system exposes all their interfaces (excepting conflicting ones) by default, which means the last thing you want to do is define _POSIX_C_SOURCE or _XOPEN_SOURCE, unless you're a glutton for punishment, or are on some strict-POSIX crusade. Sometimes you want to define macros like _REENTRANT, _THREAD_SAFE, or the 64-bit FS macros, but those are orthogonal to the larger standards macros, and do not destroy visibility to other interfaces.<br> <p> There's no substitute for testing your code on various systems. Virtual machines makes this easy-peasy. You're life is easier if you avoid FTMs. Then you can use convenient, de-facto standard APIs. It's 2014. Most of the interfaces you want are supported everywhere, but far fewer systems support the standard they were most recently specified in.<br> <p> C standards are a slightly different issue, but fortunately you can have a compiler on a single platform warn you about certain constructs. Also, there's always the option of just using GCC everywhere. I try to support GCC, clang, and Solaris Studio (which require no special effort to get ahold of other than installing the OS), but you have to draw the line somewhere, because while being "portable" doesn't necessarily mean being less productive, it can make you less productive if you do it the wrong way.<br> <p> Portability is a process, not a product. Most projects can be made way more portable than they are with little effort, especially if early in the development process you test-test-test, and don't code yourself into a corner.<br> <p> </div> Thu, 03 Apr 2014 05:54:06 +0000 Best practice? https://lwn.net/Articles/592652/ https://lwn.net/Articles/592652/ bernat <div class="FormattedComment"> What's the best practice when we want to write portable code?<br> <p> Currently, each time I want to use, for example, asprintf, I have a define _GNU_SOURCE at the top of the file. Should I use -D_GNU_SOURCE=1 instead accross my whole project? Or should I stay away _GNU_SOURCE and try to use a more conservative _POSIX_C_SOURCE intead (and wrap asprintf in a dedicated file).<br> </div> Tue, 01 Apr 2014 16:05:38 +0000 Glibc feature test macros https://lwn.net/Articles/591697/ https://lwn.net/Articles/591697/ nix <div class="FormattedComment"> They're both made available (via transparent unions).<br> <p> The only place where you have to suck things up is the BSD-specific getpgrp(), but that hasn't actually worked as in BSD for ages, even if you built with -D_BSD_SOURCE. So we can safely say that this is basically unused.<br> </div> Mon, 24 Mar 2014 18:04:10 +0000 _FILE_OFFSET_BITS default https://lwn.net/Articles/590858/ https://lwn.net/Articles/590858/ roblucid <div class="FormattedComment"> Indeed, I remember when I got my hands on the first 1GB IBM SCSI disk, which seemed unbelievably large, given ones just 3 years older at 300MB were in boxes the size of midi-tower PCs. Files, reaching sizes of 2GiB, seemed to be just in perpetuity broken.. we knew the UNIX FS imposed limits. In fact who wants to open a 2GiB text logfile with vi(1)? Even today with current RAM sizes, back then 32MiB was very large and text files were "small" and loaded into memory. BSD introduced nvi(1) which handled large files better.<br> </div> Mon, 17 Mar 2014 12:12:25 +0000 Glibc feature test macros https://lwn.net/Articles/590838/ https://lwn.net/Articles/590838/ kleptog <div class="FormattedComment"> The only time I've really had to fiddle with the feature test macros was when dealing with the structure definitions in netinet/ip.h, netinet/tcp.h and netinet/udp.h. If BSD_SOURCE is enabled the fields are called th_sport, uh_sport, etc. Otherwise the names are different. This usually happens when you've downloaded some source from the internet.<br> <p> Given they're talking about eliminating the BSD/SysV macros, does that mean GLibc will pick one of the two and if you're using the other you'll just have to suck it up?<br> </div> Sun, 16 Mar 2014 13:45:08 +0000 _FILE_OFFSET_BITS default https://lwn.net/Articles/590819/ https://lwn.net/Articles/590819/ giraffedata <p>I was not as imaginative as khim; I was just thinking of data structures that have 4 bytes for the file offset. <p> Like struct stat. If you used struct stat in a binary interface, and then recompiled with _FILE_OFFSET_BITS=64, your program would be using a struct stat64 while your communication partner would still be using stat and the file size field, not to mention the whole structure, would overrun. <p> If you just had your own data structure - maybe a database index - with a 4 byte offset field and assigned an off_t value to it, you'd end up with an arithmetic overrun, which is almost as bad as a buffer overrun. (And that's even before considering the signedness issue khim raised). <p> I'm sure there are people saying it was never right to assume the width of off_t or struct stat, and therefore essentially nobody did it, but that's hindsight. There was a time when the concept of a file larger than 2 GB was too ridiculous, and the nature of such a system too unpredictable, to justify mucking up your code with an overflow test. One could easily have reasoned that if files ever did get that big, the designers of the new interface could deal with the 32 bit assumption then (which they did). Sat, 15 Mar 2014 23:00:29 +0000 _FILE_OFFSET_BITS default https://lwn.net/Articles/590818/ https://lwn.net/Articles/590818/ kleptog <div class="FormattedComment"> Nice example, thanks for that. I wouldn't write it that way, but I can imagine that kind of code being written.<br> <p> In this example the reading in of 2G of data will probably fail but I can see now that it's not the quick win I think it was.<br> </div> Sat, 15 Mar 2014 22:41:41 +0000 _FILE_OFFSET_BITS default https://lwn.net/Articles/590817/ https://lwn.net/Articles/590817/ khim <blockquote><font class="QuotedText">Perhaps I'm being insufficiently imaginative, but I can't think of a way that compiling an old program with _FILE_OFFSET_BITS=64 could lead to a buffer overrun. Do you have an example?</font></blockquote> <p>Is this a joke? Something like this will overflow just fine with maliciously crafted file of size 2G+2*maxreqsize:<br /> &#160;&#160;<code>int mail_size = lseek(fd, 0, SEEK_END);</code><br /> &#160;&#160;<code>if (mail_size &gt; MAX_MAIL_SIZE) {</code><br /> &#160;&#160;&#160;&#160;<code>syslog(LOG_MAIL, "Mail is too big!\n");</code><br /> &#160;&#160;&#160;&#160;<code>return;</code><br /> &#160;&#160;<code>}</code><br /> &#160;&#160;<code>lseek(fd, 0, SEEK_SET);</code><br /> &#160;&#160;<code>read(fd, mail_bug, mail_size);</code><br /> &#160;&#160;<code>…</code><br /> </p> <p>Is it sloppy programming? Yeah, sure. But even if so it's safe with <code>_FILE_OFFSET_BITS=32</code> and unusafe with <code>_FILE_OFFSET_BITS=64</code>.</p> <blockquote><font class="QuotedText">In my experience programs rarely do anything with fseek/ftell.</font></blockquote> <p>Indeed. Most of the code is probably out there will probably work fine with <code>_FILE_OFFSET_BITS=64</code>. Unfortunately you only need <b>one</b> piece of code which does something sloppy with <code>long</code>s<code>/int</code>s/<code>size_t</code>s to have an exploit.</p> <p><code>fseek/fteel</code> use <code>long</code> with or without <code>_FILE_OFFSET_BITS=64</code> but without <code>_FILE_OFFSET_BITS=64</code> the actual size always fit in <code>int</code> while as with <code>_FILE_OFFSET_BITS=64</code> it's easy to get values which are negative when stored in <code>int</code> but positive when stored in <code>size_t</code>. Hilarity ensues.</p> Sat, 15 Mar 2014 21:58:34 +0000 _FILE_OFFSET_BITS default https://lwn.net/Articles/590815/ https://lwn.net/Articles/590815/ kleptog <div class="FormattedComment"> Perhaps I'm being insufficiently imaginative, but I can't think of a way that compiling an old program with _FILE_OFFSET_BITS=64 could lead to a buffer overrun. Do you have an example?<br> <p> In my experience programs rarely do anything with fseek/ftell. So not compiling with _FILE_OFFSET_BITS=64 just means these programs fail reading/writing large files without there actually being an issue. Especially if a file is opened append-only, it breaks when reaching 2G, even though the program clearly doesn't care.<br> </div> Sat, 15 Mar 2014 18:46:47 +0000 Glibc feature test macros https://lwn.net/Articles/590789/ https://lwn.net/Articles/590789/ jwakely <div class="FormattedComment"> Many of them already do, so you can e.g. include &lt;stdio.h&gt; and only get the definition of FILE, then include it again and get everything else. Glibc is weird :)<br> </div> Fri, 14 Mar 2014 23:09:52 +0000 _FILE_OFFSET_BITS default https://lwn.net/Articles/590783/ https://lwn.net/Articles/590783/ giraffedata <blockquote> _FILE_OFFSET_BITS=64, in particular, should always be set, or else it's easy to introduce latent bugs, </blockquote> <p> And you think that's a bigger risk than old programs, written in a day when file offsets greater than 32 bits were not even on the horizon, will have buffer overruns when you compile them for use on a new system? Fri, 14 Mar 2014 22:32:05 +0000 Glibc feature test macros https://lwn.net/Articles/590772/ https://lwn.net/Articles/590772/ mathstuf <div class="FormattedComment"> Wouldn't headers which use features.h then need to support being included multiple times so that extra functions could be picked up with the new feature macros?<br> </div> Fri, 14 Mar 2014 19:09:48 +0000 G++ defines _GNU_SOURCE https://lwn.net/Articles/590713/ https://lwn.net/Articles/590713/ jwakely <div class="FormattedComment"> <font class="QuotedText">&gt; _GNU_SOURCE being missing, on the other hand, just leads to confused posts on programming forums questioning about missing functions.</font><br> <p> Those people need to Read The Fantastic Article above :)<br> <p> <font class="QuotedText">&gt; But I have yet to find a situation where it being set wasn't what the programmer wanted.</font><br> <p> <a href="http://gcc.gnu.org/PR60491">http://gcc.gnu.org/PR60491</a><br> <a href="http://gcc.gnu.org/PR57582">http://gcc.gnu.org/PR57582</a><br> <a href="http://gcc.gnu.org/PR59945">http://gcc.gnu.org/PR59945</a><br> <a href="http://gcc.gnu.org/PR11196">http://gcc.gnu.org/PR11196</a><br> <p> These are all valid bug reports, the C++ standard library should not be polluting the global namespace and preventing users from declaring things called "clone" and "minor" which are not part of any C, C++ or POSIX standard. If you want those names there is a FTM you can use to get them, but 'g++ -std=c++11 -pedantic' should not force them on you.<br> </div> Fri, 14 Mar 2014 15:21:47 +0000 G++ defines _GNU_SOURCE https://lwn.net/Articles/590709/ https://lwn.net/Articles/590709/ cesarb <div class="FormattedComment"> <font class="QuotedText">&gt; Once that's done C++ developers on GNU/Linux will probably find they've been unintentionally relying on _GNU_SOURCE for years and will have to learn how to use FTMs properly to get access to various POSIX APIs.</font><br> <p> Both _GNU_SOURCE and _FILE_OFFSET_BITS=64 should be the default, since they are what most programmers expect.<br> <p> _FILE_OFFSET_BITS=64, in particular, should always be set, or else it's easy to introduce latent bugs, which will show up once you have a file with more than 2G. I have seen that happen: the previous sysadmin(s) had a hand-compiled squid proxy, which one day stopped working, because the log file had grown to 2G (yeah, the log rotation was missing too). The immediate solution was to hand-rotate the log files; the real solution was to use a squid compiled correctly (and with working log rotation).<br> <p> _GNU_SOURCE being missing, on the other hand, just leads to confused posts on programming forums questioning about missing functions. But I have yet to find a situation where it being set wasn't what the programmer wanted.<br> </div> Fri, 14 Mar 2014 15:03:19 +0000 G++ defines _GNU_SOURCE https://lwn.net/Articles/590568/ https://lwn.net/Articles/590568/ jwakely <div class="FormattedComment"> <font class="QuotedText">&gt; I wonder if modules (C++1y) could help dealing with this problem?</font><br> <p> Maybe. I'd like to think the answer is yes, but I bet I can solve the problem the old fashioned way before there's a modules proposal, let alone an implementation ;-)<br> <p> <font class="QuotedText">&gt; Is it true that with modules libstdc++ could be compiled with _GNU_SOURCE enabled, but dependencies of libstdc++ won't be exposed (don't pollute global namespace) to user, unless they affect signature of functions or layout of classes?</font><br> <p> You're asking about the properties of a non-existent feature, so it's hard to give an accurate answer, but yes, I hope that will be true.<br> </div> Thu, 13 Mar 2014 16:56:52 +0000 Glibc feature test macros https://lwn.net/Articles/590559/ https://lwn.net/Articles/590559/ ejr <div class="FormattedComment"> If anyone wants to see true feature test macro insanity, check out AIX... I truly appreciate the glibc FTM approach and this latest work, thank you!<br> </div> Thu, 13 Mar 2014 15:43:04 +0000 G++ defines _GNU_SOURCE https://lwn.net/Articles/590543/ https://lwn.net/Articles/590543/ sorokin <div class="FormattedComment"> I wonder if modules (C++1y) could help dealing with this problem?<br> <p> Is it true that with modules libstdc++ could be compiled with _GNU_SOURCE enabled, but dependencies of libstdc++ won't be exposed (don't pollute global namespace) to user, unless they affect signature of functions or layout of classes?<br> </div> Thu, 13 Mar 2014 14:15:42 +0000 Glibc feature test macros https://lwn.net/Articles/590511/ https://lwn.net/Articles/590511/ mchapman <div class="FormattedComment"> <font class="QuotedText">&gt; Notwithstanding occasional bugs in the documented FTM requirements, the problem is always the same: FTMs must be defined before including any header files. The reason for this requirement is that various header files may implicitly include other header files, and any of those files may in turn include &lt;features.h&gt;. If a macro is defined after &lt;features.h&gt; is included, then it has no effect, since that header file (like most library header files) includes a guard macro that ensures that the contents of the file are processed only on the first inclusion of the file. </font><br> <p> I wonder if it would be possible for &lt;features.h&gt; to detect that it was being re-included with a different set of FTMs enabled -- implying that some macros were defined (or redefined) after the first inclusion of the header. It could emit an appropriate #warning in this case.<br> <p> Unfortunately, looking at how &lt;features.h&gt; is implemented I don't think this would be quite straight-forward. You wouldn't just be able to capture and compare a snapshot of the macros' values, as some of them are actually used to set the values of others. But it would be a worthwhile change if it cuts down on needless bug reports.<br> </div> Thu, 13 Mar 2014 11:17:38 +0000 G++ defines _GNU_SOURCE https://lwn.net/Articles/590504/ https://lwn.net/Articles/590504/ jwakely <div class="FormattedComment"> To confuse things a bit, G++ unconditionally defines _GNU_SOURCE on systems using glibc, because the C++ standard library wants to be able to make use of GNU extensions, so uses the "give me everything" approach.<br> <p> This unfortunately means that standard C++ headers such as &lt;algorithm&gt; pollute the global namespace with dozens of non-standard names (and horrible macros like "minor"!)<br> <p> I'm going to fix this in the next few months by replacing _GNU_SOURCE with more fine-grained use of FTMs, and (I hope) some help from the glibc maintainers to redeclare the handful of GNU extensions libstdc++ actually needs using alternative names.<br> <p> Once that's done C++ developers on GNU/Linux will probably find they've been unintentionally relying on _GNU_SOURCE for years and will have to learn how to use FTMs properly to get access to various POSIX APIs.<br> </div> Thu, 13 Mar 2014 10:53:29 +0000 Glibc feature test macros https://lwn.net/Articles/590502/ https://lwn.net/Articles/590502/ ncm <div class="FormattedComment"> That cleanup must have been mind-numbing work. Thanks, Joe.<br> </div> Thu, 13 Mar 2014 10:28:41 +0000