render / rpms / libvirt

Forked from rpms/libvirt 10 months ago
Clone
404507
From 38a50c517836b12720b963c498e303d677ec434c Mon Sep 17 00:00:00 2001
404507
Message-Id: <38a50c517836b12720b963c498e303d677ec434c@dist-git>
404507
From: Martin Kletzander <mkletzan@redhat.com>
404507
Date: Wed, 31 Jan 2018 16:32:23 +0100
404507
Subject: [PATCH] util: Fix leak in virStringTrimOptionalNewline
404507
404507
https://bugzilla.redhat.com/show_bug.cgi?id=1289368
404507
404507
Do not access any data if strlen() == 0.
404507
404507
Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
404507
(cherry picked from commit ea7b2a6d04f5c354b1cda941362ba8ac42e92278)
404507
Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
404507
---
404507
 src/util/virstring.c | 10 +++++++---
404507
 1 file changed, 7 insertions(+), 3 deletions(-)
404507
404507
diff --git a/src/util/virstring.c b/src/util/virstring.c
404507
index eac4774b53..b2ebce27ff 100644
404507
--- a/src/util/virstring.c
404507
+++ b/src/util/virstring.c
404507
@@ -1394,9 +1394,13 @@ virStringEncodeBase64(const uint8_t *buf, size_t buflen)
404507
  */
404507
 void virStringTrimOptionalNewline(char *str)
404507
 {
404507
-    char *tmp = str + strlen(str) - 1;
404507
-    if (*tmp == '\n')
404507
-        *tmp = '\0';
404507
+    size_t len = strlen(str);
404507
+
404507
+    if (!len)
404507
+        return;
404507
+
404507
+    if (str[len - 1] == '\n')
404507
+        str[len - 1] = '\0';
404507
 }
404507
 
404507
 
404507
-- 
404507
2.16.1
404507