From db45e242c45ac4317156bfc86a1b1715f6109503 Mon Sep 17 00:00:00 2001 From: Liav A Date: Sun, 16 Oct 2022 23:35:08 +0300 Subject: [PATCH] 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. --- Userland/Libraries/LibC/stdio.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Userland/Libraries/LibC/stdio.cpp b/Userland/Libraries/LibC/stdio.cpp index 5f609f2c38..a4d372e6d0 100644 --- a/Userland/Libraries/LibC/stdio.cpp +++ b/Userland/Libraries/LibC/stdio.cpp @@ -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(ch & 0xff); + } return EOF; }