提交 8eaa7089 编写于 作者: M Michal Privoznik

Use g_strdup_vprintf() instead of virVasprintf() everywhere

Signed-off-by: NMichal Privoznik <mprivozn@redhat.com>
Reviewed-by: NDaniel Henrique Barboza <danielhb413@gmail.com>
上级 d36a15f9
...@@ -74,8 +74,7 @@ libvirt_vmessage(xentoollog_logger *logger_in, ...@@ -74,8 +74,7 @@ libvirt_vmessage(xentoollog_logger *logger_in,
if (level < lg->minLevel) if (level < lg->minLevel)
return; return;
if (virVasprintf(&message, format, args) < 0) message = g_strdup_vprintf(format, args);
return;
/* Should we print to a domain-specific log file? */ /* Should we print to a domain-specific log file? */
if ((start = strstr(message, ": Domain ")) && if ((start = strstr(message, ": Domain ")) &&
......
...@@ -9430,8 +9430,7 @@ int qemuDomainLogContextWrite(qemuDomainLogContextPtr ctxt, ...@@ -9430,8 +9430,7 @@ int qemuDomainLogContextWrite(qemuDomainLogContextPtr ctxt,
va_start(argptr, fmt); va_start(argptr, fmt);
if (virVasprintf(&message, fmt, argptr) < 0) message = g_strdup_vprintf(fmt, argptr);
goto cleanup;
if (!ctxt->manager && if (!ctxt->manager &&
lseek(ctxt->writefd, 0, SEEK_END) < 0) { lseek(ctxt->writefd, 0, SEEK_END) < 0) {
virReportSystemError(errno, "%s", virReportSystemError(errno, "%s",
...@@ -9524,8 +9523,7 @@ qemuDomainLogAppendMessage(virQEMUDriverPtr driver, ...@@ -9524,8 +9523,7 @@ qemuDomainLogAppendMessage(virQEMUDriverPtr driver,
va_start(ap, fmt); va_start(ap, fmt);
if (virVasprintf(&message, fmt, ap) < 0) message = g_strdup_vprintf(fmt, ap);
goto cleanup;
VIR_DEBUG("Append log message (vm='%s' message='%s) stdioLogD=%d", VIR_DEBUG("Append log message (vm='%s' message='%s) stdioLogD=%d",
vm->def->name, message, cfg->stdioLogD); vm->def->name, message, cfg->stdioLogD);
......
...@@ -110,8 +110,7 @@ void virAuditSend(virLogSourcePtr source, ...@@ -110,8 +110,7 @@ void virAuditSend(virLogSourcePtr source,
#endif #endif
va_start(args, fmt); va_start(args, fmt);
if (virVasprintf(&str, fmt, args) < 0) str = g_strdup_vprintf(fmt, args);
VIR_WARN("Out of memory while formatting audit message");
va_end(args); va_end(args);
if (auditlog && str) { if (auditlog && str) {
......
...@@ -1314,11 +1314,7 @@ virCommandAddEnvFormat(virCommandPtr cmd, const char *format, ...) ...@@ -1314,11 +1314,7 @@ virCommandAddEnvFormat(virCommandPtr cmd, const char *format, ...)
return; return;
va_start(list, format); va_start(list, format);
if (virVasprintf(&env, format, list) < 0) { env = g_strdup_vprintf(format, list);
cmd->has_error = ENOMEM;
va_end(list);
return;
}
va_end(list); va_end(list);
virCommandAddEnv(cmd, env); virCommandAddEnv(cmd, env);
...@@ -1541,11 +1537,7 @@ virCommandAddArgFormat(virCommandPtr cmd, const char *format, ...) ...@@ -1541,11 +1537,7 @@ virCommandAddArgFormat(virCommandPtr cmd, const char *format, ...)
return; return;
va_start(list, format); va_start(list, format);
if (virVasprintf(&arg, format, list) < 0) { arg = g_strdup_vprintf(format, list);
cmd->has_error = ENOMEM;
va_end(list);
return;
}
va_end(list); va_end(list);
/* Arg plus trailing NULL. */ /* Arg plus trailing NULL. */
......
...@@ -830,7 +830,7 @@ virRaiseErrorFull(const char *filename, ...@@ -830,7 +830,7 @@ virRaiseErrorFull(const char *filename,
} else { } else {
va_list ap; va_list ap;
va_start(ap, fmt); va_start(ap, fmt);
ignore_value(virVasprintfQuiet(&str, fmt, ap)); str = g_strdup_vprintf(fmt, ap);
va_end(ap); va_end(ap);
} }
...@@ -1484,16 +1484,13 @@ virLastErrorPrefixMessage(const char *fmt, ...) ...@@ -1484,16 +1484,13 @@ virLastErrorPrefixMessage(const char *fmt, ...)
return; return;
va_start(args, fmt); va_start(args, fmt);
fmtmsg = g_strdup_vprintf(fmt, args);
if (virVasprintfQuiet(&fmtmsg, fmt, args) < 0) va_end(args);
goto cleanup;
newmsg = g_strdup_printf("%s: %s", fmtmsg, err->message); newmsg = g_strdup_printf("%s: %s", fmtmsg, err->message);
VIR_FREE(err->message); VIR_FREE(err->message);
err->message = g_steal_pointer(&newmsg); err->message = g_steal_pointer(&newmsg);
cleanup:
va_end(args);
errno = save_errno; errno = save_errno;
} }
...@@ -3374,9 +3374,9 @@ virFileRemoveLastComponent(char *path) ...@@ -3374,9 +3374,9 @@ virFileRemoveLastComponent(char *path)
/** /**
* virFilePrintf: * virFilePrintf:
* *
* A replacement for fprintf() which uses virVasprintf to * A replacement for fprintf() which uses g_strdup_vprintf
* ensure that portable string format placeholders can be * to ensure that portable string format placeholders can
* used, since gnulib's fprintf() replacement is not * be used, since gnulib's fprintf() replacement is not
* LGPLV2+ compatible * LGPLV2+ compatible
*/ */
int virFilePrintf(FILE *fp, const char *msg, ...) int virFilePrintf(FILE *fp, const char *msg, ...)
...@@ -3387,8 +3387,7 @@ int virFilePrintf(FILE *fp, const char *msg, ...) ...@@ -3387,8 +3387,7 @@ int virFilePrintf(FILE *fp, const char *msg, ...)
va_start(vargs, msg); va_start(vargs, msg);
if (virVasprintf(&str, msg, vargs) < 0) str = g_strdup_vprintf(msg, vargs);
goto cleanup;
ret = strlen(str); ret = strlen(str);
if (fwrite(str, 1, ret, fp) != ret) { if (fwrite(str, 1, ret, fp) != ret) {
...@@ -3397,7 +3396,6 @@ int virFilePrintf(FILE *fp, const char *msg, ...) ...@@ -3397,7 +3396,6 @@ int virFilePrintf(FILE *fp, const char *msg, ...)
ret = -1; ret = -1;
} }
cleanup:
va_end(vargs); va_end(vargs);
return ret; return ret;
...@@ -4149,10 +4147,7 @@ virFileReadValueInt(int *value, const char *format, ...) ...@@ -4149,10 +4147,7 @@ virFileReadValueInt(int *value, const char *format, ...)
va_list ap; va_list ap;
va_start(ap, format); va_start(ap, format);
if (virVasprintf(&path, format, ap) < 0) { path = g_strdup_vprintf(format, ap);
va_end(ap);
return -1;
}
va_end(ap); va_end(ap);
if (!virFileExists(path)) if (!virFileExists(path))
...@@ -4192,10 +4187,7 @@ virFileReadValueUint(unsigned int *value, const char *format, ...) ...@@ -4192,10 +4187,7 @@ virFileReadValueUint(unsigned int *value, const char *format, ...)
va_list ap; va_list ap;
va_start(ap, format); va_start(ap, format);
if (virVasprintf(&path, format, ap) < 0) { path = g_strdup_vprintf(format, ap);
va_end(ap);
return -1;
}
va_end(ap); va_end(ap);
if (!virFileExists(path)) if (!virFileExists(path))
...@@ -4236,10 +4228,7 @@ virFileReadValueScaledInt(unsigned long long *value, const char *format, ...) ...@@ -4236,10 +4228,7 @@ virFileReadValueScaledInt(unsigned long long *value, const char *format, ...)
va_list ap; va_list ap;
va_start(ap, format); va_start(ap, format);
if (virVasprintf(&path, format, ap) < 0) { path = g_strdup_vprintf(format, ap);
va_end(ap);
return -1;
}
va_end(ap); va_end(ap);
if (!virFileExists(path)) if (!virFileExists(path))
...@@ -4282,10 +4271,7 @@ virFileReadValueBitmap(virBitmapPtr *value, const char *format, ...) ...@@ -4282,10 +4271,7 @@ virFileReadValueBitmap(virBitmapPtr *value, const char *format, ...)
va_list ap; va_list ap;
va_start(ap, format); va_start(ap, format);
if (virVasprintf(&path, format, ap) < 0) { path = g_strdup_vprintf(format, ap);
va_end(ap);
return -1;
}
va_end(ap); va_end(ap);
if (!virFileExists(path)) if (!virFileExists(path))
...@@ -4323,10 +4309,7 @@ virFileReadValueString(char **value, const char *format, ...) ...@@ -4323,10 +4309,7 @@ virFileReadValueString(char **value, const char *format, ...)
va_list ap; va_list ap;
va_start(ap, format); va_start(ap, format);
if (virVasprintf(&path, format, ap) < 0) { path = g_strdup_vprintf(format, ap);
va_end(ap);
return -1;
}
va_end(ap); va_end(ap);
if (!virFileExists(path)) if (!virFileExists(path))
......
...@@ -509,19 +509,15 @@ void virFirewallRuleAddArgFormat(virFirewallPtr firewall, ...@@ -509,19 +509,15 @@ void virFirewallRuleAddArgFormat(virFirewallPtr firewall,
VIR_FIREWALL_RULE_RETURN_IF_ERROR(firewall, rule); VIR_FIREWALL_RULE_RETURN_IF_ERROR(firewall, rule);
va_start(list, fmt); va_start(list, fmt);
arg = g_strdup_vprintf(fmt, list);
if (virVasprintf(&arg, fmt, list) < 0) va_end(list);
goto no_memory;
ADD_ARG(rule, arg); ADD_ARG(rule, arg);
va_end(list);
return; return;
no_memory: no_memory:
firewall->err = ENOMEM; firewall->err = ENOMEM;
va_end(list);
} }
......
...@@ -594,8 +594,7 @@ virLogVMessage(virLogSourcePtr source, ...@@ -594,8 +594,7 @@ virLogVMessage(virLogSourcePtr source,
/* /*
* serialize the error message, add level and timestamp * serialize the error message, add level and timestamp
*/ */
if (virVasprintfQuiet(&str, fmt, vargs) < 0) str = g_strdup_vprintf(fmt, vargs);
goto cleanup;
virLogFormatString(&msg, linenr, funcname, priority, str); virLogFormatString(&msg, linenr, funcname, priority, str);
......
...@@ -89,8 +89,8 @@ testEventReport(const char *name, bool failed, const char *msg, ...) ...@@ -89,8 +89,8 @@ testEventReport(const char *name, bool failed, const char *msg, ...)
char *str = NULL; char *str = NULL;
struct testEventResultData data; struct testEventResultData data;
if (msg && virVasprintfQuiet(&str, msg, vargs) != 0) if (msg)
failed = true; str = g_strdup_vprintf(msg, vargs);
data.failed = failed; data.failed = failed;
data.msg = str; data.msg = str;
......
...@@ -186,15 +186,13 @@ qemuMonitorReportError(qemuMonitorTestPtr test, const char *errmsg, ...) ...@@ -186,15 +186,13 @@ qemuMonitorReportError(qemuMonitorTestPtr test, const char *errmsg, ...)
va_start(msgargs, errmsg); va_start(msgargs, errmsg);
if (virVasprintf(&msg, errmsg, msgargs) < 0) msg = g_strdup_vprintf(errmsg, msgargs);
goto cleanup;
jsonmsg = g_strdup_printf("{ \"error\": " " { \"desc\": \"%s\", " jsonmsg = g_strdup_printf("{ \"error\": " " { \"desc\": \"%s\", "
" \"class\": \"UnexpectedCommand\" } }", msg); " \"class\": \"UnexpectedCommand\" } }", msg);
ret = qemuMonitorTestAddResponse(test, jsonmsg); ret = qemuMonitorTestAddResponse(test, jsonmsg);
cleanup:
va_end(msgargs); va_end(msgargs);
return ret; return ret;
} }
......
...@@ -59,10 +59,7 @@ void virHostMsgCheck(const char *prefix, ...@@ -59,10 +59,7 @@ void virHostMsgCheck(const char *prefix,
return; return;
va_start(args, format); va_start(args, format);
if (virVasprintf(&msg, format, args) < 0) { msg = g_strdup_vprintf(format, args);
perror("malloc");
abort();
}
va_end(args); va_end(args);
fprintf(stdout, _("%6s: Checking %-60s: "), prefix, msg); fprintf(stdout, _("%6s: Checking %-60s: "), prefix, msg);
...@@ -120,10 +117,7 @@ void virHostMsgFail(virHostValidateLevel level, ...@@ -120,10 +117,7 @@ void virHostMsgFail(virHostValidateLevel level,
return; return;
va_start(args, format); va_start(args, format);
if (virVasprintf(&msg, format, args) < 0) { msg = g_strdup_vprintf(format, args);
perror("malloc");
abort();
}
va_end(args); va_end(args);
if (virHostMsgWantEscape()) if (virHostMsgWantEscape())
......
...@@ -1847,11 +1847,7 @@ vshDebug(vshControl *ctl, int level, const char *format, ...) ...@@ -1847,11 +1847,7 @@ vshDebug(vshControl *ctl, int level, const char *format, ...)
va_end(ap); va_end(ap);
va_start(ap, format); va_start(ap, format);
if (virVasprintf(&str, format, ap) < 0) { str = g_strdup_vprintf(format, ap);
/* Skip debug messages on low memory */
va_end(ap);
return;
}
va_end(ap); va_end(ap);
fputs(str, stdout); fputs(str, stdout);
VIR_FREE(str); VIR_FREE(str);
...@@ -1867,8 +1863,7 @@ vshPrintExtra(vshControl *ctl, const char *format, ...) ...@@ -1867,8 +1863,7 @@ vshPrintExtra(vshControl *ctl, const char *format, ...)
return; return;
va_start(ap, format); va_start(ap, format);
if (virVasprintfQuiet(&str, format, ap) < 0) str = g_strdup_vprintf(format, ap);
vshErrorOOM();
va_end(ap); va_end(ap);
fputs(str, stdout); fputs(str, stdout);
VIR_FREE(str); VIR_FREE(str);
...@@ -1882,8 +1877,7 @@ vshPrint(vshControl *ctl G_GNUC_UNUSED, const char *format, ...) ...@@ -1882,8 +1877,7 @@ vshPrint(vshControl *ctl G_GNUC_UNUSED, const char *format, ...)
char *str; char *str;
va_start(ap, format); va_start(ap, format);
if (virVasprintfQuiet(&str, format, ap) < 0) str = g_strdup_vprintf(format, ap);
vshErrorOOM();
va_end(ap); va_end(ap);
fputs(str, stdout); fputs(str, stdout);
VIR_FREE(str); VIR_FREE(str);
...@@ -2015,9 +2009,7 @@ vshError(vshControl *ctl, const char *format, ...) ...@@ -2015,9 +2009,7 @@ vshError(vshControl *ctl, const char *format, ...)
fputs(_("error: "), stderr); fputs(_("error: "), stderr);
va_start(ap, format); va_start(ap, format);
/* We can't recursively call vshError on an OOM situation, so ignore str = g_strdup_vprintf(format, ap);
failure here. */
ignore_value(virVasprintf(&str, format, ap));
va_end(ap); va_end(ap);
fprintf(stderr, "%s\n", NULLSTR(str)); fprintf(stderr, "%s\n", NULLSTR(str));
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册