We can exit early when the input is an empty string, and we can avoid storing the string length in a variable since we only use that information once. Signed-off-by: Andrea Bolognani <abologna@xxxxxxxxxx> --- src/util/virbuffer.c | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/src/util/virbuffer.c b/src/util/virbuffer.c index a4834174a1..19cf775a8c 100644 --- a/src/util/virbuffer.c +++ b/src/util/virbuffer.c @@ -552,7 +552,6 @@ virBufferURIEncodeString(virBuffer *buf, const char *str) void virBufferEscapeShell(virBuffer *buf, const char *str) { - int len; g_autofree char *escaped = NULL; char *out; const char *cur; @@ -560,21 +559,19 @@ virBufferEscapeShell(virBuffer *buf, const char *str) if ((buf == NULL) || (str == NULL)) return; - /* Only quote if str includes shell metacharacters. */ - if (*str && !strpbrk(str, "\r\t\n !\"#$&'()*;<>?[\\]^`{|}~")) { - virBufferAdd(buf, str, -1); + if (!*str) { + virBufferAddLit(buf, "''"); return; } - if (*str) { - len = strlen(str); - - escaped = g_malloc0_n(len + 1, 4); - } else { - virBufferAddLit(buf, "''"); + /* Only quote if str includes shell metacharacters. */ + if (!strpbrk(str, "\r\t\n !\"#$&'()*;<>?[\\]^`{|}~")) { + virBufferAdd(buf, str, -1); return; } + escaped = g_malloc0_n(strlen(str) + 1, 4); + cur = str; out = escaped; -- 2.34.1