From 3506f1ecfde2dd9af54d0cb0971af4792e7d3601 Mon Sep 17 00:00:00 2001 From: Michal Privoznik Date: Fri, 4 Aug 2017 15:22:53 +0200 Subject: [PATCH] virTestCompareToFile: Don't access memory we don't own 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 --- tests/testutils.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/tests/testutils.c b/tests/testutils.c index 71692f1fa5..915cd3b792 100644 --- a/tests/testutils.c +++ b/tests/testutils.c @@ -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)) { -- GitLab