"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.

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.

Regards
Dominique

-- 
-- 
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.
diff --git a/src/getchar.c b/src/getchar.c
index 0347514..4313e69 100644
--- a/src/getchar.c
+++ b/src/getchar.c
@@ -5218,7 +5218,7 @@ check_map(
 							       && s[2] != NUL)
 		    {
 			s += 3;
-			if (len > mp->m_keylen - 3)
+			if ((unsigned)len > (unsigned)mp->m_keylen - 3U)
 			    minlen = mp->m_keylen - 3;
 		    }
 		    if (STRNCMP(s, keys, minlen) == 0)

Raspunde prin e-mail lui