Note: it has been suggested that this question duplicates Can I compare int with size_t directly in C?, but the question here specifically asks about comparing size_t with a negative value. For this reason, it should be re-opened. (And besides, it has generated a lot of thoughtful discussion!)
I'm staring at some library code (I am looking at you, Microchip), declared to return a size_t:
size_t SYS_FS_FileWrite(SYS_FS_HANDLE handle, const void *buf, size_t nbytes);
That is documented as returning -1 on an error. But size_t is an unsigned value. So, in theory and/or in practice, is the following allowed?
if (SYS_FS_FileWrite(handle, buf, nbytes) == -1) {
report_errror();
}
It is impossible for a routine that returns a
size_tto return −1, but it is perfectly fine for it to return the result of converting −1 tosize_t(presuming the relevant environment does not need that value for any conflicting purpose). If the documentation states the latter, it is fine. If it states the former, the documentation is sloppily written and probably means the latter.In a comparison of some
size_tvaluexto-1, as inx == -1, the value −1 will be converted to the typesize_tif the rank ofsize_tequals or exceeds the rank ofint. This is the case in most C implementations and would be expected in an implementation that uses −1 converted tosize_tas a return value. In a C implementation in whichsize_thad lower rank thanint,xcould be converted toint(depending on some specifics of the types). That would not change the value, andx == -1would always evaluate as false.Per a request in the comments for a test of whether
SYS_FS_FileWrite(handle, buf, nbytes) == -1is a safe test forSYS_FS_FileWritereturning −1 converted tosize_tin light of the fact that the usual arithmetic conversions might not produce the desired results, a suitable test is_Static_assert((size_t) -1 == -1, "size_t type is too narrow.");. Also, the test could be written asSYS_FS_FileWrite(handle, buf, nbytes) == (size_t) -1.