]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
util: Replace open-coded internals of VIR_TYPED_PARAMS_DEBUG with 'virTypedParamDebugstr'
authorPeter Krempa <pkrempa@redhat.com>
Wed, 22 Apr 2026 12:36:22 +0000 (14:36 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Thu, 14 May 2026 10:13:09 +0000 (12:13 +0200)
Replace the internals of the macro by a function so that
'virTypedParameterToString' doesn't need to be exported as it also
adds mappings for values which don't exist in the public API.

This change also prevents a NULL to be passed to string formatters in
case when the caller sends an unknown typed parameter type as we now
also make sure that the type is in range.

Fixes: 54dd75fd97339dd49a54554e9327e5680c72132b
Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
src/libvirt_private.syms
src/util/virtypedparam.c
src/util/virtypedparam.h

index f22b5895db12966737585c8611421ceb24ea703b..d33366fe854963c0c7d7563141dab8fb6b155cf6 100644 (file)
@@ -3654,6 +3654,7 @@ virTPMSwtpmSetupFeatureTypeFromString;
 
 
 # util/virtypedparam.h
+virTypedParamDebugstr;
 virTypedParameterAssign;
 virTypedParameterToString;
 virTypedParameterTypeFromString;
index 6cfdd3276d22a81ec313818cb1ee05a56da633a5..77c9279eff9f8c67fb147a709d287c83eec808d6 100644 (file)
@@ -1109,3 +1109,28 @@ virTypedParamListAddDouble(virTypedParamList *list,
     virTypedParamSetNameVPrintf(list, par, namefmt, ap);
     va_end(ap);
 }
+
+
+/**
+ * virTypedParamDebugstr:
+ * @param: typed parameter
+ *
+ * Format @param into a string used for debug prints in public API handlers.
+ * This must make sure to work on unknown typed parameter types.
+ *
+ * Returns the formatted string; caller must free it.
+ */
+char *
+virTypedParamDebugstr(virTypedParameterPtr param)
+{
+    g_autofree char *value = virTypedParameterToString(param);
+    int type = param->type;
+
+    if (type < 0 || type > VIR_TYPED_PARAM_LAST)
+        type = 0;
+
+    return g_strdup_printf("params[\"%s\"]=(%s)%s",
+                           param->field,
+                           virTypedParameterTypeToString(type),
+                           NULLSTR(value));
+}
index c1fc28c6123835b68500a23719e8fce4a27f8d81..3d6f6b999c3c89483f189048f0baafd9a4767b5d 100644 (file)
@@ -140,18 +140,17 @@ virTypedParamsSerialize(virTypedParameterPtr params,
 
 VIR_ENUM_DECL(virTypedParameter);
 
+char *
+virTypedParamDebugstr(virTypedParameterPtr param);
+
 #define VIR_TYPED_PARAMS_DEBUG(params, nparams) \
     do { \
         int _i; \
         if (!params) \
             break; \
         for (_i = 0; _i < (nparams); _i++) { \
-            char *_value = virTypedParameterToString((params) + _i); \
-            VIR_DEBUG("params[\"%s\"]=(%s)%s", \
-                      (params)[_i].field, \
-                      virTypedParameterTypeToString((params)[_i].type), \
-                      NULLSTR(_value)); \
-            VIR_FREE(_value); \
+            g_autofree char *_debugstr = virTypedParamDebugstr((params) + _i); \
+            VIR_DEBUG("%s", _debugstr); \
         } \
     } while (0)