[PATCH 01/18] qemuMonitorTestAddItemVerbatim: Simplify cleanup

Peter Krempa posted 18 patches 1 year, 7 months ago
[PATCH 01/18] qemuMonitorTestAddItemVerbatim: Simplify cleanup
Posted by Peter Krempa 1 year, 7 months ago
Reformat the JSON string before allocating the test data structure so
that we don't have to free it if the reformatting fails.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
---
 tests/qemumonitortestutils.c | 13 +++++--------
 1 file changed, 5 insertions(+), 8 deletions(-)

diff --git a/tests/qemumonitortestutils.c b/tests/qemumonitortestutils.c
index 4e99c4b54e..cfe78b258a 100644
--- a/tests/qemumonitortestutils.c
+++ b/tests/qemumonitortestutils.c
@@ -673,24 +673,21 @@ qemuMonitorTestAddItemVerbatim(qemuMonitorTest *test,
                                const char *response)
 {
     struct qemuMonitorTestHandlerData *data;
+    char *reformatted = NULL;
+
+    if (!(reformatted = virJSONStringReformat(command, false)))
+        return -1;

     data = g_new0(struct qemuMonitorTestHandlerData, 1);

     data->response = g_strdup(response);
     data->cmderr = g_strdup(cmderr);
-
-    data->command_name = virJSONStringReformat(command, false);
-    if (!data->command_name)
-        goto error;
+    data->command_name = g_steal_pointer(&reformatted);

     return qemuMonitorTestAddHandler(test,
                                      command,
                                      qemuMonitorTestProcessCommandVerbatim,
                                      data, qemuMonitorTestHandlerDataFree);
-
- error:
-    qemuMonitorTestHandlerDataFree(data);
-    return -1;
 }


-- 
2.40.1
Re: [PATCH 01/18] qemuMonitorTestAddItemVerbatim: Simplify cleanup
Posted by Ján Tomko 1 year, 6 months ago
On a Thursday in 2023, Peter Krempa wrote:
>Reformat the JSON string before allocating the test data structure so
>that we don't have to free it if the reformatting fails.
>
>Signed-off-by: Peter Krempa <pkrempa@redhat.com>
>---
> tests/qemumonitortestutils.c | 13 +++++--------
> 1 file changed, 5 insertions(+), 8 deletions(-)
>

Reviewed-by: Ján Tomko <jtomko@redhat.com>

Jano