[PATCH v3 14/25] util: virtypedparam: Store errors inside virTypedParamList

Peter Krempa pkrempa at redhat.com
Wed Apr 19 12:04:31 UTC 2023


The only non-abort()-ing error which can happen is if the field name is
too long. Store the overly long name in the virTypedParamList container
so that in upcoming patches the helpers adding to the list can be
refactored to not have a return value.

Signed-off-by: Peter Krempa <pkrempa at redhat.com>
---
 src/util/virtypedparam.c | 62 ++++++++++++++++++++++------------------
 1 file changed, 34 insertions(+), 28 deletions(-)

diff --git a/src/util/virtypedparam.c b/src/util/virtypedparam.c
index 68698fe583..046164d36c 100644
--- a/src/util/virtypedparam.c
+++ b/src/util/virtypedparam.c
@@ -709,6 +709,8 @@ struct _virTypedParamList {
     virTypedParameterPtr par;
     size_t npar;
     size_t par_alloc;
+
+    char *err_name; /* overly long field name for error message */
 };


@@ -740,6 +742,9 @@ virTypedParamListConcat(virTypedParamList *to,
     memcpy(to->par + to->npar, from->par, sizeof(*(to->par)) * from->npar);
     to->npar += from->npar;
     from->npar = 0;
+
+    if (!to->err_name)
+        to->err_name = g_steal_pointer(&from->err_name);
 }


@@ -750,6 +755,7 @@ virTypedParamListFree(virTypedParamList *list)
         return;

     virTypedParamsFree(list->par, list->npar);
+    g_free(list->err_name);
     g_free(list);
 }

@@ -770,6 +776,12 @@ virTypedParamListFetch(virTypedParamList *list,
                        virTypedParameterPtr *par,
                        size_t *npar)
 {
+    if (list->err_name) {
+        virReportError(VIR_ERR_INTERNAL_ERROR, _("Field name '%1$s' too long"),
+                       list->err_name);
+        return -1;
+    }
+
     if (par)
         *par = list->par;

@@ -815,17 +827,18 @@ virTypedParamListFromParams(virTypedParameterPtr *params,
 }


-static int G_GNUC_PRINTF(2, 0)
-virTypedParamSetNameVPrintf(virTypedParameterPtr par,
+static void G_GNUC_PRINTF(3, 0)
+virTypedParamSetNameVPrintf(virTypedParamList *list,
+                            virTypedParameterPtr par,
                             const char *fmt,
                             va_list ap)
 {
-    if (g_vsnprintf(par->field, VIR_TYPED_PARAM_FIELD_LENGTH, fmt, ap) > VIR_TYPED_PARAM_FIELD_LENGTH) {
-        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Field name too long"));
-        return -1;
-    }
+    g_autofree char *name = g_strdup_vprintf(fmt, ap);

-    return 0;
+    if (virStrcpyStatic(par->field, name) < 0) {
+        if (!list->err_name)
+            list->err_name = g_steal_pointer(&name);
+    }
 }


@@ -848,15 +861,14 @@ virTypedParamListAddInt(virTypedParamList *list,
 {
     virTypedParameterPtr par = virTypedParamListExtend(list);
     va_list ap;
-    int ret;

     virTypedParameterAssignValue(par, VIR_TYPED_PARAM_INT, value);

     va_start(ap, namefmt);
-    ret = virTypedParamSetNameVPrintf(par, namefmt, ap);
+    virTypedParamSetNameVPrintf(list, par, namefmt, ap);
     va_end(ap);

-    return ret;
+    return 0;
 }


@@ -868,15 +880,14 @@ virTypedParamListAddUInt(virTypedParamList *list,
 {
     virTypedParameterPtr par = virTypedParamListExtend(list);
     va_list ap;
-    int ret;

     virTypedParameterAssignValue(par, VIR_TYPED_PARAM_UINT, value);

     va_start(ap, namefmt);
-    ret = virTypedParamSetNameVPrintf(par, namefmt, ap);
+    virTypedParamSetNameVPrintf(list, par, namefmt, ap);
     va_end(ap);

-    return ret;
+    return 0;
 }


@@ -888,15 +899,14 @@ virTypedParamListAddLLong(virTypedParamList *list,
 {
     virTypedParameterPtr par = virTypedParamListExtend(list);
     va_list ap;
-    int ret;

     virTypedParameterAssignValue(par, VIR_TYPED_PARAM_LLONG, value);

     va_start(ap, namefmt);
-    ret = virTypedParamSetNameVPrintf(par, namefmt, ap);
+    virTypedParamSetNameVPrintf(list, par, namefmt, ap);
     va_end(ap);

-    return ret;
+    return 0;
 }


@@ -908,15 +918,14 @@ virTypedParamListAddULLong(virTypedParamList *list,
 {
     virTypedParameterPtr par = virTypedParamListExtend(list);
     va_list ap;
-    int ret;

     virTypedParameterAssignValue(par, VIR_TYPED_PARAM_ULLONG, value);

     va_start(ap, namefmt);
-    ret = virTypedParamSetNameVPrintf(par, namefmt, ap);
+    virTypedParamSetNameVPrintf(list, par, namefmt, ap);
     va_end(ap);

-    return ret;
+    return 0;
 }


@@ -928,15 +937,14 @@ virTypedParamListAddString(virTypedParamList *list,
 {
     virTypedParameterPtr par = virTypedParamListExtend(list);
     va_list ap;
-    int ret;

     virTypedParameterAssignValue(par, VIR_TYPED_PARAM_STRING, value);

     va_start(ap, namefmt);
-    ret = virTypedParamSetNameVPrintf(par, namefmt, ap);
+    virTypedParamSetNameVPrintf(list, par, namefmt, ap);
     va_end(ap);

-    return ret;
+    return 0;
 }


@@ -948,15 +956,14 @@ virTypedParamListAddBoolean(virTypedParamList *list,
 {
     virTypedParameterPtr par = virTypedParamListExtend(list);
     va_list ap;
-    int ret;

     virTypedParameterAssignValue(par, VIR_TYPED_PARAM_BOOLEAN, value);

     va_start(ap, namefmt);
-    ret = virTypedParamSetNameVPrintf(par, namefmt, ap);
+    virTypedParamSetNameVPrintf(list, par, namefmt, ap);
     va_end(ap);

-    return ret;
+    return 0;
 }


@@ -968,13 +975,12 @@ virTypedParamListAddDouble(virTypedParamList *list,
 {
     virTypedParameterPtr par = virTypedParamListExtend(list);
     va_list ap;
-    int ret;

     virTypedParameterAssignValue(par, VIR_TYPED_PARAM_DOUBLE, value);

     va_start(ap, namefmt);
-    ret = virTypedParamSetNameVPrintf(par, namefmt, ap);
+    virTypedParamSetNameVPrintf(list, par, namefmt, ap);
     va_end(ap);

-    return ret;
+    return 0;
 }
-- 
2.39.2



More information about the libvir-list mailing list