Andre Kostur said:
Arguably they do make odd design decisions. Or more accurately, they are
prohibited from making the "correct" design decision, and make do with
what the language provides. An unsigned variable is self-documenting in
a way. It tells other programmers that this negative values are
completely invalid. Like the size of an array. If that size were
represented as a signed integer, then it implicitly states that an array
may have a negative size.
But a similar example would be the size of a block of memory. For instance
memcpy takes an unsigned type for its third parameter presumably since you
cannot copy a negative number of bytes of memory. But this loses the chance
to do some error checking.
For instance I might write some code like this
size_t off = p - q; // p >= q
memcpy(a, p, off);
In this code p and q are pointers to some location with the char array a. I
am assuming the off is a positive or zero quantity. But suppose because of
bugged code that is wrong, memcpy cannot refuse to copy the very large
positive number of bytes that will result, because it is designed to copy
any unsigned quantity. But if memcpy took a signed type as its third
parameter, it could then quite reasonably refuse to copy a meaningless
negative number of bytes, thus reducing the chance of a buffer overflow.
That particular overflow could not have happened but for the existence of
signed integers.
It could not have happened but for the mix. Surely you're not proposing to
ban signed integers. Now that really would be ridiculous!
It seems quite reasonable to blame the programmer for
ignoring compiler warnings.
I think if you look at the code the programmer did not ignore compiler
warnings. I'm guessing of course but I think he did add a cast to the while
loop condition because of a compiler warning, but did not to the subtraction
because that particular compiler does not produce a warning for that.
Of course a better reaction would have been to change the type of cbRead to
unsigned.
Around here (where I work), the mandate is
that you crank the compiler warnings as high as you can bear (when the
compiler starts complaining about it's own header files... time to stop
cranking the warnings....), and your compile must have no warnings.
If only everywhere was so enlightened. But what to you do about bogus
warnings? I've known compiler to warn me about this
if (x == 0 && y <10 || y > 10)
telling me that I really should put brackets around y <10 || y > 10.
Arguably I should but it's a style issue not something a compiler should
warn me about.
john