]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
test-fileio: cast EOF to (char) before comparing with char explicitly
authorLennart Poettering <lennart@poettering.net>
Mon, 25 Nov 2019 14:15:38 +0000 (15:15 +0100)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Tue, 26 Nov 2019 13:55:47 +0000 (14:55 +0100)
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
src/test/test-fileio.c

index 56af5a8bc31ada80b205c62d0a5d3b04daffd19a..23c7d370d4eb1a86fae3e8e500482631ec412d47 100644 (file)
@@ -1,6 +1,7 @@
 /* SPDX-License-Identifier: LGPL-2.1+ */
 
 #include <fcntl.h>
+#include <limits.h>
 #include <stdio.h>
 #include <unistd.h>
 
@@ -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);