From: Lennart Poettering Date: Mon, 25 Nov 2019 14:15:38 +0000 (+0100) Subject: test-fileio: cast EOF to (char) before comparing with char explicitly X-Git-Tag: v244~27 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=72fd79b3ceff4004e2fbf8b324f418cfa86b59f7;p=thirdparty%2Fsystemd.git test-fileio: cast EOF to (char) before comparing with char explicitly EOF is defined to -1, hence on platforms that have "char" unsigned we can't compare it as-is, except if we accept an implicit cast. let's make it an explicit cast, acknowledging the issue. Fixes: #14118 --- diff --git a/src/test/test-fileio.c b/src/test/test-fileio.c index 56af5a8bc31..23c7d370d4e 100644 --- a/src/test/test-fileio.c +++ b/src/test/test-fileio.c @@ -1,6 +1,7 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ #include +#include #include #include @@ -639,14 +640,18 @@ static void test_fgetc(void) { f = fmemopen_unlocked((void*) chars, sizeof(chars), "re"); assert_se(f); - for (unsigned i = 0; i < sizeof(chars); i++) { + for (size_t i = 0; i < sizeof(chars); i++) { assert_se(safe_fgetc(f, &c) == 1); assert_se(c == chars[i]); - /* EOF is -1, and hence we can't push value 255 in this way if char is signed */ - assert_se(ungetc(c, f) != EOF || c == EOF); - assert_se(c == EOF || safe_fgetc(f, &c) == 1); - assert_se(c == chars[i]); + if (ungetc(c, f) == EOF) { + /* EOF is -1, and hence we can't push value 255 in this way – if char is signed */ + assert_se(c == (char) EOF); + assert_se(CHAR_MIN == -128); /* verify that char is signed on this platform */ + } else { + assert_se(safe_fgetc(f, &c) == 1); + assert_se(c == chars[i]); + } /* But it works when we push it properly cast */ assert_se(ungetc((unsigned char) c, f) != EOF);