richardphibel / rpms / systemd

Forked from rpms/systemd 2 years ago
Clone
eb6853
From a7415ab87fa5cc7cae356aa296e5c3fec4add1bf Mon Sep 17 00:00:00 2001
eb6853
From: Yu Watanabe <watanabe.yu+github@gmail.com>
eb6853
Date: Wed, 31 Jan 2018 23:43:28 +0900
eb6853
Subject: [PATCH] strv: fix buffer size calculation in strv_join_quoted()
eb6853
eb6853
Fixes #8056.
eb6853
eb6853
[fbui: the affected function was removed since v236+ (by commit
eb6853
       2e59b24) so the patch is not needed by upstream which was at
eb6853
       v237+ when the issue was found.]
eb6853
eb6853
rhel-only
eb6853
Resolves: #1989245
eb6853
---
eb6853
 src/shared/strv.c | 13 +++++--------
eb6853
 1 file changed, 5 insertions(+), 8 deletions(-)
eb6853
eb6853
diff --git a/src/shared/strv.c b/src/shared/strv.c
eb6853
index e27ac68151..416373b183 100644
eb6853
--- a/src/shared/strv.c
eb6853
+++ b/src/shared/strv.c
eb6853
@@ -353,21 +353,18 @@ char *strv_join_quoted(char **l) {
eb6853
         size_t allocated = 0, len = 0;
eb6853
 
eb6853
         STRV_FOREACH(s, l) {
eb6853
-                /* assuming here that escaped string cannot be more
eb6853
-                 * than twice as long, and reserving space for the
eb6853
-                 * separator and quotes.
eb6853
-                 */
eb6853
                 _cleanup_free_ char *esc = NULL;
eb6853
                 size_t needed;
eb6853
 
eb6853
-                if (!GREEDY_REALLOC(buf, allocated,
eb6853
-                                    len + strlen(*s) * 2 + 3))
eb6853
-                        goto oom;
eb6853
-
eb6853
                 esc = cescape(*s);
eb6853
                 if (!esc)
eb6853
                         goto oom;
eb6853
 
eb6853
+                /* reserving space for the escaped text, separator, quotes and NULL terminator. */
eb6853
+                if (!GREEDY_REALLOC(buf, allocated,
eb6853
+                                    len + strlen(esc) + 4))
eb6853
+                        goto oom;
eb6853
+
eb6853
                 needed = snprintf(buf + len, allocated - len, "%s\"%s\"",
eb6853
                                   len > 0 ? " " : "", esc);
eb6853
                 assert(needed < allocated - len);