]> git.ipfire.org Git - thirdparty/valgrind.git/commitdiff
s390x: disasm-test tweaks
authorFlorian Krohm <flo2030@eich-krohm.de>
Mon, 24 Mar 2025 14:44:07 +0000 (14:44 +0000)
committerFlorian Krohm <flo2030@eich-krohm.de>
Mon, 24 Mar 2025 14:44:07 +0000 (14:44 +0000)
- Give error messages a prefix.
- Do not segfault when the .dump file is empty. This happens when the
  generated testcase has compiler errors.
- Avoid a file leak when reading the .dump file causes an I/O error.

none/tests/s390x/disasm-test/main.c
none/tests/s390x/disasm-test/objdump.c

index 75d007b9331708a98bc8b8c0be204d440f4bdd8c..46f55cf4ce47985ef602ae967ccfeb3d6ef76712 100644 (file)
@@ -296,6 +296,7 @@ error(const char *fmt, ...)
 {
    va_list args;
    va_start(args, fmt);
+   fprintf(stderr, "error: ");
    vfprintf(stderr, fmt, args);
    va_end(args);
 }
index 62d8346540bee4e3266172dfb8ea58cf774a936d..7100e4742090e8f9a25fd3214ec57830dbe268b4 100644 (file)
@@ -32,6 +32,7 @@
 
 static int get_nibble(const char *);
 static int get_byte(const char *);
+static void io_error(FILE *, const char *, const char *);
 
 
 objdump_file *
@@ -43,20 +44,24 @@ read_objdump(const char *file)
    /* Slurp file into memory */
    FILE *fp = fopen(file, "rb");
    if (fp == NULL) {
-      error("%s: fopen failed\n", file);
+      io_error(fp, file, "fopen failed\n");
       return NULL;
    }
 
    /* Determine file size */
    int rc = fseek(fp, 0, SEEK_END);
    if (rc < 0) {
-      error("%s: fseek failed\n", file);
+      io_error(fp, file, "fseek failed\n");
       return NULL;
    }
 
    long size = ftell(fp);
    if (size < 0) {
-      error("%s: ftell failed\n", file);
+      io_error(fp, file, "ftell failed\n");
+      return NULL;
+   }
+   if (size == 0) {
+      io_error(fp, file, "file is empty\n");
       return NULL;
    }
    rewind(fp);
@@ -64,7 +69,7 @@ read_objdump(const char *file)
    char *const buf = mallock(size + 1);
    size_t num_read = fread(buf, 1, size, fp);
    if (num_read != size) {
-      error("%s: fread failed\n", file);
+      io_error(fp, file, "fread failed\n");
       free(buf);
       return NULL;
    }
@@ -235,3 +240,11 @@ get_byte(const char *p)
 
    return (n1 << 4) + n2;
 }
+
+static void
+io_error(FILE *fp, const char *file, const char *msg)
+{
+   if (fp)
+      fclose(fp);
+   error("%s: %s", file, msg);
+}