News for hackers. Stuff that matters.
Re:Useful GCC warning options not enabled by -Wall -Wextra by jcelerier on Wednesday October 04, 2017 @11:34AM (#15401198)
> Why are those warnings not automatically included in "-Wall -Wextra"? Is there any issue with enabling them? Are these somewhat experimental?

Because tons of people keep complaining "muh my build breaks if they enable new warnings". Dammit, idiots, if your build break there's a good chance your software is already broken too; that's the point of breaking the build: making bugs visible so that you have to fix them.

Re:Useful GCC warning options not enabled by -Wall -Wextra by quotemstr on Wednesday October 04, 2017 @11:52AM (#15401386)
That's why you don't enable -Werror in release builds, contrary to received wisdom on the subject.

When you compile with -Werror, you surrender control over your build's success to the whims of the same people who think that it's okay that memcpy(0, 0, 0) is undefined behavior. The personal opinions of future compiler authors shouldn't affect whether your program builds successfully. Sometimes warnings are bogus.

Sure, use -Werror in development. But don't make it the default.

Re:Useful GCC warning options not enabled by -Wall -Wextra by jcelerier on Wednesday October 04, 2017 @12:34PM (#15401713)
> The personal opinions of future compiler authors shouldn't affect whether your program builds successfully.

So, let's say that you find some 2003 software on some deep corner of SourceForge that you want to use.

Would you rather :

* Have the build fail because new warnings were introduced ? * Have the build succeed and then get crashes at runtime because the behaviour of the compiler changed anyways and the program was rendered invalid ?

I'll personally always want the first one.

Re:Useful GCC warning options not enabled by -Wall -Wextra by quotemstr on Wednesday October 04, 2017 @12:51PM (#15401853)
There's a name for a kind of change that makes a previously correct program incorrect: "compiler bug".
Re:Useful GCC warning options not enabled by -Wall -Wextra by jcelerier on Wednesday October 04, 2017 @01:01PM (#15401942)
> There's a name for a kind of change that makes a previously correct program incorrect: "compiler bug".

No. The only thing that matters is that the language rules are respected. Or maybe you'd want programs that were correct with the first C compilers to still work today ? Because for instance they didn't do type checks at all. `void f(float, float); int x = f;` you say ? no problem! `char c; c[-15];` ? no problem! Everything did compile, sure. Is it the world we want ? Most certainly not.

Re:Useful GCC warning options not enabled by -Wall -Wextra by colejohnson66 on Wednesday October 04, 2017 @08:44PM (#15406016)
Trigraphs used to be part of the standard and are now removed. So really old programs that used them now would fail to compile. Granted, that is a bit of a low hanging fruit.
Re:Useful GCC warning options not enabled by -Wall -Wextra by spc476 on Wednesday October 04, 2017 @05:22PM (#15404816)
Also "horribly written code."

I've been playing around with some code from 1993---the Viola web browser (one of the first graphical web browsers). Ooh boy was it fun when I ran it with "-pedantic -Wall -Wextra". Hundreds, if not thousands of warnings. A ton of unused argumetns, implicit declarations (no prototypes), unused variables, return type not specified, a few "control reaches end of non-void function," some format string mismatches. Pretty much what you would expect from K&R C (the code paid some lip service to ANSI C, but not a whole lot).

Also, the code runs (not well, but it does) on 32-bit systems, but immediately crashes on a 64-bit system. That's because of the whole "int==long==ptr==32 bits" that permeates the code.

Re:Useful GCC warning options not enabled by -Wall -Wextra by eru on Wednesday October 04, 2017 @01:52PM (#15402514)
Who says the program was previously correct? Just because it compiled and didn't blow up so far, doesn't mean it's correct. It might or might not have been correct..
Re:Useful GCC warning options not enabled by -Wall -Wextra by mrguyorama on Wednesday October 04, 2017 @01:03PM (#15401972)
the program wasn't "previously correct", else a new, but still language compliant, compiler would build it just fine
Re:Useful GCC warning options not enabled by -Wall -Wextra by YSFEJ4SWJUVU6 on Wednesday October 04, 2017 @01:12PM (#15402047)
Worth noting that some warning flags (at least when combined with -Werror* ) make the compiler not standard compliant.

* It's been some time since I last partook in language lawyering, and can't remember if standard compliant C compilers are allowed to produce diagnostics, the term used, for well-defined code as long as they also succeed.

Re:Useful GCC warning options not enabled by -Wall -Wextra by clhodapp on Wednesday October 04, 2017 @01:25PM (#15402174)
Perhaps what we really want is a flag to cause the compiler to error for code that would produce undefined behavior and give warnings for the rest? Turning all warnings into errors is a great "worse is better" technique but it seems a bit too coarse-grained to me.
Re:Useful GCC warning options not enabled by -Wall -Wextra by eru on Wednesday October 04, 2017 @01:52PM (#15402525)
Alas, undefined behaviour in C++ and C is in general not that easily detected..
Re:Useful GCC warning options not enabled by -Wall -Wextra by clhodapp on Wednesday October 04, 2017 @04:04PM (#15404063)
Oh, to be sure! However, my claim is that if compiler authors are able to add more no-false-positives undefined behavior warnings over time, I kinda want those to "break" the build for my existing software... But if something is just a style check or creates false results, I'd rather the build be allowed to happen.
Re:Useful GCC warning options not enabled by -Wall -Wextra by withzombies on Wednesday October 04, 2017 @02:27PM (#15402976)
It's true, but there's a few tools out there to help now, such as tis-interpreter (https://github.com/TrustInSoft/tis-interpreter)
Re:Useful GCC warning options not enabled by -Wall -Wextra by eru on Thursday October 05, 2017 @12:03PM (#15410243)
Yes, an interpreter has a much better shot at detecting runtime undefined behaviour than a compiler.

The whole point of undefined behaviour in C and C++ is to let the compiler cheat: ie a Java or Haskell compiler would have to take into account that (i < i + 1) can sometimes be wrong for native ints, and would have to prove that overflow can't happen in order to optimize this comparison away to True. Undefined behaviour in the standard frees C and C++ compilers from these obligations, and they can just assume overflow for signed ints won't happen.

These shortcuts (plus a lot of smarts) make it feasible to write a fast optimizing compiler with the 1970s state of the art in static analysis.

Re:Useful GCC warning options not enabled by -Wall -Wextra by mjevans on Wednesday October 04, 2017 @08:55PM (#15406066)
Agreed. If the compiler wants to optimize something because of Undefined Behavior, that's something that -Wall should break on.

'Warning, you have undefined behavior', let the programmer decide what the intent of the section is and fix it.

Re:Useful GCC warning options not enabled by -Wall -Wextra by Sean1708 on Thursday October 05, 2017 @05:39AM (#15407820)
Just FYI, not all undefined behaviour can be detected at compile time using current C semantics. I'm very very in favour of cracking down on undefined behaviour and changing things from undefined to implementation-defined and stuff like that, but it's not as easy as just flicking a switch and making the compiler warn whenever it assumes undefined behaviour won't happen.
Re:Useful GCC warning options not enabled by -Wall -Wextra by mrguyorama on Wednesday October 04, 2017 @01:15PM (#15402083)
Do you have an example I could take a look at? I'm actually not well versed in the C world
Re:Useful GCC warning options not enabled by -Wall -Wextra by microtherion on Wednesday October 04, 2017 @04:55PM (#15404558)
A simple case is the warning clang will generate for:

  int err = 0;
  if (err = f()) {
  }
This is indeed a useful warning, as it's common to mistype == as = in this situation, but it's also absolutely correct code, and the idiom that silences the warning (surrounding with extra parentheses) does not change anything about the correctness of the code.
Re:Useful GCC warning options not enabled by -Wall -Wextra by makapuf on Thursday October 05, 2017 @02:56AM (#15407122)
A hint could be that you're initializing to zero a local just to overwrite it. Not an error or a warning but it could hint that you did not mean to, isn't it ?
Re:Useful GCC warning options not enabled by -Wall -Wextra by microtherion on Thursday October 05, 2017 @04:23PM (#15412313)
The initialization is just an example to show that err is a previously declared variable. The actual code could easily be different, but the warning is emitted on seeing the assignment in the conditional.
Re:Useful GCC warning options not enabled by -Wall -Wextra by YSFEJ4SWJUVU6 on Wednesday October 04, 2017 @01:34PM (#15402281)
Unintended code (like having both arms of an if-else-statement execute the same action, or a duplicate condition in a logical expression) is sometimes perfectly valid as far as the standard goes. A double condition for example may simply be a result of poor refactoring, and thus the program even works as intended – however, the compiler cannot know that. That's why the helpful warnings flag those statements – -Werror then makes the compiler abort the compilation altogether on any warnings which is clearly not standard-compliant in cases like the one I outlined.

The majority of the warning options showed in the article are intended to find unintended code (which can however perfectly defined if not intended behaviour as opposed to invalid, undefined behaviour). The very basic examples showed for those options also themselves do not contain anything that by itself would make the code not standard-compliant.

Re:Useful GCC warning options not enabled by -Wall -Wextra by dkersten on Wednesday October 04, 2017 @04:07PM (#15404094)
Well, the standard does not say that if you ask the compiler to flag potentially suspect code that it should compile successfully. That doesn’t make it non standard if you decide you do want your compiler to stop compiling when it encounters potentially suspect code and enable that mode wth a conpiler flag.
Re:Useful GCC warning options not enabled by -Wall -Wextra by johannes1234321 on Wednesday October 04, 2017 @12:15PM (#15401558)
The opinions of (future) compiler authors decides already wether your programm will work in all cases. With those warnings they indicate some of their decisions.
Re:Useful GCC warning options not enabled by -Wall -Wextra by quotemstr on Wednesday October 04, 2017 @12:51PM (#15401847)
Future compiler authors are at least in theory beholden to current language specifications. If a program does something legal that a compiler author feels is a bad idea, the compiler can whine all it wants, but it still has to compile the program. Compilers drop support for older specifications far less often than they add new warnings.
Re:Useful GCC warning options not enabled by -Wall -Wextra by tomsmeding on Wednesday October 04, 2017 @05:39PM (#15404949)
This is especially true for warnings about && and || precedence. When you use those two operators in one expression without parentheses, IIRC both gcc and clang complain, but I once wanted to compile some software that thought it was a good idea to -Werror all the code and don't use parens around &&. Fun times fixing all the zillion makefiles that mentioned Werror. (Yay autoconf?)
Re:Useful GCC warning options not enabled by -Wall -Wextra by pklausler on Wednesday October 04, 2017 @05:53PM (#15405045)
That's the one warning that I like to disable. I hate having to write superfluous parentheses around conjunctions. I mean, come on. It's just as lame as requiring them around multiplications would be.
Re:Useful GCC warning options not enabled by -Wall -Wextra by mjevans on Wednesday October 04, 2017 @08:51PM (#15406041)
Even if /you/ know the order of operations, making them explicit with the parentheses provides clarity and allows future maintainers to quickly (and correctly) deduce the structure of the logical construct.

I generally only leave them out in short simple to follow 'normal math' order of operations.

Re:Useful GCC warning options not enabled by -Wall -Wextra by quotemstr on Wednesday October 04, 2017 @09:04PM (#15406095)
Sure. The rule you suggest might be good coding practice --- but people can disagree, and their omitting the extra braces doesn't make the code wrong. An existing correct program shouldn't one day stop building because someone added a warning that enforces your preferred operating precedence marking style.
Re:Useful GCC warning options not enabled by -Wall -Wextra by zaphar on Wednesday October 04, 2017 @10:47PM (#15406411)
That is because the compiler authors conflated warnings with a linter. Warnings should be used to indicate basically just two things.

1. Dead/Duplicated Code.

2. Probably Broken Code.

Anything else is stylistic and the realm of opinion. Even if it's informed opinion enforcing it belongs in a linter.

Re:Useful GCC warning options not enabled by -Wall -Wextra by quotemstr on Wednesday October 04, 2017 @11:53PM (#15406583)
To be fair, a compiler contains all the infrastructure you'd want in a linter. I don't mind compilers having lint-like warnings: I just want them to very clear about the differences between these warnings and warnings that reflect functional problems with high probability.
Re:Useful GCC warning options not enabled by -Wall -Wextra by evincarofautumn on Thursday October 05, 2017 @09:18AM (#15408878)
What I’d like is for compiler authors to just admit this and include a lint mode.

There’s some practical and political difficulty with adding new warnings to a widely used compiler, partly because you don’t want to generate too many new warnings for existing code, partly because people compile with -Werror and upgrading the compiler will break (well, sprain) their build. Those difficulties would be easy to skirt by just tossing new warnings into lint mode and promoting them to compiler warnings if they turn out to be very useful in practice.

Re:Useful GCC warning options not enabled by -Wall -Wextra by zaphar on Thursday October 05, 2017 @02:02PM (#15411168)
I agree. A -Wlint would be a great way to keep the distinction but reuse the compilers infrastructure.
Re:Useful GCC warning options not enabled by -Wall -Wextra by bluGill on Wednesday October 04, 2017 @05:44PM (#15404985)
The C standard is dropping support for things like gets, a compliant modern compiler should fail to build any code that uses that. This is very controversial with compilers - and gets is among the most horrid thing that ever made it into a compiler.
Re:Useful GCC warning options not enabled by -Wall -Wextra by quotemstr on Wednesday October 04, 2017 @07:29PM (#15405680)
PulseEvent and IsBadReadPtr are worse. Windows has some of the very best and the very worst in API design.
Re:Useful GCC warning options not enabled by -Wall -Wextra by AgentME on Wednesday October 04, 2017 @04:42PM (#15404436)
In general yes, but the posts above were talking about a warning about undefined behavior, so the standard says that anything could happen in that case.
Re:Useful GCC warning options not enabled by -Wall -Wextra by Joky on Thursday October 05, 2017 @04:26AM (#15407479)
> you surrender control over your build's success to the whims of the same people who think that it's okay that memcpy(0, 0, 0) is undefined behavior.

You mean the people who write the specs of the language? Well you surrender control to these folks when you decided to pick this language!

Otherwise the problem about memcpy of zero-size and nullptr is unfortunate from compiler engineers (at least clang engineers) point of view. The story is that the compiler is optimizing based on programmer annotations: after all if a programmer takes the effort to annotate their APIs, let's benefit from this. However the GNU libc annotated `memcpy` (and others) with this non_null attribute, causing this fragility on invalid (according to the spec) but common code.

Opinion of clang authors is somehow "Deleting a null pointer check on p after a memcpy(p, q, 0) call seems extremely user-hostile, and very unlikely to result in a valuable improvement to the program".

Clang dev thread: http://lists.llvm.org/pipermail/cfe-dev/2017-January/052066....

Note in this thread that clang devs are trying to get the language changed to remove this UB.

Re:Useful GCC warning options not enabled by -Wall -Wextra by jlg23 on Wednesday October 04, 2017 @02:58PM (#15403345)
I disagree. Rather write nasty rants in comments that explain compiler-specific workarounds. It should not be too many anyway, if one adheres to KISS.

Otherwise you surrender control over the correctness of your program "to the whims of the same people who think that it's okay that memcpy(0, 0, 0) is undefined behavior".

More debatable decisions will be made in compiler implementations and I'd rather force the user who compiles the program to understand the implications of warnings by turning them into errors than letting them ignore them (because "it compiled, after all!").

If I was to make the call, I'd make "-Werror" default in all compilers, remove the option and allow to disable it by "--I-really-want-to-ignore-warnings-for-fscks-sake" which when used would sleep(30) after an all-caps nag screen that tells people "don't ever use that option."

Re:Useful GCC warning options not enabled by -Wall -Wextra by spc476 on Wednesday October 04, 2017 @05:06PM (#15404657)
I have one project where I run with "-pedantic -std=c99 -Wall -Wextra". The one error that pops up is:

     warning: ISO C forbids assignment between function pointer and ‘void *’ [-pedantic]
But POSIX requires that behavior (the actual target of the build). So for now, it's benign, but if the platform changes (to a non-POSIX system) then yes, it will have to be investigated.
Re:Useful GCC warning options not enabled by -Wall -Wextra by nwellnhof on Thursday October 05, 2017 @07:43AM (#15408334)
This affects many codebases. For example, looking up functions with `dlsym` triggers this warning which is probably one of the main reasons for the POSIX requirement. It's unfortunate that GCC doesn't allow to disable only this particular warning. When writing cross-platform code, `-pedantic` is too useful to disable completely.
Re:Useful GCC warning options not enabled by -Wall -Wextra by daurnimator on Thursday October 05, 2017 @04:23AM (#15407463)
Try using intptr_t
Re:Useful GCC warning options not enabled by -Wall -Wextra by millstone on Wednesday October 04, 2017 @03:23PM (#15403628)
When I download some random unmaintained tarball from years back, I don't want to have to fight with the compiler. Just build dammit.
Re:Useful GCC warning options not enabled by -Wall -Wextra by anyfoo on Wednesday October 04, 2017 @03:54PM (#15403933)
"When I buy some random unmaintained car from years back, I don't want to have to fight with the transmission fault mode. Just drive dammit."
Re:Useful GCC warning options not enabled by -Wall -Wextra by dottedmag on Thursday October 05, 2017 @04:03AM (#15407369)
The better analogy is: "When I pick up some blueprint for the building from 80s I don't want to have to fight with changed sensibilities of the foreman. Just build the thing dammit."
Re:Useful GCC warning options not enabled by -Wall -Wextra by throwaway2048 on Wednesday October 04, 2017 @04:30PM (#15404320)
Software doesn't mechanically wear over time, this is an awful analogy.
Re:Useful GCC warning options not enabled by -Wall -Wextra by anyfoo on Wednesday October 04, 2017 @04:46PM (#15404467)
Yeah, I admit. A better analogy on my part would have been that you buy a car and it's not street legal anymore, or one that does not work in today's environment anymore. "I don't want to add seat belts, just drive" or "I don't want it to run on unleaded fuel". (Note that I have no idea if there actually were cars that required leaded fuel.)

Anyway, I think even more likely than running into new compiler warnings/errors is running into external dependency issues, which require you to touch the software anyway, and, I'd argue, are often much harder to fix than fixing code that triggers warnings.

Re:Useful GCC warning options not enabled by -Wall -Wextra by AgentME on Wednesday October 04, 2017 @04:41PM (#15404423)
If you build old software that relied on a specific compiler treating undefined behavior in a certain way, then it could.
Re:Useful GCC warning options not enabled by -Wall -Wextra by jcelerier on Wednesday October 04, 2017 @04:53PM (#15404536)
yes, people actively rewrite it in rust instead

More seriously, software certainly wears, not relative to itself but relative to the environment it is run in. It wears in the sense that active maintenance operations are needed to have old software perform as it should: installing old libraries, running it in emulators, etc.

Re:Useful GCC warning options not enabled by -Wall -Wextra by Arkanosis on Wednesday October 04, 2017 @04:38PM (#15404389)
You'd be surprised…
Re:Useful GCC warning options not enabled by -Wall -Wextra by digi_owl on Wednesday October 04, 2017 @04:44PM (#15404446)
What are the odds that a tar-ball suffering bitrot will decompress cleanly?
Re:Useful GCC warning options not enabled by -Wall -Wextra by Arkanosis on Wednesday October 04, 2017 @05:02PM (#15404626)
What I had in mind was UB changing over time more than tarballs getting corrupted (eg. strict aliasing-related optimizations).

There's practically no chance for a corrupted gzip-compressed tarball to decompress cleanly (even if by chance it inflates without error, there's a CRC32 of the uncompressed payload at the end of the gzip stream).

Re:Useful GCC warning options not enabled by -Wall -Wextra by kbenson on Wednesday October 04, 2017 @05:28PM (#15404862)
> What I had in mind was UB changing over time

I don't want to keep posting this[1] in every C discussion, but when it's relevant, it's relevant. TL;DR A particular version of a compiler interpreted undefined behavior in such a way as to elide code that specifically tested for an error case to exit early because the error implied undefined behavior, so the test could never be reached without undefined behavior. If that sounds like circular logic, that's because it is.

The error happened to exist in a fairly specific version of clang shipped by apple, but not in any of the major release versions:

I was not able to reproduce this bug on other platforms, perhaps because the compiler optimization is not applied by other compilers. On Linux, I tested GCC 4.9, 5.4, and 6.3, as well as Clang 3.6, 3.8, and 4.0. None were affected. Nevertheless, the compiler behavior is technically allowed, thus it should be assumed that it can happen on other platforms as well.

The specific compiler version which was observed to be affected is:

$ clang++ --version

Apple LLVM version 8.1.0 (clang-802.0.41)

Target: x86_64-apple-darwin16.5.0

Thread model: posix

InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

(Note: Despite being Clang-based, Apple's compiler version numbers have no apparent relationship to Clang version numbers.)

1: https://news.ycombinator.com/item?id=14163111

Re:Useful GCC warning options not enabled by -Wall -Wextra by anyfoo on Wednesday October 04, 2017 @02:51PM (#15403257)
> the same people who think that it's okay that memcpy(0, 0, 0) is undefined behavior.

I am curious about this particular example. How does that ever make sense, where do you have that?

I'd accept that it would be nice to have memcpy(p1, p2, n) with valid pointers p1 and p2 to still be defined when n=0, because maybe n is variable and might work out to be 0 in some cases, which you then don't need to treat specially. I don't know whether that case is defined or not.

But in the case where either p1 or p2 are NULL, the memcpy will be illegal no matter what n is, so if p1 or p2 are variable you always have to check them anyway, and to run into the memcpy(0, 0, 0) case you would actually have to special case n=0 when p1 or p2 were NULL, which makes no sense.

Unless 0 is actually a valid address (kernel code with identity mapping at 0, simple platforms), but then NULL becomes a whole different beast.

Re:Useful GCC warning options not enabled by -Wall -Wextra by quotemstr on Wednesday October 04, 2017 @03:26PM (#15403660)
Because memcpy(x, y, 0) for any x and y is a no-op. Why should we have a special undefined case for x==nullptr || x==nullptr? In general, we shouldn't introduce unnecessary boundary conditions to APIs.
Re:Useful GCC warning options not enabled by -Wall -Wextra by anyfoo on Wednesday October 04, 2017 @03:48PM (#15403873)
Because not making that case undefined puts additional and completely unnecessary burden on any implementation of memcpy? Now you have defined that memcpy(_, _, 0) is a no-op, so now the implementation has to make sure that it remains a no-op, which, if for instance a particular memcpy needs to perform an operation before the actual copy, may well involve adding conditionals to your implementation. For a case that a program will never sensibly run into (see my previous comment for why).
Re:Useful GCC warning options not enabled by -Wall -Wextra by quotemstr on Wednesday October 04, 2017 @03:50PM (#15403894)
It's perfectly sensible to run into the case: imagine we're copying from a buffer that's allocated on demand. In the case where we haven't allocated the buffer, we have buf==nullptr and buffsz=0. Why should it be wrong to memcpy(other_buffer, buf, bufsz) without first checking whether bufsz is zero (and thus buf is nullptr)?

I don't think your optimization actually exists. In what possible world can we generate better code by assuming that for memcpy(x,y,n), when n==0, x!=nullptr||y!=nullptr?

I think that in some cases, the claim that undefined behavior allows for better optimization is bullshit.

Re:Useful GCC warning options not enabled by -Wall -Wextra by anyfoo on Wednesday October 04, 2017 @04:14PM (#15404154)
> In the case where we haven't allocated the buffer, we have buf==nullptr and buffsz=0. Why should it be wrong to memcpy(other_buffer, buf, bufsz) without first checking whether bufsz is zero (and thus buf is nullptr)?

That's not enough. For the case that resolves to memcpy(0, 0, 0), other_buffer is also 0. That means you need to guard against memcpy (0, buf, buffsz) with buff/buffsz != 0 somewhere anyway. Unless you are saying that the standard says that the more general memcpy(p, 0, 0) is undefined, which is a slightly different discussion.

> I don't think your optimization actually exists. In what possible world can we generate better code by assuming that for memcpy(x,y,n), when n==0, x!=nullptr||y!=nullptr?

In any world where you need to e.g. lock the destination memory before accessing it, or do any other preparation/teardown. Not all platforms out there have one linear address space. In small scale applications, as just one example, it is not uncommon to have different pointer types for different types of memory, e.g. internal vs. external memory, I/O address spaces (lots of side effects there), maybe even a non-volatile memory address space.

In my opinion, it is easy and sensible to declare that memcpy(0,0,0) shall be undefined, especially because, again, no sensible program should ever reach that case without error (which, again, is different from the memcpy(p1, p2, 0) with p1/p2 != 0 case which can be sensible reached).

In fact, I'd argue that your proposal is the one that introduces the special case that all implementations would now have to take care of, adding complexity at best and a performance impact at worst, for something that a program won't ever do.

Re:Useful GCC warning options not enabled by -Wall -Wextra by quotemstr on Wednesday October 04, 2017 @04:38PM (#15404393)
Can you cite one example of a modern platform that would be meaningfully impacted by making memcpy(a,b,0) well-defined for all a, b? These hypothetical situations you're talking about aren't real. We don't have computers with 18-bit bytes or one's complement arithmetic either.

As for the distinction between a==0&&b==0 and a==0||b==0: first of all, the standard does make memcpy(a,b,0) undefined when a==0||b==0. Second, it's perfectly reasonable to run into a situation where both and b can be zero: consider the same demand-allocated buffer case. Why wouldn't you also want to demand-allocate the destination buffer?

Re:Useful GCC warning options not enabled by -Wall -Wextra by CamperBob2 on Wednesday October 04, 2017 @05:16PM (#15404777)
In my opinion, it is easy and sensible to declare that memcpy(0,0,0) shall be undefined, especially because, again, no sensible program should ever reach that case without error (which, again, is different from the memcpy(p1, p2, 0) with p1/p2 != 0 case which can be sensible reached).

It's common to see valid but semantically-meaningless constructs like that in autogenerated C code. Same with quite a few other cases mentioned in the article.

It's just not a good idea for a compiler author to arbitrarily start throwing warnings for memcpy(a,b,0) at this late date, regardless of a and b. That ship has sailed, leaks and all.

Re:Useful GCC warning options not enabled by -Wall -Wextra by dbaupp on Wednesday October 04, 2017 @10:12PM (#15406306)
You've phrased the result of the UB in a way that disguises the benefit: your complaint is adding a special case, and for an arbitrary memcpy(x, y, n) call, the compiler would have to provide it isn't in this special case (i.e. n != 0) or else it couldn't assume x != nullptr && y != nullptr. The codegen benefit isn't when n == 0, but when n is unknown.
Re:Useful GCC warning options not enabled by -Wall -Wextra by quotemstr on Wednesday October 04, 2017 @11:55PM (#15406592)
Huh? What special case? When n is zero, we copy no bytes. That's the opposite of a special case: it's a general rule!
Re:Useful GCC warning options not enabled by -Wall -Wextra by noselasd on Thursday October 05, 2017 @05:00AM (#15407650)
This is not how an assembly programmer sees this problem. (memcpy is often hand coded in assembly).

This means the code takes advantage of CPU pipelines and branch predictions, which means the source/destination pointer might be de-referenced and loaded into a register before the number of bytes is being inspected, or the assembly is executed with a 0 value, which still makes the CPU do a load or store on the src or dest pointer. This is being done to gain performance, and doing things in another order or explicitly validating the input decreases performance.

If then the src/dest pointer is NULL, the above scenario plays out badly, but in order to not sacrifice performance, the burden is placed on the caller to correctly use memcpy() instead of memcpy() detecting invalid use.

Re:Useful GCC warning options not enabled by -Wall -Wextra by quotemstr on Thursday October 05, 2017 @01:27PM (#15410882)
For memcpy(x,y,n), x and y can refer to the last byte on a page. What are you going to do, prefetch that one byte?
Re:Useful GCC warning options not enabled by -Wall -Wextra by dbaupp on Thursday October 05, 2017 @11:18AM (#15409869)
The rule is memcpy(x, y, n) is undefined if x or y are null. Making it defined if n is zero is a special case inside that rule.
Re:Useful GCC warning options not enabled by -Wall -Wextra by quotemstr on Thursday October 05, 2017 @01:27PM (#15410878)
Sure: except that rule isn't useful. You can't _rely_ on undefined behavior, so preserving the rule that "memcpy(x, y, n) is undefined if x or y are null" isn't helpful. OTOH, preserving the rule that memcpy(x,y,n) is defined for all x,y when n==0 really is useful.
Re:Useful GCC warning options not enabled by -Wall -Wextra by dbaupp on Thursday October 05, 2017 @03:22PM (#15411862)
It is useful. I'm focusing on the effect on code generation: the compiler relies on that rule to deduce that x and y are not null. The special case for n==0 makes it harder for the compiler to deduce the non-null-ness and thus harder for it to optimise based on that. Restricting when optimisations can be applied is the effect of your special case.

You might not agree with this as a design decision but I'm not arguing that this is the right trade-off, just answering:

> I don't think your optimization actually exists. In what possible world can we generate better code by assuming that for memcpy(x,y,n), when n==0, x!=nullptr||y!=nullptr?

Re:Useful GCC warning options not enabled by -Wall -Wextra by Someone on Thursday October 05, 2017 @03:29AM (#15407245)
But memcpy(0, x, y) is illegal for any x and y.

In mathematics, this is called a discontinuity. Google the value of 0^0 for comparable discussion (it ‘is’ 1 because that makes more formulas look nice)

Re:Useful GCC warning options not enabled by -Wall -Wextra by userbinator on Wednesday October 04, 2017 @10:04PM (#15406279)
The fundamental question can be summarised thus: "If you don't access an invalid address, is it still invalid?"

By my common sense, the answer is no.

Re:Useful GCC warning options not enabled by -Wall -Wextra by bennofs on Wednesday October 04, 2017 @12:43PM (#15401777)
I agree that using `-Werror` in release builds is a bad idea. But I don't think you need to "not make it the default" for this: just provide an easy way to disable it, and do it automatically for release builds. It should be on by default for any developer.
Re:Useful GCC warning options not enabled by -Wall -Wextra by slavik81 on Wednesday October 04, 2017 @04:46PM (#15404469)
I used to have it on by default, but I found it getting in my way while developing. I'd do things like add some temporary debug code, or comment out part of an algorithm and then end up with a bunch of compile errors to fix. The compiler rightly points out that the code is of poor quality, but that's because I'm not done yet.

Warnings in that case are still useful. If the project is otherwise warning-free then it's easy enough to spot the new warnings and judge if they represent a real mistake.

My preference today is to set -Werror on CI builds. Any code that's final should be warning-free, but it's unnecessarily painful for work-in-progress.

Re:Useful GCC warning options not enabled by -Wall -Wextra by quotemstr on Wednesday October 04, 2017 @04:57PM (#15404576)
In Java, the compiler complains about unreachable code and aborts. I think that's ridiculous. Sometimes it's useful to create unreachable code during development.

  int foo = doSomething();
  return 0;  // Debug XXX DONOTSUBMIT
  int bar = doSomethingElse();
  return foo + bar;
It's hilarious, though, that this workaround works:

  int foo = doSomething();
  if (1 > 0) return 0;  // Debug XXX DONOTSUBMIT
  int bar = doSomethingElse();
  return foo + bar;
Go's mandatory warnings are equally annoying. I'm sick and tired of little rarefied groups of language designers trying to impose their ideas about best practices on the rest of the world.
Re:Useful GCC warning options not enabled by -Wall -Wextra by wruza on Wednesday October 04, 2017 @05:53PM (#15405047)
What did you expect from a language that dictates to where to put your opening brace.
Re:Useful GCC warning options not enabled by -Wall -Wextra by vkou on Wednesday October 04, 2017 @08:34PM (#15405978)
What? That's a common style choice, not a language requirement.
Re:Useful GCC warning options not enabled by -Wall -Wextra by ameliaquining on Wednesday October 04, 2017 @11:29PM (#15406526)
You can't put the opening brace of a block on a separate line in Go, because the parser will insert a semicolon before it.
Re:Useful GCC warning options not enabled by -Wall -Wextra by vkou on Thursday October 05, 2017 @02:54AM (#15407112)
Sorry - I thought the conversation was about Java.
Re:Useful GCC warning options not enabled by -Wall -Wextra by accatyyc on Wednesday October 04, 2017 @03:43PM (#15403839)
Why should it? I just don’t get the obsession. As long as no one commits any warnings (and it’s easy to have a no warning policy), then I don’t see why it should be enabled. Sometimes I just quickly wanna throw in an unsafe printf here and there, or not change a type in every place when iterating and trying changes. It’s just strange to disallow yourself to try quick changes that warns because they might be unwanted in certain situations.

And don’t worry, if the new idea I’m trying is worth it, I will fix it up so it doesn’t have any warnings. And I saved a lot of time by not fixing all these warnings on the previous 2 ideas I tried.

Re:Useful GCC warning options not enabled by -Wall -Wextra by bluGill on Wednesday October 04, 2017 @05:49PM (#15405018)
Because painful experience has shown us time and time again that if a developer doesn't have it on all the time they will not let issues creep in. In theory you can use a CI system which does have it on, but those are not universal.
Re:Useful GCC warning options not enabled by -Wall -Wextra by accatyyc on Wednesday October 04, 2017 @06:54PM (#15405446)
Or... you can just fix the warnings before commiting. We have no tolerance for warnings on pushed code, and this has seriously never been a problem.

It’s just annoying to have to remove every unused variable/parameter when trying changes out.

Re:Useful GCC warning options not enabled by -Wall -Wextra by ajdlinux on Wednesday October 04, 2017 @08:37PM (#15405991)
Then use -Wno-* to disable specific warnings that are problematic, when they become problematic. That solves 90% of the issues with using -Werror.
Re:Useful GCC warning options not enabled by -Wall -Wextra by quickben on Wednesday October 04, 2017 @04:01PM (#15404006)
What's warning on one platform is usually error on another.

People that decide what memcpy does, have vastly different problems in mind (and hardware realities) from people complaining about it when using it on <random platform>.

Re:Useful GCC warning options not enabled by -Wall -Wextra by adekok on Wednesday October 04, 2017 @03:36PM (#15403777)
Using -Werror in development is annoying too. It works if everyone is running exactly the same OS, header files, and version of the compiler.

If not... different developers have different errors.

I try to build with all of the warnings turned on, multiple static analysis tools, and with multiple OS / compilers. Yet every time a compiler or OS is upgraded, there's a whole suite of new build warnings.

Re:Useful GCC warning options not enabled by -Wall -Wextra by bluGill on Wednesday October 04, 2017 @05:52PM (#15405038)
Have you tried this lately - in the last 10 years? things used to be bad, but clang and gcc at least have really cleaned up their act, most warning are now zero false positives and so you want to clean everything up. The only time I see the issue you described was when someone upgraded the compiler, and the new warnings were all real bugs.

Static analysis tools are a different problem: I find that most of them have so many false positives they are not worth looking at.

Re:Useful GCC warning options not enabled by -Wall -Wextra by adekok on Monday October 09, 2017 @08:14AM (#15433158)
> Have you tried this lately - in the last 10 years?

I'm wondering what it is about HN that lets people be condescending know-it-alls.

If you look, there's only one of me online. I write between 10K and 20K lines of C a year.

So yes, I have tried this in the last 10 years.

Re:Useful GCC warning options not enabled by -Wall -Wextra by loeg on Wednesday October 04, 2017 @11:49AM (#15401362)
Quite a few of the warnings not enabled in -Wall or -Wextra fail spuriously on good code. In combination with -Werror, new failures on perfectly fine code is frustrating for developers.
Re:Useful GCC warning options not enabled by -Wall -Wextra by krylon on Wednesday October 04, 2017 @12:11PM (#15401532)
I'd rather have a couple of false positives and then selectively disable warnings (I think one can use pragmas to disable specific warnings for specific regions of code in gcc).

At least, I would like an additional flag that really does enable all warnings, say -Wabsolutelyall or something like that.

Re:Useful GCC warning options not enabled by -Wall -Wextra by marcosdumay on Wednesday October 04, 2017 @12:58PM (#15401915)
You probably don't want that, but a flag that enables all sensible messages, yet does not enable stuff that is there just for compatibility or because it's useful in some context.

It should also disable Werror, otherwise we will be claiming for some new flags in a few years.

Re:Useful GCC warning options not enabled by -Wall -Wextra by craftyguy on Wednesday October 04, 2017 @11:42AM (#15401278)
that's a terrible excuse for not enabling them in -Wall. Is there a -Wreally-all?
Re:Useful GCC warning options not enabled by -Wall -Wextra by loeg on Wednesday October 04, 2017 @11:47AM (#15401332)
Clang has -Weverything. GCC doesn't: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66293
Re:Useful GCC warning options not enabled by -Wall -Wextra by jordigh on Wednesday October 04, 2017 @12:31PM (#15401690)
It looks like -Weverything for gcc would make a lot of code not compile (along with -Werror). For example, it would include -Weffc++, right? Those are warnings if your code just doesn't obey one guy's now-outdated opinions of what C++ should look like.

That's what this says at the bottom:

https://github.com/Tuplanolla/gcc-weverything

Re:Useful GCC warning options not enabled by -Wall -Wextra by jcelerier on Wednesday October 04, 2017 @12:39PM (#15401746)
> For example, it would include -Weffc++, right?

Maybe, but where's the problem ? Just add -Wno-effc++ afterwards.

Re:Useful GCC warning options not enabled by -Wall -Wextra by loeg on Wednesday October 04, 2017 @01:29PM (#15402232)
Yes, -Weverything + -Werror is often a silly combination. That isn't a GCC-specific problem, though. Clang with both flags will fail to compile quite a lot of code, too.

As the other reply points out, you can just use -Wno-effc++ on top of -Weverything. You could also avoid that particular problem by not using C++ ;-).

Re:Useful GCC warning options not enabled by -Wall -Wextra by kllrnohj on Wednesday October 04, 2017 @03:45PM (#15403850)
Note that Clang recommends you don't use -Weverything because it contains warnings that are in development or have a known high false-positive rate.
Re:Useful GCC warning options not enabled by -Wall -Wextra by klodolph on Wednesday October 04, 2017 @12:03PM (#15401467)
The actual reason why -Wall doesn't include more warnings is because too many people rely on the fact that -Wall includes warnings for questionable code that is easy to avoid. It's a bad name, but it's not being changed any time soon.
Re:Useful GCC warning options not enabled by -Wall -Wextra by sytelus on Friday October 06, 2017 @04:40AM (#15415307)
Everybody should use this in their new projects: http://shitalshah.com/p/how-to-enable-and-use-gcc-strict-mod...
Re:Useful GCC warning options not enabled by -Wall -Wextra by balls187 on Wednesday October 04, 2017 @11:34PM (#15406542)
If you knowingly upgrade libraries and/or other project dependencies you wouldn't expect backward compatibility (assume semver conventions), do you?

Why would upgrading a compiler be any different?