]> xenbits.xensource.com Git - libvirt.git/commitdiff
tests: utils: Fix out-of-bounds access to memory in virTestCompareToFile
authorPeter Krempa <pkrempa@redhat.com>
Tue, 3 Apr 2018 14:16:52 +0000 (16:16 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Wed, 4 Apr 2018 10:04:21 +0000 (12:04 +0200)
Similarly to 3506f1ecfde, the contents of 'cmpcontent' may be an empty
string so the following code would access memory out of the array.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
tests/testutils.c

index 4bd1b63755f1e8f26da4887c794a0159aa963e14..7c095caac94a4b59df33b9f5fc57a54a0f79da12 100644 (file)
@@ -797,10 +797,11 @@ virTestCompareToFile(const char *strcontent,
 
     if (filecontent) {
         size_t filecontentLen = strlen(filecontent);
+        size_t cmpcontentLen = strlen(cmpcontent);
 
         if (filecontentLen > 0 &&
             filecontent[filecontentLen - 1] == '\n' &&
-            cmpcontent[strlen(cmpcontent) - 1] != '\n') {
+            (cmpcontentLen == 0 || cmpcontent[cmpcontentLen - 1] != '\n')) {
             if (virAsprintf(&fixedcontent, "%s\n", cmpcontent) < 0)
                 goto failure;
             cmpcontent = fixedcontent;