]> git.ipfire.org Git - thirdparty/libarchive.git/commitdiff
Issue 695: check fwrite return value, sync up test harnesses
authorTim Kientzle <kientzle@gmail.com>
Fri, 29 Apr 2016 02:44:20 +0000 (19:44 -0700)
committerTim Kientzle <kientzle@gmail.com>
Fri, 29 Apr 2016 02:44:20 +0000 (19:44 -0700)
cat/test/main.c
cpio/test/main.c
libarchive/test/main.c
tar/test/main.c

index 8c414da1431f5640479af65c4347cebd1d47e586..319f68c774f0fbb1170d582cef05d75ea8021ed4 100644 (file)
@@ -780,6 +780,34 @@ assertion_equal_mem(const char *file, int line,
        return (0);
 }
 
+/* Verify that a block of memory is filled with the specified byte. */
+int
+assertion_memory_filled_with(const char *file, int line,
+    const void *_v1, const char *vd,
+    size_t l, const char *ld,
+    char b, const char *bd, void *extra)
+{
+       const char *v1 = (const char *)_v1;
+       size_t c = 0;
+       size_t i;
+       (void)ld; /* UNUSED */
+
+       assertion_count(file, line);
+
+       for (i = 0; i < l; ++i) {
+               if (v1[i] == b) {
+                       ++c;
+               }
+       }
+       if (c == l)
+               return (1);
+
+       failure_start(file, line, "%s (size %d) not filled with %s", vd, (int)l, bd);
+       logprintf("   Only %d bytes were correct\n", (int)c);
+       failure_finish(extra);
+       return (0);
+}
+
 /* Verify that the named file exists and is empty. */
 int
 assertion_empty_file(const char *filename, int line, const char *f1)
@@ -2276,7 +2304,10 @@ copy_reference_file(const char *name)
        /* Not a lot of error checking here; the input better be right. */
        out = fopen(name, "wb");
        while ((rbytes = fread(buff, 1, sizeof(buff), in)) > 0) {
-               fwrite(buff, 1, rbytes, out);
+               if (fwrite(buff, 1, rbytes, out) != rbytes) {
+                       logprintf("Error: fwrite\n");
+                       break;
+               }
        }
        fclose(out);
        fclose(in);
index 5801127443564862e8a40754075043fc97275909..fa22adfbc81a806d2380292278e524d5c3eb8909 100644 (file)
@@ -781,6 +781,34 @@ assertion_equal_mem(const char *file, int line,
        return (0);
 }
 
+/* Verify that a block of memory is filled with the specified byte. */
+int
+assertion_memory_filled_with(const char *file, int line,
+    const void *_v1, const char *vd,
+    size_t l, const char *ld,
+    char b, const char *bd, void *extra)
+{
+       const char *v1 = (const char *)_v1;
+       size_t c = 0;
+       size_t i;
+       (void)ld; /* UNUSED */
+
+       assertion_count(file, line);
+
+       for (i = 0; i < l; ++i) {
+               if (v1[i] == b) {
+                       ++c;
+               }
+       }
+       if (c == l)
+               return (1);
+
+       failure_start(file, line, "%s (size %d) not filled with %s", vd, (int)l, bd);
+       logprintf("   Only %d bytes were correct\n", (int)c);
+       failure_finish(extra);
+       return (0);
+}
+
 /* Verify that the named file exists and is empty. */
 int
 assertion_empty_file(const char *filename, int line, const char *f1)
@@ -2277,7 +2305,10 @@ copy_reference_file(const char *name)
        /* Not a lot of error checking here; the input better be right. */
        out = fopen(name, "wb");
        while ((rbytes = fread(buff, 1, sizeof(buff), in)) > 0) {
-               fwrite(buff, 1, rbytes, out);
+               if (fwrite(buff, 1, rbytes, out) != rbytes) {
+                       logprintf("Error: fwrite\n");
+                       break;
+               }
        }
        fclose(out);
        fclose(in);
index 8561129a8c00b3df5677588db1da4cda6d7b7ecf..e0af4314ea0195b033f75f19e9a655a43d7883d4 100644 (file)
@@ -2303,7 +2303,10 @@ copy_reference_file(const char *name)
        /* Not a lot of error checking here; the input better be right. */
        out = fopen(name, "wb");
        while ((rbytes = fread(buff, 1, sizeof(buff), in)) > 0) {
-               fwrite(buff, 1, rbytes, out);
+               if (fwrite(buff, 1, rbytes, out) != rbytes) {
+                       logprintf("Error: fwrite\n");
+                       break;
+               }
        }
        fclose(out);
        fclose(in);
index 33a6364b2cf1d66921b04530e2fcde30e32e0e94..90801a98bc8fdb2bcdf403391d777cde8afe22ec 100644 (file)
@@ -781,6 +781,34 @@ assertion_equal_mem(const char *file, int line,
        return (0);
 }
 
+/* Verify that a block of memory is filled with the specified byte. */
+int
+assertion_memory_filled_with(const char *file, int line,
+    const void *_v1, const char *vd,
+    size_t l, const char *ld,
+    char b, const char *bd, void *extra)
+{
+       const char *v1 = (const char *)_v1;
+       size_t c = 0;
+       size_t i;
+       (void)ld; /* UNUSED */
+
+       assertion_count(file, line);
+
+       for (i = 0; i < l; ++i) {
+               if (v1[i] == b) {
+                       ++c;
+               }
+       }
+       if (c == l)
+               return (1);
+
+       failure_start(file, line, "%s (size %d) not filled with %s", vd, (int)l, bd);
+       logprintf("   Only %d bytes were correct\n", (int)c);
+       failure_finish(extra);
+       return (0);
+}
+
 /* Verify that the named file exists and is empty. */
 int
 assertion_empty_file(const char *filename, int line, const char *f1)
@@ -2277,7 +2305,10 @@ copy_reference_file(const char *name)
        /* Not a lot of error checking here; the input better be right. */
        out = fopen(name, "wb");
        while ((rbytes = fread(buff, 1, sizeof(buff), in)) > 0) {
-               fwrite(buff, 1, rbytes, out);
+               if (fwrite(buff, 1, rbytes, out) != rbytes) {
+                       logprintf("Error: fwrite\n");
+                       break;
+               }
        }
        fclose(out);
        fclose(in);