mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2025-01-22 04:25:18 +00:00
virBufferTrim: do not accept len
Always trim the full specified suffix. All of the callers outside of tests were passing either strlen or the actual length of the string. Signed-off-by: Ján Tomko <jtomko@redhat.com> Reviewed-by: Peter Krempa <pkrempa@redhat.com>
This commit is contained in:
parent
4838e8b3fc
commit
0a125c7144
@ -1946,7 +1946,7 @@ virDomainUSBAddressPortFormatBuf(virBufferPtr buf,
|
|||||||
break;
|
break;
|
||||||
virBufferAsprintf(buf, "%u.", port[i]);
|
virBufferAsprintf(buf, "%u.", port[i]);
|
||||||
}
|
}
|
||||||
virBufferTrim(buf, ".", -1);
|
virBufferTrim(buf, ".");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -25521,7 +25521,7 @@ virDomainVirtioNetGuestOptsFormat(char **outstr,
|
|||||||
virBufferAsprintf(&buf, "ufo='%s' ",
|
virBufferAsprintf(&buf, "ufo='%s' ",
|
||||||
virTristateSwitchTypeToString(def->driver.virtio.guest.ufo));
|
virTristateSwitchTypeToString(def->driver.virtio.guest.ufo));
|
||||||
}
|
}
|
||||||
virBufferTrim(&buf, " ", -1);
|
virBufferTrim(&buf, " ");
|
||||||
|
|
||||||
*outstr = virBufferContentAndReset(&buf);
|
*outstr = virBufferContentAndReset(&buf);
|
||||||
return 0;
|
return 0;
|
||||||
@ -25561,7 +25561,7 @@ virDomainVirtioNetHostOptsFormat(char **outstr,
|
|||||||
virBufferAsprintf(&buf, "mrg_rxbuf='%s' ",
|
virBufferAsprintf(&buf, "mrg_rxbuf='%s' ",
|
||||||
virTristateSwitchTypeToString(def->driver.virtio.host.mrg_rxbuf));
|
virTristateSwitchTypeToString(def->driver.virtio.host.mrg_rxbuf));
|
||||||
}
|
}
|
||||||
virBufferTrim(&buf, " ", -1);
|
virBufferTrim(&buf, " ");
|
||||||
|
|
||||||
*outstr = virBufferContentAndReset(&buf);
|
*outstr = virBufferContentAndReset(&buf);
|
||||||
return 0;
|
return 0;
|
||||||
@ -30580,7 +30580,7 @@ virDomainGetBlkioParametersAssignFromDef(virDomainDefPtr def,
|
|||||||
def->blkio.devices[i].path, \
|
def->blkio.devices[i].path, \
|
||||||
def->blkio.devices[i].param); \
|
def->blkio.devices[i].param); \
|
||||||
} \
|
} \
|
||||||
virBufferTrim(&buf, ",", -1); \
|
virBufferTrim(&buf, ","); \
|
||||||
data = virBufferContentAndReset(&buf); \
|
data = virBufferContentAndReset(&buf); \
|
||||||
if (virTypedParameterAssign(&(params[(*nparams)++]), name, \
|
if (virTypedParameterAssign(&(params[(*nparams)++]), name, \
|
||||||
VIR_TYPED_PARAM_STRING, data) < 0) \
|
VIR_TYPED_PARAM_STRING, data) < 0) \
|
||||||
|
@ -1981,7 +1981,7 @@ x86FormatSignatures(virCPUx86ModelPtr model)
|
|||||||
(unsigned long)model->signatures[i]);
|
(unsigned long)model->signatures[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
virBufferTrim(&buf, ",", -1);
|
virBufferTrim(&buf, ",");
|
||||||
|
|
||||||
return virBufferContentAndReset(&buf);
|
return virBufferContentAndReset(&buf);
|
||||||
}
|
}
|
||||||
@ -3059,8 +3059,8 @@ virCPUx86UpdateLive(virCPUDefPtr cpu,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
virBufferTrim(&bufAdded, ",", -1);
|
virBufferTrim(&bufAdded, ",");
|
||||||
virBufferTrim(&bufRemoved, ",", -1);
|
virBufferTrim(&bufRemoved, ",");
|
||||||
|
|
||||||
added = virBufferContentAndReset(&bufAdded);
|
added = virBufferContentAndReset(&bufAdded);
|
||||||
removed = virBufferContentAndReset(&bufRemoved);
|
removed = virBufferContentAndReset(&bufRemoved);
|
||||||
|
@ -2360,7 +2360,7 @@ qemuBuildFloppyCommandLineControllerOptions(virCommandPtr cmd,
|
|||||||
|
|
||||||
if (explicitfdc && hasfloppy) {
|
if (explicitfdc && hasfloppy) {
|
||||||
/* Newer Q35 machine types require an explicit FDC controller */
|
/* Newer Q35 machine types require an explicit FDC controller */
|
||||||
virBufferTrim(&fdc_opts, ",", -1);
|
virBufferTrim(&fdc_opts, ",");
|
||||||
virCommandAddArg(cmd, "-device");
|
virCommandAddArg(cmd, "-device");
|
||||||
virCommandAddArgBuffer(cmd, &fdc_opts);
|
virCommandAddArgBuffer(cmd, &fdc_opts);
|
||||||
}
|
}
|
||||||
@ -3979,7 +3979,7 @@ qemuBuildHostNetStr(virDomainNetDefPtr net,
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
virBufferTrim(&buf, ",", -1);
|
virBufferTrim(&buf, ",");
|
||||||
|
|
||||||
return virBufferContentAndReset(&buf);
|
return virBufferContentAndReset(&buf);
|
||||||
}
|
}
|
||||||
@ -6330,7 +6330,7 @@ qemuBuildBootCommandLine(virCommandPtr cmd,
|
|||||||
if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_BOOT_STRICT))
|
if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_BOOT_STRICT))
|
||||||
virBufferAddLit(&boot_buf, "strict=on,");
|
virBufferAddLit(&boot_buf, "strict=on,");
|
||||||
|
|
||||||
virBufferTrim(&boot_buf, ",", -1);
|
virBufferTrim(&boot_buf, ",");
|
||||||
|
|
||||||
boot_opts_str = virBufferContentAndReset(&boot_buf);
|
boot_opts_str = virBufferContentAndReset(&boot_buf);
|
||||||
if (boot_opts_str) {
|
if (boot_opts_str) {
|
||||||
@ -7744,7 +7744,7 @@ qemuBuildGraphicsSPICECommandLine(virQEMUDriverConfigPtr cfg,
|
|||||||
* doesn't support it, it fallbacks to previous migration algorithm silently. */
|
* doesn't support it, it fallbacks to previous migration algorithm silently. */
|
||||||
virBufferAddLit(&opt, "seamless-migration=on,");
|
virBufferAddLit(&opt, "seamless-migration=on,");
|
||||||
|
|
||||||
virBufferTrim(&opt, ",", -1);
|
virBufferTrim(&opt, ",");
|
||||||
|
|
||||||
virCommandAddArg(cmd, "-spice");
|
virCommandAddArg(cmd, "-spice");
|
||||||
virCommandAddArgBuffer(cmd, &opt);
|
virCommandAddArgBuffer(cmd, &opt);
|
||||||
|
@ -357,7 +357,7 @@ virNetSSHCheckHostKey(virNetSSHSessionPtr sess)
|
|||||||
* we have to use a *MAGIC* constant. */
|
* we have to use a *MAGIC* constant. */
|
||||||
for (i = 0; i < 16; i++)
|
for (i = 0; i < 16; i++)
|
||||||
virBufferAsprintf(&buff, "%02hhX:", keyhash[i]);
|
virBufferAsprintf(&buff, "%02hhX:", keyhash[i]);
|
||||||
virBufferTrim(&buff, ":", 1);
|
virBufferTrim(&buff, ":");
|
||||||
|
|
||||||
keyhashstr = virBufferContentAndReset(&buff);
|
keyhashstr = virBufferContentAndReset(&buff);
|
||||||
|
|
||||||
|
@ -755,7 +755,7 @@ storageBackendCreateQemuImgOpts(virStorageEncryptionInfoDefPtr encinfo,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
virBufferTrim(&buf, ",", -1);
|
virBufferTrim(&buf, ",");
|
||||||
|
|
||||||
*opts = virBufferContentAndReset(&buf);
|
*opts = virBufferContentAndReset(&buf);
|
||||||
return 0;
|
return 0;
|
||||||
@ -4083,7 +4083,7 @@ virStorageBackendFileSystemMountAddOptions(virCommandPtr cmd,
|
|||||||
"mount_opts from XML", def->name, uuidstr);
|
"mount_opts from XML", def->name, uuidstr);
|
||||||
}
|
}
|
||||||
|
|
||||||
virBufferTrim(&buf, ",", -1);
|
virBufferTrim(&buf, ",");
|
||||||
mountOpts = virBufferContentAndReset(&buf);
|
mountOpts = virBufferContentAndReset(&buf);
|
||||||
|
|
||||||
if (mountOpts)
|
if (mountOpts)
|
||||||
|
@ -637,39 +637,27 @@ virBufferStrcat(virBufferPtr buf, ...)
|
|||||||
/**
|
/**
|
||||||
* virBufferTrim:
|
* virBufferTrim:
|
||||||
* @buf: the buffer to trim
|
* @buf: the buffer to trim
|
||||||
* @str: the optional string, to force an exact trim
|
* @str: the string to be trimmed from the tail
|
||||||
* @len: the number of bytes to trim, or -1 to use @str
|
|
||||||
*
|
*
|
||||||
* Trim the tail of a buffer. If @str is provided, the trim only occurs
|
* Trim the supplied string from the tail of the buffer.
|
||||||
* if the current tail of the buffer matches @str; a non-negative @len
|
|
||||||
* further limits how much of the tail is trimmed. If @str is NULL, then
|
|
||||||
* @len must be non-negative.
|
|
||||||
*/
|
*/
|
||||||
void
|
void
|
||||||
virBufferTrim(virBufferPtr buf, const char *str, int len)
|
virBufferTrim(virBufferPtr buf, const char *str)
|
||||||
{
|
{
|
||||||
size_t len2 = 0;
|
size_t len = 0;
|
||||||
|
|
||||||
if (!buf || !buf->str)
|
if (!buf || !buf->str)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (!str && len < 0)
|
if (!str)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
len = strlen(str);
|
||||||
|
|
||||||
if (len > 0 && len > buf->str->len)
|
if (len > buf->str->len ||
|
||||||
|
memcmp(&buf->str->str[buf->str->len - len], str, len) != 0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (str) {
|
|
||||||
len2 = strlen(str);
|
|
||||||
if (len2 > buf->str->len ||
|
|
||||||
memcmp(&buf->str->str[buf->str->len - len2], str, len2) != 0)
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (len < 0)
|
|
||||||
len = len2;
|
|
||||||
|
|
||||||
g_string_truncate(buf->str, buf->str->len - len);
|
g_string_truncate(buf->str, buf->str->len - len);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -91,7 +91,7 @@ void virBufferSetIndent(virBufferPtr, int indent);
|
|||||||
size_t virBufferGetIndent(const virBuffer *buf);
|
size_t virBufferGetIndent(const virBuffer *buf);
|
||||||
size_t virBufferGetEffectiveIndent(const virBuffer *buf);
|
size_t virBufferGetEffectiveIndent(const virBuffer *buf);
|
||||||
|
|
||||||
void virBufferTrim(virBufferPtr buf, const char *trim, int len);
|
void virBufferTrim(virBufferPtr buf, const char *trim);
|
||||||
void virBufferTrimChars(virBufferPtr buf, const char *trim);
|
void virBufferTrimChars(virBufferPtr buf, const char *trim);
|
||||||
void virBufferTrimLen(virBufferPtr buf, int len);
|
void virBufferTrimLen(virBufferPtr buf, int len);
|
||||||
void virBufferAddStr(virBufferPtr buf, const char *str);
|
void virBufferAddStr(virBufferPtr buf, const char *str);
|
||||||
|
@ -220,7 +220,7 @@ virQEMUBuildCommandLineJSON(virJSONValuePtr value,
|
|||||||
if (virQEMUBuildCommandLineJSONRecurse(NULL, value, buf, array, false) < 0)
|
if (virQEMUBuildCommandLineJSONRecurse(NULL, value, buf, array, false) < 0)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
virBufferTrim(buf, ",", -1);
|
virBufferTrim(buf, ",");
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -1432,7 +1432,7 @@ virResctrlAllocMemoryBandwidthFormat(virResctrlAllocPtr alloc,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
virBufferTrim(buf, ";", 1);
|
virBufferTrim(buf, ";");
|
||||||
virBufferAddChar(buf, '\n');
|
virBufferAddChar(buf, '\n');
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -1574,7 +1574,7 @@ virResctrlAllocFormatCache(virResctrlAllocPtr alloc,
|
|||||||
VIR_FREE(mask_str);
|
VIR_FREE(mask_str);
|
||||||
}
|
}
|
||||||
|
|
||||||
virBufferTrim(buf, ";", 1);
|
virBufferTrim(buf, ";");
|
||||||
virBufferAddChar(buf, '\n');
|
virBufferAddChar(buf, '\n');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -454,7 +454,7 @@ testQemuImageCreate(const void *opaque)
|
|||||||
|
|
||||||
virBufferStrcat(&actualbuf, "protocol:\n", NULLSTR(jsonprotocol),
|
virBufferStrcat(&actualbuf, "protocol:\n", NULLSTR(jsonprotocol),
|
||||||
"\nformat:\n", NULLSTR(jsonformat), NULL);
|
"\nformat:\n", NULLSTR(jsonformat), NULL);
|
||||||
virBufferTrim(&actualbuf, "\n", -1);
|
virBufferTrim(&actualbuf, "\n");
|
||||||
virBufferAddLit(&actualbuf, "\n");
|
virBufferAddLit(&actualbuf, "\n");
|
||||||
|
|
||||||
jsonpath = g_strdup_printf("%s%s.json", testQemuImageCreatePath, data->name);
|
jsonpath = g_strdup_printf("%s%s.json", testQemuImageCreatePath, data->name);
|
||||||
|
@ -2777,7 +2777,7 @@ testBlockNodeNameDetect(const void *opaque)
|
|||||||
|
|
||||||
virHashForEach(nodedata, testBlockNodeNameDetectFormat, &buf);
|
virHashForEach(nodedata, testBlockNodeNameDetectFormat, &buf);
|
||||||
|
|
||||||
virBufferTrim(&buf, "\n", -1);
|
virBufferTrim(&buf, "\n");
|
||||||
|
|
||||||
actual = virBufferContentAndReset(&buf);
|
actual = virBufferContentAndReset(&buf);
|
||||||
|
|
||||||
@ -2919,7 +2919,7 @@ testQueryJobs(const void *opaque)
|
|||||||
for (i = 0; i < njobs; i++)
|
for (i = 0; i < njobs; i++)
|
||||||
testQueryJobsPrintJob(&buf, jobs[i]);
|
testQueryJobsPrintJob(&buf, jobs[i]);
|
||||||
|
|
||||||
virBufferTrim(&buf, "\n", -1);
|
virBufferTrim(&buf, "\n");
|
||||||
|
|
||||||
actual = virBufferContentAndReset(&buf);
|
actual = virBufferContentAndReset(&buf);
|
||||||
|
|
||||||
|
@ -102,21 +102,20 @@ static int testBufTrim(const void *data G_GNUC_UNUSED)
|
|||||||
const char *expected = "a,b";
|
const char *expected = "a,b";
|
||||||
int ret = -1;
|
int ret = -1;
|
||||||
|
|
||||||
virBufferTrim(buf, "", 0);
|
virBufferTrim(buf, "");
|
||||||
buf = &bufinit;
|
buf = &bufinit;
|
||||||
|
|
||||||
virBufferAddLit(buf, "a;");
|
virBufferAddLit(buf, "a;");
|
||||||
virBufferTrim(buf, "", 0);
|
virBufferTrim(buf, "");
|
||||||
virBufferTrim(buf, "", -1);
|
virBufferTrim(buf, "");
|
||||||
virBufferTrimLen(buf, 1);
|
virBufferTrimLen(buf, 1);
|
||||||
virBufferTrimLen(buf, 5);
|
virBufferTrimLen(buf, 5);
|
||||||
virBufferTrimLen(buf, 2);
|
virBufferTrimLen(buf, 2);
|
||||||
|
|
||||||
virBufferAddLit(buf, ",b,,");
|
virBufferAddLit(buf, ",b,,");
|
||||||
virBufferTrim(buf, NULL, -1);
|
virBufferTrim(buf, NULL);
|
||||||
virBufferTrim(buf, "b", -1);
|
virBufferTrim(buf, "b");
|
||||||
virBufferTrim(buf, "b,,", 1);
|
virBufferTrim(buf, ",,");
|
||||||
virBufferTrim(buf, ",", -1);
|
|
||||||
|
|
||||||
result = virBufferContentAndReset(buf);
|
result = virBufferContentAndReset(buf);
|
||||||
if (!result || STRNEQ(result, expected)) {
|
if (!result || STRNEQ(result, expected)) {
|
||||||
|
@ -8453,7 +8453,7 @@ cmdDesc(vshControl *ctl, const vshCmd *cmd)
|
|||||||
while ((opt = vshCommandOptArgv(ctl, cmd, opt)))
|
while ((opt = vshCommandOptArgv(ctl, cmd, opt)))
|
||||||
virBufferAsprintf(&buf, "%s ", opt->data);
|
virBufferAsprintf(&buf, "%s ", opt->data);
|
||||||
|
|
||||||
virBufferTrim(&buf, " ", -1);
|
virBufferTrim(&buf, " ");
|
||||||
|
|
||||||
desc = virBufferContentAndReset(&buf);
|
desc = virBufferContentAndReset(&buf);
|
||||||
|
|
||||||
@ -9570,7 +9570,7 @@ cmdQemuMonitorCommand(vshControl *ctl, const vshCmd *cmd)
|
|||||||
while ((opt = vshCommandOptArgv(ctl, cmd, opt)))
|
while ((opt = vshCommandOptArgv(ctl, cmd, opt)))
|
||||||
virBufferAsprintf(&buf, "%s ", opt->data);
|
virBufferAsprintf(&buf, "%s ", opt->data);
|
||||||
|
|
||||||
virBufferTrim(&buf, " ", -1);
|
virBufferTrim(&buf, " ");
|
||||||
|
|
||||||
monitor_cmd = virBufferContentAndReset(&buf);
|
monitor_cmd = virBufferContentAndReset(&buf);
|
||||||
|
|
||||||
@ -9863,7 +9863,7 @@ cmdQemuAgentCommand(vshControl *ctl, const vshCmd *cmd)
|
|||||||
while ((opt = vshCommandOptArgv(ctl, cmd, opt)))
|
while ((opt = vshCommandOptArgv(ctl, cmd, opt)))
|
||||||
virBufferAsprintf(&buf, "%s ", opt->data);
|
virBufferAsprintf(&buf, "%s ", opt->data);
|
||||||
|
|
||||||
virBufferTrim(&buf, " ", -1);
|
virBufferTrim(&buf, " ");
|
||||||
|
|
||||||
guest_agent_cmd = virBufferContentAndReset(&buf);
|
guest_agent_cmd = virBufferContentAndReset(&buf);
|
||||||
|
|
||||||
@ -14077,7 +14077,7 @@ cmdDomFSInfo(vshControl *ctl, const vshCmd *cmd)
|
|||||||
|
|
||||||
for (j = 0; j < info[i]->ndevAlias; j++)
|
for (j = 0; j < info[i]->ndevAlias; j++)
|
||||||
virBufferAsprintf(&targetsBuff, "%s,", info[i]->devAlias[j]);
|
virBufferAsprintf(&targetsBuff, "%s,", info[i]->devAlias[j]);
|
||||||
virBufferTrim(&targetsBuff, ",", -1);
|
virBufferTrim(&targetsBuff, ",");
|
||||||
|
|
||||||
targets = virBufferContentAndReset(&targetsBuff);
|
targets = virBufferContentAndReset(&targetsBuff);
|
||||||
|
|
||||||
|
@ -2225,7 +2225,7 @@ vshOutputLogFile(vshControl *ctl, int log_level, const char *msg_format,
|
|||||||
}
|
}
|
||||||
virBufferAsprintf(&buf, "%s ", lvl);
|
virBufferAsprintf(&buf, "%s ", lvl);
|
||||||
virBufferVasprintf(&buf, msg_format, ap);
|
virBufferVasprintf(&buf, msg_format, ap);
|
||||||
virBufferTrim(&buf, "\n", -1);
|
virBufferTrim(&buf, "\n");
|
||||||
virBufferAddChar(&buf, '\n');
|
virBufferAddChar(&buf, '\n');
|
||||||
|
|
||||||
str = virBufferContentAndReset(&buf);
|
str = virBufferContentAndReset(&buf);
|
||||||
@ -2517,7 +2517,7 @@ vshTreePrintInternal(vshControl *ctl,
|
|||||||
false, indent) < 0)
|
false, indent) < 0)
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
virBufferTrim(indent, " ", -1);
|
virBufferTrim(indent, " ");
|
||||||
|
|
||||||
/* If there was no child device, and we're the last in
|
/* If there was no child device, and we're the last in
|
||||||
* a list of devices, then print another blank line */
|
* a list of devices, then print another blank line */
|
||||||
|
Loading…
x
Reference in New Issue
Block a user