LibC: Do an explicit static_cast in the fgetc function

We assumed that by returning a char in the fgetc function that an
implicit cast is sufficient, but apparently if that char contains 0xff,
the result int will be -1 (0xFFFFFFFF). To ensure this does not happen,
let's do an explicit casting.
This commit is contained in:
Liav A 2022-10-16 23:35:08 +03:00 committed by Linus Groh
parent 711f64d366
commit db45e242c4

View file

@ -633,8 +633,11 @@ int fgetc(FILE* stream)
VERIFY(stream);
char ch;
size_t nread = fread(&ch, sizeof(char), 1, stream);
if (nread == 1)
return ch;
if (nread == 1) {
// Note: We do this static_cast because otherwise when casting a char that contains
// 0xFF results in an int containing -1, so an explicit cast is required here.
return static_cast<int>(ch & 0xff);
}
return EOF;
}