mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2025-02-22 11:22:23 +00:00
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:
parent
f375533e07
commit
622e6293d9
@ -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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user