From: high.crew3868@fastmail.com   
      
   On 1/1/26 11:26 PM, Michael S wrote:   
   > On Thu, 1 Jan 2026 22:54:05 +0100   
   > highcrew wrote:   
   >> For the lazy, I report it here:   
   >>   
   >> int table[4] = {0};   
   >> int exists_in_table(int v)   
   >> {   
   >> // return true in one of the first 4 iterations   
   >> // or UB due to out-of-bounds access   
   >> for (int i = 0; i <= 4; i++) {   
   >> if (table[i] == v) return 1;   
   >> }   
   >> return 0;   
   >> }   
   >>   
   >> This is compiled (with no warning whatsoever) into:   
   >>   
   >> exists_in_table:   
   >> mov eax, 1   
   >> ret   
   >> table:   
   >> .zero 16   
   >>   
    > [...]   
    >   
   > IMHO, for compiler that eliminated all comparisons (I assume that it was   
   > gcc -O2/-O3) an absence of warning is a bug.   
      
   Correct: -O2. Same code compiled with -O0:   
   https://godbolt.org/z/xE61j3PdM -> Still no warnings, although it will   
   at least contain   
   the `return 0`.   
      
   Which doesn't mean it is better: the code is obviously wrong, and   
   *I think* the compiler should whine.   
      
    > It's worth reporting.   
      
   Do you think so? I think everybody already knows...   
      
   First off, this example is mentioned since forever on the cppreference   
   website. Secondly, no warning is issued by Clang either.   
      
   Again, with this I don't mean the compilers are behaving correctly.   
   I just think this situation is well known.   
      
   --   
   High Crew   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   
|