LWN: Comments on "GCC 12.1 Released" https://lwn.net/Articles/894149/ This is a special feed containing comments posted to the individual LWN article titled "GCC 12.1 Released". en-us Thu, 25 Sep 2025 10:36:14 +0000 Thu, 25 Sep 2025 10:36:14 +0000 https://www.rssboard.org/rss-specification lwn@lwn.net GCC 12.1 Released https://lwn.net/Articles/895696/ https://lwn.net/Articles/895696/ anton <blockquote> (And the compiler will happily transform assumedly-aligned loads into LDRD/LDM.) </blockquote> I was somewhat surprised how hard it was to find ldrds in the binary in order to exercise them: only 32 non-sp/fp ldrds and 58 ldms in 19587 instructions. For comparison, an Aarch64 binary of (a later version of) the same program has 257 non-sp/fp ldps in 21745 instructions. By general-purpose I mean the, e.g. Zen3 core that's targeted by free software developers and/or ISVs, not, e.g., AMDs PSPs which are indeed Aarch32 cores last I heard, but which we unfortunately cannot program. <blockquote> There are plenty of libraries originally designed for Linux userspace that are quite usable and useful on higher-end microcontrollers, and it would be a shame if the only thing preventing them from working in that environment was an accidental reliance on misaligned data. </blockquote> Indeed, ideally already the GPL prevents them from being used in such locked-down environments. But if gcc maintainers' willingness to break programs hurts the proprietary crowd for a change, that's less of a concern to me than when they hurt free software developers and users. <blockquote> It would also be a shame if GCC wasted performance on those microcontrollers by assuming all data might be misaligned and never using LDRD/LDM, given the vast majority of existing code does follow the alignment rules correctly and is currently benefiting from that optimisation. </blockquote> On the contrary, I would find it a shame if programmers who know how to get good performance by using unaligned accesses would <a href="http://al.howardknight.net/?ID=165290702500">slow down</a> their programs in order to cater for gcc's sillyness. Wed, 18 May 2022 21:07:25 +0000 GCC 12.1 Released https://lwn.net/Articles/895653/ https://lwn.net/Articles/895653/ excors <div class="FormattedComment"> <font class="QuotedText">&gt; In general-purpose computers, the transition to ARMv8-A has happened quite a while ago (e.g., with Raspi3 in 2016).</font><br> <font class="QuotedText">&gt; </font><br> <font class="QuotedText">&gt; However, maybe it has more to do with the instruction set. In that case, Aarch32 seems to be pretty alive on RaspiOS (although even they have started releasing an Aarch64 version).</font><br> <p> True, my previous comment should have said &quot;ARMv8-A AArch64&quot; (not &quot;ARMv8-A&quot;) - the rules for ARMv8-A AArch32 look essentially identical to ARMv7-A, so unaligned LDRD/LDM/etc will fault as you showed in a later comment. (And the compiler will happily transform assumedly-aligned loads into LDRD/LDM.)<br> <p> <font class="QuotedText">&gt; ARMv8-M is irrelevant for general-purpose computers.</font><br> <p> Also true (well, assuming you mean the main user-visible processor and ignore the potentially dozens of microcontrollers in the same computer), but I&#x27;m not sure &quot;general-purpose computer&quot; is that useful a distinction in practice. There are plenty of libraries originally designed for Linux userspace that are quite usable and useful on higher-end microcontrollers, and it would be a shame if the only thing preventing them from working in that environment was an accidental reliance on misaligned data. It would also be a shame if GCC wasted performance on those microcontrollers by assuming all data might be misaligned and never using LDRD/LDM, given the vast majority of existing code does follow the alignment rules correctly and is currently benefiting from that optimisation. So I believe there&#x27;s still value in following those alignment rules in new code, for portability to real systems that may realistically want to reuse your code.<br> </div> Wed, 18 May 2022 14:23:28 +0000 GCC 12.1 Released https://lwn.net/Articles/895571/ https://lwn.net/Articles/895571/ anton I have now managed to indeed get a SIGBUS on a Raspi4 by running 32-bit code that uses ldrd with an unaligned address (but regular ldr does not produce such an exception). So SSE/SSE2, ldrd (and friends) are the last die-hards in a general-purpose world dominated by instructions that work with unaligned addresses. Tue, 17 May 2022 20:39:57 +0000 GCC 12.1 Released https://lwn.net/Articles/895429/ https://lwn.net/Articles/895429/ jpfrancois <div class="FormattedComment"> But the check the return value is much more robuste in à lot of case :<br> If you change the format strings it still works.<br> You do not need to implement your security check across all call site.<br> What if you have à slightly more complex format string ? You have to implement correctly the size calculation everywhere ?<br> <p> <p> </div> Mon, 16 May 2022 19:50:43 +0000 GCC 12.1 Released https://lwn.net/Articles/895312/ https://lwn.net/Articles/895312/ wtarreau <div class="FormattedComment"> That&#x27;s typically what I&#x27;m doing with asm or arch-specific optimizations in general: try to make sure the code works on generic platforms (since it helps detect bugs) and only make efforts on relevant ones, typically x86 and armv8 in my case.<br> <p> </div> Sun, 15 May 2022 16:53:12 +0000 GCC 12.1 Released https://lwn.net/Articles/895306/ https://lwn.net/Articles/895306/ anton <blockquote> It does get a lot easier if you exclude ARMv7, though that transition is either pretty recent or hasn't happened yet, depending on what field you're working in. </blockquote> In general-purpose computers, the transition to ARMv8-A has happened quite a while ago (e.g., with Raspi3 in 2016). <p>However, maybe it has more to do with the instruction set. In that case, Aarch32 seems to be pretty alive on RaspiOS (although even they have started releasing an Aarch64 version). However the Cortex-X2 and Cortex-A510 announced by ARM almost a year ago don't support Aarch32, so Aarch32 is a second-class citizen already, and I expect that there will be no hardware support on general-purpose computers for it in the not-too-distant future. <p>Personal experience: I just tried to run an EABI5 binary on all four ARMv8-A machines (with various distributions) we have around. On three I get "no such file or directory" (apparently the kernel does not understand the binary at all), the fourth (a Raspi4 with 64-bit Debian 10) eventually chokes on a missing library. It seems that Aarch32 is not very important for 64-bit Linux distributions. <p>Concerning the SCTLR_ELx.A bit, IA-32 and AMD64 have a similar bit since the 486, which I tried to use (for portability checking in a development environment), but had to give up on, because on IA-32 the ABI puts doubles at 4-byte boundaries, and the flag would cause fault on such accesses. Another attempt with AMD64 failed because gcc produces unaligned accesses from pairs of user-written aligned accesses. So if Linux has not set SCTLR_ELx.A in the past, setting it now would probably cause quite a bit of breakage. <p>Concerning atomics, they are no excuse for breaking code that does not perform atomic accesses (I doubt that the auto-vectorizer dares auto-vectorizing atomics). <p>ARMv8-M is irrelevant for general-purpose computers. To those who think it has anything to do with ARMv8-A: it has not. E.g., there is no Aarch64 (the headline feature of ARMv8-A) in ARMv8-M. Yes ARM's naming is confusing. Sun, 15 May 2022 12:48:42 +0000 GCC 12.1 Released https://lwn.net/Articles/895305/ https://lwn.net/Articles/895305/ excors <div class="FormattedComment"> <font class="QuotedText">&gt; The surviving general-purpose architectures are AMD64, Aarch64, RV64GC, Power, s390.</font><br> <p> It does get a lot easier if you exclude ARMv7, though that transition is either pretty recent or hasn&#x27;t happened yet, depending on what field you&#x27;re working in.<br> <p> If I&#x27;m reading it right, ARMv8-A says: Unaligned accesses to Device memory (i.e. MMIO) always fault. Most loads/stores to unaligned Normal memory are okay, but multi-register loads/stores will fault if the SCTLR_ELx.A bit is set (though I believe Linux doesn&#x27;t set that), and Exclusive/Acquire/Release/Atomic accesses will fault unless your CPU is ARMv8.4 (or older with an optional feature) (but even when unaligned atomics are supported, they may (unpredictably) fault if they cross a 16-byte boundary).<br> <p> ARMv7-A will fault in much less obscure cases, e.g. any unaligned multi-word access (LDM, LDRD, etc) regardless of SCTLR.A. That&#x27;s a problem whenever you&#x27;re loading an int64_t, or even two adjacent int32_ts (because the compiler likes to merge them into one instruction), and if it&#x27;s not aligned you&#x27;ll need to tell the compiler with __attribute__((packed)).<br> <p> ARMv8-M also faults on unaligned multi-word accesses. An ARMv8-M Baseline implementation (which I think is the modern replacement for ARMv6-M) will even fault on unaligned single-word accesses.<br> </div> Sun, 15 May 2022 11:23:54 +0000 GCC 12.1 Released https://lwn.net/Articles/895303/ https://lwn.net/Articles/895303/ anton The problem happens with code that, <a href="https://gcc.gnu.org/bugzilla/show_bug.cgi?id=65709">e.g.</a> copies a block of memory with 64-byte accesses from one unaligned address to another unaligned address. The auto-vectorized code uses movdqa for one of the addresses, and extra code is generated to align this address to a 16-byte boundary assuming that the original address is 8-byte aligned. However, the original address is not 8-byte-aligned, and the movdqa then traps. <p>gcc <a href="http://www.complang.tuwien.ac.at/anton/autovectors/">could have used movdqu instead and achieved the same performance</a> for this loop even in the 8-byte-aligned case (plus the intended non-trap in the unaligned case). Sun, 15 May 2022 09:57:22 +0000 GCC 12.1 Released https://lwn.net/Articles/895302/ https://lwn.net/Articles/895302/ anton <blockquote> Of course, such code was already broken on most(?) non-x86 targets because the x86 is the weirdo. </blockquote> Of course, this is one of the claims commonly made by those who advocate that compilers break programs with undefined behaviour. <p>First of all, if a program works on some machine, and the compiler breaks it on that machine, the fact that earlier it may not have worked on some other machine does not help the user and is pure whataboutism. <p>Next, is it actually true? The surviving general-purpose architectures are AMD64, Aarch64, RV64GC, Power, s390. I just tried it on an Aarch64 (Odroid N2) and RV64GC (Starfive Visionfive) machine, and they performed the unaligned access without complaint. Power has supported unaligned accesses in big-endian mode for a long time, and AFAIK they also support it in their new little-endian mode (and the old little-endian mode has not been used in general-purpose computers). Even on the Alphas from the last century, unaligned accesses were supported in Linux by default, albeit very slowly (and with a report in dmesg), and I had to <a href="http://www.complang.tuwien.ac.at/anton/uace.c">take special measures</a> to trap unaligned accesses. <p>So, these days an architecture that traps on unaligned accesses is weirdo. In particular, SSE is weirdo (Intel did not repeat this misdesign with AVX, and AMD (but unfortunately not Intel) even supports a fix for SSE), but even SSE includes instructions that tolerate unaligned accesses, so the gcc maintainers could choose to use those to avoid the breakage. <p>Concerning the claim (not made here) that using the trap-on-unaligned-access instructions are faster, such claims usually come without any empirical support. I microbenchmarked that (with a microbenchmark based on code in a bug report where Jakub Jelinek had justified gcc's use of these instructions with this claim), and found that the claim is not true for this microbenchmark. Sun, 15 May 2022 09:23:44 +0000 GCC 12.1 Released https://lwn.net/Articles/894858/ https://lwn.net/Articles/894858/ NYKevin <div class="FormattedComment"> The C standard doesn&#x27;t really give compilers a whole lot to go on here. For certain issues, it directs the compiler to &quot;emit a diagnostic,&quot; and for a superset of those issues, it says &quot;the program is ill-formed,&quot; but that&#x27;s it.<br> <p> * Warnings vs. errors? Unspecified. The compiler is entirely within its rights to produce a binary even if the program is ill-formed and a diagnostic is required, so long as the compiler at least prints some sort of message diagnosing the issue.<br> * -Wall vs. -Wextra vs. -Wsome-random-thing? Nope. Most compilers emit all standardized warnings with no flags, so the additional warnings you can enable are all nonstandard, and it&#x27;s purely an issue of implementation quality how they work, which flags toggle which warnings, and so on.<br> * Formatting of messages? No. The standard simply directs the compiler to &quot;emit a diagnostic,&quot; and compiler writers are responsible for figuring out what that means and how to implement it. This is arguably a good thing, because it makes it possible to display warnings graphically or in an IDE (rather than e.g. requiring the use of stderr and then having the IDE parse the output from a separate process, which might be a good design but should not be mandatory), but it also means that different compilers can print totally different messages for the same problem.<br> <p> About the best you can do is pick a set of warnings that you think is appropriate for your codebase (e.g. start with -Wall and add/subtract warnings as necessary), fix all of those warnings, and then aggressively WONTFIX any bugs that people file about warnings that are not on the list (unless it looks like the warning may have identified a real bug, in which case you might want to consider adding it to your list). If people don&#x27;t like that, they can fork it.<br> </div> Wed, 11 May 2022 21:42:12 +0000 GCC 12.1 Released https://lwn.net/Articles/894848/ https://lwn.net/Articles/894848/ wtarreau <div class="FormattedComment"> Or an old Solaris one that returned 0 or -1 when the operation failed (I don&#x27;t remember, sorry), or the one in dietlibc that used to do something similar, etc. Even here the snprintf() doc doesn&#x27;t match what we do on most modern systems:<br> <p> <a href="https://pubs.opengroup.org/onlinepubs/7908799/xsh/snprintf.html">https://pubs.opengroup.org/onlinepubs/7908799/xsh/snprint...</a><br> <p> RETURN VALUE<br> Upon successful completion, these functions return the number of bytes<br> transmitted excluding the terminating null in the case of sprintf() or snprintf()<br> or a negative value if an output error was encountered. <br> <p> On Linux+glibc:<br> The functions snprintf() and vsnprintf() do not write more than size<br> bytes (including the terminating null byte (&#x27;\0&#x27;)). If the output was<br> truncated due to this limit, then the return value is the number of<br> characters (excluding the terminating null byte) which would have been<br> written to the final string if enough space had been available.<br> <p> That&#x27;s what most modern systems do, allowing you to realloc() the area and try<br> again. Some do not support being passed size zero, others do.<br> <p> snprintf() is one of the most important and least portable functions when it comes<br> to good security practices. There&#x27;s also %z (size_t) that&#x27;s not much portable, and<br> &quot;%.*s&quot; that often does fun things like shifting all args by one since %.* is not<br> understood as consuming an extra argument, so usually you segfault by trying to<br> print the string from a pointer that&#x27;s in fact its max length.<br> <p> </div> Wed, 11 May 2022 20:44:54 +0000 GCC 12.1 Released https://lwn.net/Articles/894676/ https://lwn.net/Articles/894676/ geert <div class="FormattedComment"> Like the snprintf() you had to roll yourself, because VxWorks didn&#x27;t provide one?<br> </div> Wed, 11 May 2022 08:11:21 +0000 GCC 12.1 Released https://lwn.net/Articles/894556/ https://lwn.net/Articles/894556/ dvdeug <div class="FormattedComment"> <font class="QuotedText">&gt; There are sufficiently bogus snprintf() implementations in the wild,</font><br> <p> So working implementations can&#x27;t be improved because you have to be backwardly compatible with systems that can&#x27;t properly implement functions released with BSD4.4 and standardized in C99. Where are these snprintf implementations? Especially &quot;in the wild&quot;? It&#x27;s not sounding like something that most GCC users or developers would care about.<br> </div> Tue, 10 May 2022 20:39:23 +0000 GCC 12.1 Released https://lwn.net/Articles/894588/ https://lwn.net/Articles/894588/ wtarreau <div class="FormattedComment"> Exact. Plus &quot;enabling specific warnings&quot; would only work if there was a portable way to enable (or silence) warnings across all compilers without having to perform a discovery pass first. Enabling a fixed set everywhere is trivial when you use *your* compiler for *your* project. When you distribute your code and it builds on a wide range of compilers that&#x27;s a totally different story.<br> </div> Tue, 10 May 2022 18:43:28 +0000 GCC 12.1 Released https://lwn.net/Articles/894577/ https://lwn.net/Articles/894577/ mpr22 <div class="FormattedComment"> GCC&#x27;s -Wall command-line option does not turn on all warnings, hasn&#x27;t done for years, and quite possibly has never done so in the quarter-century I&#x27;ve been using GCC.<br> </div> Tue, 10 May 2022 17:22:34 +0000 GCC 12.1 Released https://lwn.net/Articles/894552/ https://lwn.net/Articles/894552/ dvdeug <div class="FormattedComment"> If you want all warnings, use Wall. If you want a specific set of warnings, turn just those warnings on. Turning arbitrary warnings on and turning warnings into errors turns the language into an ever-evolving, compiler-specific language, which is your choice, but something terribly silly to complain about.<br> </div> Tue, 10 May 2022 15:51:24 +0000 GCC 12.1 Released https://lwn.net/Articles/894446/ https://lwn.net/Articles/894446/ kilobyte <div class="FormattedComment"> Valgrind points out alignment violations. So run it -- it&#x27;s a yet another reason to do so.<br> </div> Tue, 10 May 2022 07:49:17 +0000 GCC 12.1 Released https://lwn.net/Articles/894426/ https://lwn.net/Articles/894426/ wtarreau <div class="FormattedComment"> That&#x27;s exactly the way I see it. Nowadays people using C need it for low-level stuff because someone has to do it, and the places where C is needed want to have a good trust on the code translation to machine code because where it&#x27;s used, it matters. Usually it&#x27;s a mix of relying on hardware (e.g. hope the compiler will produce a ROL when using both a left and right shifts), the OS (e.g. cause a segfault when writing at address zero), and the libc (e.g; memcpy() does what the standard says it does).<br> <p> That&#x27;s why for me it&#x27;s important that a C compiler tries less to guess about improbable mistakes that are relevant to absolutely zero use cases for this language, but instead focuses on real mistakes that are easy to solve (e.g; operators precedence, asking for braces, undefined use of self-increment in arguments, etc).<br> <p> I&#x27;m fine with having such unlikely analysis but only on developer&#x27;s request (e.g. -Wsuspicious). It could then go further and report some uncommon constructs that are inefficient and are suspicious because of that without annoying users when -Wall is used to detect likely incompatibilities on their platforms (because that&#x27;s why most of us use -Wall -Werror, it&#x27;s to catch problems at build time on other platforms).<br> <p> </div> Tue, 10 May 2022 03:43:02 +0000 GCC 12.1 Released https://lwn.net/Articles/894231/ https://lwn.net/Articles/894231/ foom <div class="FormattedComment"> Yeah, the usual case that trips folks up is when they lie to the compiler and claim larger alignment but then don&#x27;t actually provide it. (e.g. using __attribute__((aligned(16))) but then use a custom allocator which only provides 8byte aligned memory.)<br> <p> The compiler believes what you tell it, and will emit instructions or do optimizations that depend upon that claimed alignment. But it won&#x27;t _always_ choose instructions that will trap, so depending on optimizations you can get away with the incorrectly specified alignment until a future compiler upgrade causes a different instruction choice.<br> </div> Mon, 09 May 2022 23:27:33 +0000 GCC 12.1 Released https://lwn.net/Articles/894387/ https://lwn.net/Articles/894387/ NYKevin <div class="FormattedComment"> It&#x27;s not wrong, merely inadequate. Quoth James Mickens:<br> <p> <font class="QuotedText">&gt; You might ask, “Why would someone write code in a grotesque</font><br> <font class="QuotedText">&gt; language that exposes raw memory addresses? Why not use</font><br> <font class="QuotedText">&gt; a modern language with garbage collection and functional</font><br> <font class="QuotedText">&gt; programming and free massages after lunch?” Here’s the</font><br> <font class="QuotedText">&gt; answer: Pointers are real. They’re what the hardware understands.</font><br> <font class="QuotedText">&gt; Somebody has to deal with them. You can’t just place</font><br> <font class="QuotedText">&gt; a LISP book on top of an x86 chip and hope that the hardware</font><br> <font class="QuotedText">&gt; learns about lambda calculus by osmosis.</font><br> <p> <a href="https://www.usenix.org/system/files/1311_05-08_mickens.pdf">https://www.usenix.org/system/files/1311_05-08_mickens.pdf</a><br> <p> (I&#x27;m sure that Mr. Mickens is/was aware that LISP machines existed, once upon a time, but they&#x27;re hardly relevant to the modern era.)<br> </div> Mon, 09 May 2022 20:17:59 +0000 GCC 12.1 Released https://lwn.net/Articles/894384/ https://lwn.net/Articles/894384/ mpr22 <div class="FormattedComment"> This whole discussion does a very good job of convincing me that the real problem in this particular scenario is C&#x27;s string model being profoundly Worng.<br> </div> Mon, 09 May 2022 19:35:38 +0000 GCC 12.1 Released https://lwn.net/Articles/894382/ https://lwn.net/Articles/894382/ wtarreau <div class="FormattedComment"> <font class="QuotedText">&gt; On GCC 10 you get the same warning even without any -O flag. So this did somewhat improve in later GCC versions.</font><br> <p> In a sense, that&#x27;s a way to see it... But 4.7 never got it wrong at all and used to provide meaningful warnings if you go in that direction :-) Plus it was 3 times faster.<br> </div> Mon, 09 May 2022 19:25:46 +0000 GCC 12.1 Released https://lwn.net/Articles/894381/ https://lwn.net/Articles/894381/ wtarreau <div class="FormattedComment"> <font class="QuotedText">&gt; You could remove the length check and do &quot;if (snprintf(...) &gt;= sizeof(fullpath)) return -1;&quot;, because -Wformat-truncation=1 only warns if it heuristically estimates that truncation is likely *and* the return value is unused. That would make the code simpler and more robust, since it no longer relies on you manually replicating snprintf&#x27;s length calculation, and would eliminate the warning.</font><br> <p> Sorry, but no. There are sufficiently bogus snprintf() implementations in the wild, I&#x27;m not going to remove a security check in my code just to silence a bogus warning in gcc. Instead I added the condition to snprintf() in addition to the existing one, making the code even uglier, and I even managed to fail it once by forgetting to add &quot;&gt; sizeof()&quot; at the end. Fortunately it broke in the right direction and stopped working. A similar bug in the other direction can cause an introduction of a vulnerability, as quite often when playing dirty length tricks to shut up a compiler.<br> <p> <font class="QuotedText">&gt; I suspect the compiler is converting the check into &quot;strlen(dir) + strlen(file) &gt; 4096/2-2&quot;, and both values are unsigned so it can deduce strlen(dir) &lt;= 2046 and strlen(file) &lt;= 2046, but it forgets the relationship between them because it doesn&#x27;t support multi-variable constraints on string lengths - it just has an integer upper/lower bound for each string independently </font><br> <p> That was exactly my feeling as well, which proves that the warning is totally bogus and should be reverted. But they never revert warnings, they just add tons more until the code becomes unreadable in ifdefs and convoluted tests that become totally insecure.<br> <p> <font class="QuotedText">&gt; &gt; This enables all the warnings about constructions that some users consider questionable, and that are easy to avoid (or modify to prevent the warning), even in conjunction with macros.</font><br> <font class="QuotedText">&gt; which is also behaving as advertised, because C string functions are always questionable, and it&#x27;s easy to avoid the warning by checking snprintf&#x27;s return value.</font><br> <p> I get your point but here we&#x27;re reaching the point that many of us have been seriously questioning for a while: &quot;how long before we have to definitely remove -Wall projects built with gcc&quot;. That&#x27;s sad because it used to catch many programmers&#x27; bugs in the past and has become useless and unusable over time. Reminds me of the 90s when compilers could almost compile /etc/passwd without sweating...<br> <p> </div> Mon, 09 May 2022 19:24:29 +0000 GCC 12.1 Released https://lwn.net/Articles/894316/ https://lwn.net/Articles/894316/ tzafrir <div class="FormattedComment"> On GCC 10 you get the same warning even without any -O flag. So this did somewhat improve in later GCC versions.<br> </div> Mon, 09 May 2022 12:22:42 +0000 GCC 12.1 Released https://lwn.net/Articles/894315/ https://lwn.net/Articles/894315/ pbonzini <div class="FormattedComment"> You may be confusing with stack alignment. The x86-64 ABI promises 16-byte stack alignment, and if some function failed to preserve that everything broke because GCC used aligned-access instructions on the stack.<br> </div> Mon, 09 May 2022 12:03:43 +0000 GCC 12.1 Released https://lwn.net/Articles/894314/ https://lwn.net/Articles/894314/ excors <div class="FormattedComment"> That&#x27;s true for lots of compiler warnings. The optimisation passes provide a lot of information about control flow and data flow, especially when they remove function call boundaries by inlining, which helps determine whether code is probably buggy (and should be warned about) or probably safe (no warning). Without that information, the compiler can&#x27;t be confident either way and will usually err on the side of not warning (because programmers get really annoyed by false positives, especially if there&#x27;s no easy way to make the compiler shut up). So it will usually find and report more bugs when you turn on optimisation.<br> <p> In this case, if the variables are declared as char* then the compiler has no idea of their probable length and doesn&#x27;t warn. It&#x27;s only because they&#x27;re declared as char[MAXPATHLEN] that it becomes reasonably confident in its guess that the string might actually be MAXPATHLEN-1 in length, which is enough confidence to emit the (incorrect) warning. More sophisticated optimisation passes let it make a better guess of the string&#x27;s length, reducing the false positives.<br> </div> Mon, 09 May 2022 11:49:45 +0000 GCC 12.1 Released https://lwn.net/Articles/894313/ https://lwn.net/Articles/894313/ pizza <div class="FormattedComment"> <font class="QuotedText">&gt; Wait wait what, the warnings change depending on optimization level? Am I the only one for whom this is surprising news?</font><br> <p> This would appear to be an obvious conclusion from different optimization levels producing different sets of warnings.<br> <p> I don&#x27;t know when I first became aware of this, but it&#x27;s been at least a decade.<br> </div> Mon, 09 May 2022 11:43:54 +0000 GCC 12.1 Released https://lwn.net/Articles/894312/ https://lwn.net/Articles/894312/ anselm <blockquote><em>Wait wait what, the warnings change depending on optimization level?</em></blockquote> <p> That's not new. I seem to remember from back when I was programming in C more that some GCC warnings about unreachable code or uninitialised variables were only output under optimisation, because otherwise the analysis on which these warnings were based would not have been performed. </p> Mon, 09 May 2022 11:40:43 +0000 GCC 12.1 Released https://lwn.net/Articles/894309/ https://lwn.net/Articles/894309/ atnot <div class="FormattedComment"> <font class="QuotedText">&gt; It appears this only fixes the warning at -O2, not -O1</font><br> <p> Wait wait what, the warnings change depending on optimization level? Am I the only one for whom this is surprising news?<br> </div> Mon, 09 May 2022 10:44:53 +0000 GCC 12.1 Released https://lwn.net/Articles/894308/ https://lwn.net/Articles/894308/ excors <div class="FormattedComment"> <font class="QuotedText">&gt; So I have two options, either I conclude that I can remove all my now useless length checks (since gcc12 doesn&#x27;t trust them, so possibly it optimised them away, not checked) or I&#x27;ll simply disable that warning that became stupid.</font><br> <p> You could remove the length check and do &quot;if (snprintf(...) &gt;= sizeof(fullpath)) return -1;&quot;, because -Wformat-truncation=1 only warns if it heuristically estimates that truncation is likely *and* the return value is unused. That would make the code simpler and more robust, since it no longer relies on you manually replicating snprintf&#x27;s length calculation, and would eliminate the warning.<br> <p> <font class="QuotedText">&gt; if I lower the limit on the sump of strlen() in the first check to sizeof/2, it accepts to pass!</font><br> <p> I suspect the compiler is converting the check into &quot;strlen(dir) + strlen(file) &gt; 4096/2-2&quot;, and both values are unsigned so it can deduce strlen(dir) &lt;= 2046 and strlen(file) &lt;= 2046, but it forgets the relationship between them because it doesn&#x27;t support multi-variable constraints on string lengths - it just has an integer upper/lower bound for each string independently (I think?). Then it knows the snprintf won&#x27;t need more than 4094 bytes and can&#x27;t overflow. In the original code, all it can deduce is strlen(dir) &lt;= 4096 etc, which isn&#x27;t sufficient to prove it won&#x27;t overflow.<br> <p> It appears this only fixes the warning at -O2, not -O1, seemingly because -O1 doesn&#x27;t deduce string length constraints from strlen comparisons and it just uses the declared length instead.<br> <p> The GCC documentation says:<br> <p> <font class="QuotedText">&gt; When the exact number of bytes written by a format directive cannot be determined at compile-time it is estimated based on heuristics that depend on the level argument and on optimization. While enabling optimization will in most cases improve the accuracy of the warning, it may also result in false positives.</font><br> <p> so it&#x27;s behaving as advertised (i.e. not stable or precise). And -Wall says:<br> <p> <font class="QuotedText">&gt; This enables all the warnings about constructions that some users consider questionable, and that are easy to avoid (or modify to prevent the warning), even in conjunction with macros.</font><br> <p> which is also behaving as advertised, because C string functions are always questionable, and it&#x27;s easy to avoid the warning by checking snprintf&#x27;s return value.<br> </div> Mon, 09 May 2022 10:08:08 +0000 GCC 12.1 Released https://lwn.net/Articles/894298/ https://lwn.net/Articles/894298/ wtarreau ... and that started already with a new awesome warning, it didn't take long! Note, this one is implified, it instead complains at plenty of places where controls were already in place.<p></p> <pre> $ cat thankyougcc12.c #include &lt;sys/param.h&gt; #include &lt;stdio.h&gt; #include &lt;string.h&gt; char dir[MAXPATHLEN]; char file[MAXPATHLEN]; char fullpath[MAXPATHLEN]; /* returns -1 in case of error */ int makefullpath() { if ((strlen(dir) + 1 + strlen(file) + 1) &gt; sizeof(fullpath)) return -1; snprintf(fullpath, sizeof(fullpath), "%s/%s", dir, file); return 0; } $ x86_64-linux-gcc -O2 -Wall-c thankyougcc12.c thankyougcc12.c: In function 'makefullpath': thankyougcc12.c:15:50: warning: '%s' directive output may be truncated writing up to 4094 bytes into a region of size between 1 and 4095 [-Wformat-truncation=] 15 | snprintf(fullpath, sizeof(fullpath), "%s/%s", dir, file); | ^~ ~~~~ thankyougcc12.c:15:9: note: 'snprintf' output between 2 and 8190 bytes into a destination of size 4096 15 | snprintf(fullpath, sizeof(fullpath), "%s/%s", dir, file); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ </pre> Sure... I just performed the length check <b>before</b> calling <tt>snprintf()</tt> and it believes I'm trying to stuff the sum of these in this string. So I have two options, either I conclude that I can remove all my now useless length checks (since gcc12 doesn't trust them, so possibly it optimised them away, not checked) or I'll simply disable that warning that became stupid.<p></p> And it's really the control fro the previous check that is wrong, because if I lower the limit on the sump of <tt>strlen()</tt> in the first check to <tt>sizeof/2</tt>, it accepts to pass! So it looks like they've implemented a string length test for snprintf() that didn't consider that two strings could be concatenated by a single call (yes we can do that!). It would be nice if they only enabled warnings after they tested that they actually work on real code.<p></p> It's sad that each and every new version forces you to disable useful warnings that once used to be valid and became useless over time, it <b>does</b> render the code less secure by letting stupid bugs slip through. Because of this, in the long term I'll probably end up writing my own function and stop calling it snprintf() directly so that it stops being smart. Too bad if I introduce new bugs in this action.<p></p> What would be needed would be a diagnostic mode where you ask for suggestions or "are you sure" only as a developer, but not stuff like this that prove the compiler didn't understand the code but will cause build breakage at users', and it completely discourages programmers from putting error checks in their code since regardless of what was done, the compiler complains anyway. <p></p> Ah, GNU Complainers Collection, I really love you :-( Mon, 09 May 2022 07:45:46 +0000 GCC 12.1 Released https://lwn.net/Articles/894287/ https://lwn.net/Articles/894287/ hvd <div class="FormattedComment"> There is no requirement on compilers to support arbitrary libc implementations. libc and the compiler work together, either may depend on internals of the other to make the combined product conform to the relevant standards. For instance, glibc relies on the compiler to define __STRICT_ANSI__ when invoked in standards-conforming mode. The C standard says nothing about this macro beyond that it&#x27;s in the namespace that&#x27;s reserved for any use by the implementation and compilers are not required to define this macro, but that is not an issue, glibc is for use with compilers that do define it. If some other compiler, say, pcc, doesn&#x27;t define it, fine, that just means pcc+glibc is not standards-conforming, but that&#x27;s not a bug in either pcc or glibc, that&#x27;s a problem for whoever decided to combine those two. It works the other way around as well. The compiler relies on memcpy to not set errno. The C standard does not guarantee this and implementations are allowed to set it, but GCC is for use with libc implementations that don&#x27;t set it. If some hypothetical elibc does make memcpy set errno, that just means the combination of GCC+elibc is non-conforming, but that&#x27;s not a bug in either GCC or elibc, that&#x27;s a problem for whoever decided to combine those two.<br> </div> Mon, 09 May 2022 00:47:23 +0000 GCC 12.1 Released https://lwn.net/Articles/894273/ https://lwn.net/Articles/894273/ NYKevin <div class="FormattedComment"> I remember reading in the comments of an otherwise-unrelated GCC bug[1] that ISO C says memcpy() is permitted to clobber errno, and that therefore gcc is technically required to either prove that errno is not used by the application code, or to emit extra instructions to save and restore the variable. The reaction from the gcc developers to this revelation could be paraphrased as &quot;haha, no.&quot; Which isn&#x27;t a terribly surprising response when you consider just how liberally gcc emits memcpy calls, but I found it amusing. So yes, they will deviate from the standard in cases where the standard is ridiculous or otherwise problematic.<br> <p> [1]: <a href="https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56888">https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56888</a><br> </div> Sun, 08 May 2022 23:45:55 +0000 GCC 12.1 Released https://lwn.net/Articles/894272/ https://lwn.net/Articles/894272/ NYKevin <div class="FormattedComment"> This is a valid position for application code to take, but library code IMHO generally should not be in the business of dictating architecture support unless it is doing something hardware-specific (e.g. if your library provides fast lock-free data structures, it&#x27;s fair enough to say &quot;the hardware must support certain atomic primitives,&quot; if your library does float math, it&#x27;s fair enough to say &quot;the hardware must conform to IEEE 754,&quot; and so on). Thing is, there&#x27;s a lot of library code out there[citation needed], and it&#x27;s hard to say with absolute certainty which libraries are getting used on more esoteric hardware configurations.<br> </div> Sun, 08 May 2022 23:38:19 +0000 GCC 12.1 Released https://lwn.net/Articles/894270/ https://lwn.net/Articles/894270/ Paf <div class="FormattedComment"> It’s also not unrealistic to write code only aimed at those platforms…. I’m involved in a decent size project and we target those two plus a variant of PowerPC and that last is for weird semi-historical reasons.<br> <p> For a specific software project, it’s not crazy to only aim at ARM and x86, or even just x86 or ARM depending on what you’re up to.<br> <p> How many non-embedded systems aren’t one of those two? Is it even 0.1% any more? I’m sure it’s not 1%.<br> </div> Sun, 08 May 2022 22:47:29 +0000 GCC 12.1 Released https://lwn.net/Articles/894268/ https://lwn.net/Articles/894268/ ballombe <div class="FormattedComment"> The opposite happened to me. The bug was not fixed because my code &quot;interpreted the standard too rigidly...&quot;<br> </div> Sun, 08 May 2022 22:06:24 +0000 GCC 12.1 Released https://lwn.net/Articles/894267/ https://lwn.net/Articles/894267/ wtarreau <div class="FormattedComment"> It&#x27;s even worse (or better), ARM is also excellent with unaligned accesses nowadays, so you if you don&#x27;t run your code on a wide variety of platforms, you can have broken code that runs fine on the two most popular platforms without ever noticing.<br> <p> </div> Sun, 08 May 2022 21:14:03 +0000 GCC 12.1 Released https://lwn.net/Articles/894232/ https://lwn.net/Articles/894232/ NYKevin <p>Of course, such code was already broken on most(?) non-x86 targets because <a href="https://devblogs.microsoft.com/oldnewthing/20040914-00/?p=37873">the x86 is the weirdo</a>. But I imagine quite a few developers are of the "unless it breaks on my laptop, I don't care" mentality... Sun, 08 May 2022 18:57:29 +0000 GCC 12.1 Released https://lwn.net/Articles/894229/ https://lwn.net/Articles/894229/ hmh <div class="FormattedComment"> GCC used to switch from (non-vector) instructions that tolerate unaligned access to (vector) instructions that forbid it when autovectorizing for x86 targets. Even when there were alternative (slower?) vector instructions that would tolerate unaligned access.<br> <p> This is going to expose bad source code that relied on UB related to unaligned access and was therefore not compatible with autovectorizaton on x86, but did not disable it explicitly and instead depended on -O2 to not enable autovectorizaton.<br> <p> Since it triggers at runtime, I foresee some &quot;explicitly disable vectorization on anything using -O2&quot; CFLAGS patching in the future...<br> </div> Sun, 08 May 2022 18:31:18 +0000 GCC 12.1 Released https://lwn.net/Articles/894219/ https://lwn.net/Articles/894219/ Sesse <div class="FormattedComment"> If a struct only contains small types, its minimum alignment is not going to be 16, and GCC&#x27;s autovectorization will of course not use instructions that expect such an alignment.<br> </div> Sun, 08 May 2022 09:38:58 +0000