1
0
mirror of https://gitlab.com/libvirt/libvirt.git synced 2025-01-11 09:17:52 +03:00

util: buffer: Simplify escape buffer allocations

Replace combinations of xalloc_oversized and VIR_ALLOC_N_QUIET by using
g_malloc0_n which does the checking internally.

This conversion is done with a semantic difference and slightly higher
memory requirements as I've opted to allocate one chunk more than
necessary rather than trying to accomodate the NUL byte separately.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
Peter Krempa 2019-10-24 08:49:57 +02:00
parent ff06e83407
commit 241057681a

View File

@ -467,11 +467,7 @@ virBufferEscapeString(virBufferPtr buf, const char *format, const char *str)
return; return;
} }
if (xalloc_oversized(6, len) || escaped = g_malloc0_n(len + 1, 6);
VIR_ALLOC_N_QUIET(escaped, 6 * len + 1) < 0) {
virBufferSetError(buf, errno);
return;
}
cur = str; cur = str;
out = escaped; out = escaped;
@ -616,11 +612,7 @@ virBufferEscape(virBufferPtr buf, char escape, const char *toescape,
return; return;
} }
if (xalloc_oversized(2, len) || escaped = g_malloc0_n(len + 1, 2);
VIR_ALLOC_N_QUIET(escaped, 2 * len + 1) < 0) {
virBufferSetError(buf, errno);
return;
}
cur = str; cur = str;
out = escaped; out = escaped;
@ -715,11 +707,8 @@ virBufferEscapeShell(virBufferPtr buf, const char *str)
if (*str) { if (*str) {
len = strlen(str); len = strlen(str);
if (xalloc_oversized(4, len) ||
VIR_ALLOC_N_QUIET(escaped, 4 * len + 3) < 0) { escaped = g_malloc0_n(len + 1, 4);
virBufferSetError(buf, errno);
return;
}
} else { } else {
virBufferAddLit(buf, "''"); virBufferAddLit(buf, "''");
return; return;