]> git.ipfire.org Git - thirdparty/elfutils.git/commitdiff
tests: Make sure to not call memcmp with NULL arguments.
authorMark Wielaard <mark@klomp.org>
Fri, 8 May 2020 22:18:20 +0000 (00:18 +0200)
committerMark Wielaard <mark@klomp.org>
Thu, 14 May 2020 12:30:57 +0000 (14:30 +0200)
GCC10 -fanalyzer thinks we are too clever:

elfputzdata.c: In function ‘main’:
elfputzdata.c:178:8: warning: use of possibly-NULL ‘orig_buf’ where
                     non-null expected [CWE-690]
                     [-Wanalyzer-possible-null-argument]
  178 |     && memcmp (orig_buf, d->d_buf, orig_size) == 0)

orig_buf can only be NULL when orig_size is zero, but it might still
be undefined behaviour. So don't try to be too smart and just check
whether we actually have an buffer.

Signed-off-by: Mark Wielaard <mark@klomp.org>
tests/ChangeLog
tests/elfputzdata.c

index 4e9ae020bef3d4e9b8f53384996c79d055caba0d..05aab3ef41e5e5f5a025ee264697fbb7aed44b29 100644 (file)
@@ -1,3 +1,8 @@
+2020-05-08  Mark Wielaard  <mark@klomp.org>
+
+       * elfputzdata.c (main): Explicitly check orig_buf is not NULL
+       before calling memcmp.
+
 2020-05-05  Mark Wielaard  <mark@klomp.org>
 
        * testfile-lto-gcc8.bz2: New test file.
index 66ab77ba7d5dbbdd0e919892d7762c76986140bc..0d9c020ecf7e1c1d97391cc0e91682cef452530c 100644 (file)
@@ -105,14 +105,17 @@ main (int argc, char *argv[])
                  printf ("Unexpected data size for orig section %zd\n", idx);
                  return -1;
                }
-             char *orig_buf = malloc (d->d_size);
-             if (orig_size > 0 && orig_buf == NULL)
+             char *orig_buf = NULL;
+             if (orig_size > 0)
                {
-                 printf ("No memory to copy section %zd data\n", idx);
-                 return -1;
+                 orig_buf = malloc (d->d_size);
+                 if (orig_buf == NULL)
+                   {
+                     printf ("No memory to copy section %zd data\n", idx);
+                     return -1;
+                   }
+                 memcpy (orig_buf, d->d_buf, orig_size);
                }
-             if (orig_size > 0)
-               memcpy (orig_buf, d->d_buf, orig_size);
 
              bool forced = false;
              if (gnu)
@@ -175,7 +178,8 @@ main (int argc, char *argv[])
                }
 
              if (new_size == orig_size
-                 && memcmp (orig_buf, d->d_buf, orig_size) == 0)
+                 && (orig_buf == NULL
+                     || memcmp (orig_buf, d->d_buf, orig_size) == 0))
                {
                  printf ("section %zd didn't compress\n", idx);
                  return -1;
@@ -211,7 +215,8 @@ main (int argc, char *argv[])
                  return -1;
                }
              if (newer_size != orig_size
-                 && memcmp (orig_buf, d->d_buf, orig_size) != 0)
+                 && (orig_buf == NULL
+                     || memcmp (orig_buf, d->d_buf, orig_size) != 0))
                {
                  printf ("section %zd didn't correctly uncompress\n", idx);
                  return -1;