]> git.ipfire.org Git - thirdparty/knot-dns.git/commitdiff
contrib/files: fix stream error handling (clang analyzer)
authorJan Hák <jan.hak@nic.cz>
Thu, 18 Sep 2025 09:01:11 +0000 (11:01 +0200)
committerDaniel Salzman <daniel.salzman@nic.cz>
Wed, 24 Sep 2025 09:06:52 +0000 (11:06 +0200)
POSIX / fread():
The file position indicator for the stream (if defined) shall be advanced by the
number of bytes successfully read. If an error occurs, the resulting value of
the file position indicator for the stream is unspecified. If a partial element
is read, its value is unspecified.

src/contrib/files.c

index 9fef7200142f6cd66390e83274f97596f82c289b..89a818676241f8865aa7ebeb4a864a1edfb7d870 100644 (file)
@@ -224,9 +224,11 @@ int copy_file(const char *dest, const char *src)
                goto done;
        }
 
-       ssize_t cnt;
+       assert(ret == 0);
+       size_t cnt;
        while ((cnt = fread(buf, sizeof(*buf), BUFSIZE, from)) != 0 &&
-              (ret = (fwrite(buf, sizeof(*buf), cnt, file) == cnt))) {
+              (ret = (fwrite(buf, sizeof(*buf), cnt, file) == cnt)) &&
+              (feof(from) | ferror(from)) == 0) {
        }
 
        ret = !ret || ferror(from);