]> xenbits.xensource.com Git - libvirt.git/commitdiff
virTestCompareToFile: Don't access memory we don't own
authorMichal Privoznik <mprivozn@redhat.com>
Fri, 4 Aug 2017 13:22:53 +0000 (15:22 +0200)
committerMichal Privoznik <mprivozn@redhat.com>
Mon, 7 Aug 2017 08:44:06 +0000 (10:44 +0200)
After reading the contents of a file some cleanup is performed.
However, the check for it might access a byte outside of the
string - if the file is empty in the first place. Then strlen()
is zero.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
tests/testutils.c

index 71692f1fa56316145d1fc4cd1461f5231a920aeb..915cd3b7925bf98eb26ed0d19cd8a826d929a216 100644 (file)
@@ -795,12 +795,16 @@ virTestCompareToFile(const char *strcontent,
     if (virTestLoadFile(filename, &filecontent) < 0 && !virTestGetRegenerate())
         goto failure;
 
-    if (filecontent &&
-        filecontent[strlen(filecontent) - 1] == '\n' &&
-        strcontent[strlen(strcontent) - 1] != '\n') {
-        if (virAsprintf(&fixedcontent, "%s\n", strcontent) < 0)
-            goto failure;
-        cmpcontent = fixedcontent;
+    if (filecontent) {
+        size_t filecontentLen = strlen(filecontent);
+
+        if (filecontentLen > 0 &&
+            filecontent[filecontentLen - 1] == '\n' &&
+            strcontent[strlen(strcontent) - 1] != '\n') {
+            if (virAsprintf(&fixedcontent, "%s\n", strcontent) < 0)
+                goto failure;
+            cmpcontent = fixedcontent;
+        }
     }
 
     if (STRNEQ_NULLABLE(cmpcontent, filecontent)) {