提交 082274ea 编写于 作者: E Eric Blake

qemu: simplify string cleanup

No need to open code a string list cleanup, if we are nice
to the caller by guaranteeing a NULL-terminated result.

* src/qemu/qemu_monitor_json.c (qemuMonitorJSONGetCPUDefinitions)
(qemuMonitorJSONGetCommands, qemuMonitorJSONGetEvents)
(qemuMonitorJSONGetObjectTypes, qemuMonitorJSONGetObjectProps):
Use simpler cleanup.
Signed-off-by: NEric Blake <eblake@redhat.com>
上级 764bb5e5
...@@ -3978,7 +3978,8 @@ int qemuMonitorJSONGetMachines(qemuMonitorPtr mon, ...@@ -3978,7 +3978,8 @@ int qemuMonitorJSONGetMachines(qemuMonitorPtr mon,
goto cleanup; goto cleanup;
} }
if (VIR_ALLOC_N(infolist, n) < 0) { /* null-terminated list */
if (VIR_ALLOC_N(infolist, n + 1) < 0) {
virReportOOMError(); virReportOOMError();
goto cleanup; goto cleanup;
} }
...@@ -4091,7 +4092,8 @@ int qemuMonitorJSONGetCPUDefinitions(qemuMonitorPtr mon, ...@@ -4091,7 +4092,8 @@ int qemuMonitorJSONGetCPUDefinitions(qemuMonitorPtr mon,
goto cleanup; goto cleanup;
} }
if (VIR_ALLOC_N(cpulist, n) < 0) { /* null-terminated list */
if (VIR_ALLOC_N(cpulist, n + 1) < 0) {
virReportOOMError(); virReportOOMError();
goto cleanup; goto cleanup;
} }
...@@ -4116,11 +4118,8 @@ int qemuMonitorJSONGetCPUDefinitions(qemuMonitorPtr mon, ...@@ -4116,11 +4118,8 @@ int qemuMonitorJSONGetCPUDefinitions(qemuMonitorPtr mon,
*cpus = cpulist; *cpus = cpulist;
cleanup: cleanup:
if (ret < 0 && cpulist) { if (ret < 0)
for (i = 0 ; i < n ; i++) virStringFreeList(cpulist);
VIR_FREE(cpulist[i]);
VIR_FREE(cpulist);
}
virJSONValueFree(cmd); virJSONValueFree(cmd);
virJSONValueFree(reply); virJSONValueFree(reply);
return ret; return ret;
...@@ -4165,7 +4164,8 @@ int qemuMonitorJSONGetCommands(qemuMonitorPtr mon, ...@@ -4165,7 +4164,8 @@ int qemuMonitorJSONGetCommands(qemuMonitorPtr mon,
goto cleanup; goto cleanup;
} }
if (VIR_ALLOC_N(commandlist, n) < 0) { /* null-terminated list */
if (VIR_ALLOC_N(commandlist, n + 1) < 0) {
virReportOOMError(); virReportOOMError();
goto cleanup; goto cleanup;
} }
...@@ -4190,11 +4190,8 @@ int qemuMonitorJSONGetCommands(qemuMonitorPtr mon, ...@@ -4190,11 +4190,8 @@ int qemuMonitorJSONGetCommands(qemuMonitorPtr mon,
*commands = commandlist; *commands = commandlist;
cleanup: cleanup:
if (ret < 0 && commandlist) { if (ret < 0)
for (i = 0 ; i < n ; i++) virStringFreeList(commandlist);
VIR_FREE(commandlist[i]);
VIR_FREE(commandlist);
}
virJSONValueFree(cmd); virJSONValueFree(cmd);
virJSONValueFree(reply); virJSONValueFree(reply);
return ret; return ret;
...@@ -4244,7 +4241,8 @@ int qemuMonitorJSONGetEvents(qemuMonitorPtr mon, ...@@ -4244,7 +4241,8 @@ int qemuMonitorJSONGetEvents(qemuMonitorPtr mon,
goto cleanup; goto cleanup;
} }
if (VIR_ALLOC_N(eventlist, n) < 0) { /* null-terminated list */
if (VIR_ALLOC_N(eventlist, n + 1) < 0) {
virReportOOMError(); virReportOOMError();
goto cleanup; goto cleanup;
} }
...@@ -4269,11 +4267,8 @@ int qemuMonitorJSONGetEvents(qemuMonitorPtr mon, ...@@ -4269,11 +4267,8 @@ int qemuMonitorJSONGetEvents(qemuMonitorPtr mon,
*events = eventlist; *events = eventlist;
cleanup: cleanup:
if (ret < 0 && eventlist) { if (ret < 0)
for (i = 0 ; i < n ; i++) virStringFreeList(eventlist);
VIR_FREE(eventlist[i]);
VIR_FREE(eventlist);
}
virJSONValueFree(cmd); virJSONValueFree(cmd);
virJSONValueFree(reply); virJSONValueFree(reply);
return ret; return ret;
...@@ -4369,7 +4364,8 @@ int qemuMonitorJSONGetObjectTypes(qemuMonitorPtr mon, ...@@ -4369,7 +4364,8 @@ int qemuMonitorJSONGetObjectTypes(qemuMonitorPtr mon,
goto cleanup; goto cleanup;
} }
if (VIR_ALLOC_N(typelist, n) < 0) { /* null-terminated list */
if (VIR_ALLOC_N(typelist, n + 1) < 0) {
virReportOOMError(); virReportOOMError();
goto cleanup; goto cleanup;
} }
...@@ -4394,11 +4390,8 @@ int qemuMonitorJSONGetObjectTypes(qemuMonitorPtr mon, ...@@ -4394,11 +4390,8 @@ int qemuMonitorJSONGetObjectTypes(qemuMonitorPtr mon,
*types = typelist; *types = typelist;
cleanup: cleanup:
if (ret < 0 && typelist) { if (ret < 0)
for (i = 0 ; i < n ; i++) virStringFreeList(typelist);
VIR_FREE(typelist[i]);
VIR_FREE(typelist);
}
virJSONValueFree(cmd); virJSONValueFree(cmd);
virJSONValueFree(reply); virJSONValueFree(reply);
return ret; return ret;
...@@ -4451,7 +4444,8 @@ int qemuMonitorJSONGetObjectProps(qemuMonitorPtr mon, ...@@ -4451,7 +4444,8 @@ int qemuMonitorJSONGetObjectProps(qemuMonitorPtr mon,
goto cleanup; goto cleanup;
} }
if (VIR_ALLOC_N(proplist, n) < 0) { /* null-terminated list */
if (VIR_ALLOC_N(proplist, n + 1) < 0) {
virReportOOMError(); virReportOOMError();
goto cleanup; goto cleanup;
} }
...@@ -4476,11 +4470,8 @@ int qemuMonitorJSONGetObjectProps(qemuMonitorPtr mon, ...@@ -4476,11 +4470,8 @@ int qemuMonitorJSONGetObjectProps(qemuMonitorPtr mon,
*props = proplist; *props = proplist;
cleanup: cleanup:
if (ret < 0 && proplist) { if (ret < 0)
for (i = 0 ; i < n ; i++) virStringFreeList(proplist);
VIR_FREE(proplist[i]);
VIR_FREE(proplist);
}
virJSONValueFree(cmd); virJSONValueFree(cmd);
virJSONValueFree(reply); virJSONValueFree(reply);
return ret; return ret;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册