Dominique Pellé wrote:

> "Bram Moolenaar wrote:
> 
> >> getchar.c: In function 'check_map':
> >> getchar.c:5221:7: warning: assuming signed overflow does not occur when
> >> assuming that (X - c) <= X is always true [-Wstrict-overflow]
> >>      if (len > mp->m_keylen - 3)
> >>         ^
> >
> > That looks like a compiler problem.
> 
> 
> Line getchar.cpp:5221 is as follows:
> 
> 5221    if (len > mp->m_keylen - 3)
> 
> I don't think that it is spurious warnings, What gcc is saying
> here is that when it compiled the code, it made an optimization
> which assumed that an addition or subtraction did not overflow
> on 32 bits. This is a fair assumption, since overflow with sign
> integer is undefined behavior. It is only well defined with unsigned
> integers. However, lots of code wrongly assume that signed
> integer overflow wrap, which is not correct, and can cause real
> bugs when cranking up optimizations. Hence the warning.

Unfortunately this is caused by recent C standard where people writing
compiler optimizers forced this "undefined behavior" so that they could
make programs 0.1% faster.  Before this signed integers always
predictably overflowed, since practically all CPUs work that way.
I never like "undefined behavior".

> In this case, if mp->m_keylen was INT_MIN, then
> "mp->keylen - 3" would overflow which would be undefined
> behavior (bug!). This is probably impossible in this case,
> because m_key_len is always a length so positive, so
> removing 3 will never overflow.
> 
> That warning does not happen with -O2 but happens with -O3
> on my machine.
> 
> Attached patch fixes the warning in getchar.cpp by casting
> to unsigned.

That makes it:
        if ((unsigned)len > (unsigned)mp->m_keylen - 3U)

That fixes the warning, but isn't that code wrong?  if mp->m_keylen is
zero the result must be TRUE.

-- 
ARTHUR:  You fight with the strength of many men, Sir knight.
         I am Arthur, King of the Britons.  [pause]
         I seek the finest and the bravest knights in the land to join me
         in my Court of Camelot.  [pause]
         You have proved yourself worthy; will you join me?  [pause]
         You make me sad.  So be it.  Come, Patsy.
BLACK KNIGHT:  None shall pass.
                                  The Quest for the Holy Grail (Monty Python)

 /// Bram Moolenaar -- [email protected] -- http://www.Moolenaar.net   \\\
///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\  an exciting new programming language -- http://www.Zimbu.org        ///
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///

-- 
-- 
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

--- 
You received this message because you are subscribed to the Google Groups 
"vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Raspunde prin e-mail lui