virbuffer: Simplify virBufferEscapeShell()

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@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
This commit is contained in:
Andrea Bolognani 2022-02-11 17:14:52 +01:00
parent f375533e07
commit 622e6293d9

View File

@ -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,20 +559,18 @@ virBufferEscapeShell(virBuffer *buf, const char *str)
if ((buf == NULL) || (str == NULL))
return;
if (!*str) {
virBufferAddLit(buf, "''");
return;
}
/* Only quote if str includes shell metacharacters. */
if (*str && !strpbrk(str, "\r\t\n !\"#$&'()*;<>?[\\]^`{|}~")) {
if (!strpbrk(str, "\r\t\n !\"#$&'()*;<>?[\\]^`{|}~")) {
virBufferAdd(buf, str, -1);
return;
}
if (*str) {
len = strlen(str);
escaped = g_malloc0_n(len + 1, 4);
} else {
virBufferAddLit(buf, "''");
return;
}
escaped = g_malloc0_n(strlen(str) + 1, 4);
cur = str;
out = escaped;