Discussion:
Why doesn't VS2008 detect this warning
(too old to reply)
David F.
2010-03-05 12:24:17 UTC
Permalink
A 1993 Borland compiler finds this problem (i>=0 always true) but VS2008
doesn't?

for (unsigned i = strlen("test") - 1; i>=0; i--) {
.. do stuff..
}
David Lowndes
2010-03-05 19:18:25 UTC
Permalink
Post by David F.
A 1993 Borland compiler finds this problem (i>=0 always true) but VS2008
doesn't?
for (unsigned i = strlen("test") - 1; i>=0; i--) {
.. do stuff..
}
I thought older versions of the MS compiler used to catch that one so
I'm disappointed to note that it doesn't catch it now.

It now only shows up with the static analyser :(

warning C6295: Ill-defined for-loop: 'unsigned int' values are always
of range '0' to '4294967295'. Loop executes infinitely

Dave
h***@40th.com
2010-03-06 13:44:35 UTC
Permalink
Post by David F.
A 1993 Borland compiler finds this problem (i>=0 always true) but VS2008
for (unsigned i = strlen("test") - 1; i>=0; i--) {
So where's the error? It's not illegal to shoot
yourself in the foot. How is that any different
than for (;;) {...}? DL said analyze found it.
Good enough to stop from pulling the trigger.
--
40th Floor - Software @ http://40th.com/
PhantasmX3 - The finest sound in the world
CastleKeeper - IP camera surveillance/recorder, NVR
David Lowndes
2010-03-06 14:32:21 UTC
Permalink
Post by h***@40th.com
So where's the error? It's not illegal to shoot
yourself in the foot. How is that any different
than for (;;) {...}? DL said analyze found it.
I think the point is that you can do the former unintentionally - it
was deemed worthwhile having as a warning in older products, so why
not now? I am surprised that it wasn't a warning, I'm pretty sure it
used to be caught.

The static analyser option isn't available in some editions of the
tools.

Dave
Alexander Grigoriev
2010-03-06 16:36:59 UTC
Permalink
Such warning (condition always true) was enabled in some version of DDK.
Unfortunately, it was creating too much noise from some macros. The warning
needed to be smarter, to distinct a constant expression from a non-constant
expression which is always true (or false).
Post by David Lowndes
Post by h***@40th.com
So where's the error? It's not illegal to shoot
yourself in the foot. How is that any different
than for (;;) {...}? DL said analyze found it.
I think the point is that you can do the former unintentionally - it
was deemed worthwhile having as a warning in older products, so why
not now? I am surprised that it wasn't a warning, I'm pretty sure it
used to be caught.
The static analyser option isn't available in some editions of the
tools.
Dave
David F.
2010-03-06 17:13:50 UTC
Permalink
Post by h***@40th.com
Post by David F.
A 1993 Borland compiler finds this problem (i>=0 always true) but VS2008
for (unsigned i = strlen("test") - 1; i>=0; i--) {
So where's the error? It's not illegal to shoot
yourself in the foot. How is that any different
than for (;;) {...}? DL said analyze found it.
Good enough to stop from pulling the trigger.
Because that's what a good compiler is supposed to do, help prevent
problems. Why have any warning or error checking at all, just compile it
and making assumptions on errors and no warning notifications and let the
programmer step through the 100's of thousands of lines of code trying to
find the small mistake.

In this case it was converting a class where a programmer used an int
instead of unsigned and int was causing a problem do to check that only
checked < some limit and the input could be negative causing a crash. but
due to the lack of VS ability to report problems, we had to solve it a
different way.
h***@40th.com
2010-03-07 02:04:07 UTC
Permalink
Post by David F.
Because that's what a good compiler is supposed to do, help prevent
problems. Why have any warning or error checking at all, just compile it
for (unsigned i = strlen("test") - 1; i>=0; i--) {
rc = 0;
}

...cpp(3005) : warning C6295: Ill-defined for-loop: 'unsigned int'
values are always of range '0' to '4294967295'. Loop executes infinitely

Just sayin' (use a tool that does what you want). Is
this in vs10 64-bit now?
--
40th Floor - Software @ http://40th.com/
PhantasmX3 - The finest sound in the world
CastleKeeper - IP camera surveillance/recorder, NVR
Jialiang Ge [MSFT]
2010-03-08 05:40:03 UTC
Permalink
Hi David

In Visual Studio 2008, if you enable /analyze switch
(http://msdn.microsoft.com/en-us/library/ms173498.aspx), the compiler will
produce the warning:

warning C6295: Ill-defined for-loop: 'unsigned int' values are always of
range '0' to '4294967295'. Loop executes infinitely

Is this what you are looking for?

Regards,
Jialiang Ge
Microsoft Online Community Support

=================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
=================================================

Loading...