Cpp size_t 0 is smaller than minus one
Table of Contents
Be careful with your loops
size_t zero=0;
long minus_one=-1;
EXCPECT_FALSE( zero < minus_one ); // should be false, but in fact is true
Why that matters?
I got this bug:
long limit = -1; // default value was designed to skip the loop
...
if( .. ) { limit = optionalCalculatedValue; }
...
for( size_t i=0; i<limit; i++) {
// infinite loop on default limit value
}
Also, there was similar but less obvious situation is same project:
for( size_t i=0; i<parameter-1; i++) {
// infinite loop if parameter is 0
}
You can fix it like this:
for( size_t i=0; i+1<parameter; i++) {
// now it's ok
}