[PATCH] virTypedParamsValidateTemplate: Fix counting of templates

Peter Krempa via Devel posted 1 patch 2 weeks ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/libvirt tags/patchew/fdac6347c173db75953f5657f4d4b150ac5f9c88.1778918126.git.pkrempa@redhat.com
src/util/virtypedparam.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
[PATCH] virTypedParamsValidateTemplate: Fix counting of templates
Posted by Peter Krempa via Devel 2 weeks ago
From: Peter Krempa <pkrempa@redhat.com>

There are 2 bugs in virTypedParamsValidateTemplate's counting of the
passed amount of templates:
 - the condition looked for empty strings rather than non-empty ones
 - the count was 1 more than the amount of templates due to use of
   post-increment directly in the condition

Fixes: 45617351585caa2c5bcc51af48bd32fd750e7afd
Closes: https://gitlab.com/libvirt/libvirt/-/work_items/880
Signed-off-by: Peter Krempa <pkrempa@redhat.com>
---
 src/util/virtypedparam.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/util/virtypedparam.c b/src/util/virtypedparam.c
index 92f25cea39..bca7478109 100644
--- a/src/util/virtypedparam.c
+++ b/src/util/virtypedparam.c
@@ -213,8 +213,8 @@ virTypedParamsValidateTemplate(virTypedParameterPtr params,

     /* we need to copy the list of templates because
      * 'virTypedParamsValidateInternal' will need to sort it */
-    while (*templates[ntemplates++].name == '\0')
-        ;
+    while (*templates[ntemplates].name != '\0')
+        ntemplates++;

     templ_copy = g_new0(virTypedParamValidationTemplate, ntemplates);
     memcpy(templ_copy, templates, sizeof(*templates) * ntemplates);
-- 
2.54.0
Re: [PATCH] virTypedParamsValidateTemplate: Fix counting of templates
Posted by Jiri Denemark via Devel 1 week, 4 days ago
On Sat, May 16, 2026 at 09:55:26 +0200, Peter Krempa wrote:
> From: Peter Krempa <pkrempa@redhat.com>
> 
> There are 2 bugs in virTypedParamsValidateTemplate's counting of the
> passed amount of templates:
>  - the condition looked for empty strings rather than non-empty ones
>  - the count was 1 more than the amount of templates due to use of
>    post-increment directly in the condition
> 
> Fixes: 45617351585caa2c5bcc51af48bd32fd750e7afd
> Closes: https://gitlab.com/libvirt/libvirt/-/work_items/880
> Signed-off-by: Peter Krempa <pkrempa@redhat.com>
> ---
>  src/util/virtypedparam.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/src/util/virtypedparam.c b/src/util/virtypedparam.c
> index 92f25cea39..bca7478109 100644
> --- a/src/util/virtypedparam.c
> +++ b/src/util/virtypedparam.c
> @@ -213,8 +213,8 @@ virTypedParamsValidateTemplate(virTypedParameterPtr params,
> 
>      /* we need to copy the list of templates because
>       * 'virTypedParamsValidateInternal' will need to sort it */
> -    while (*templates[ntemplates++].name == '\0')
> -        ;
> +    while (*templates[ntemplates].name != '\0')
> +        ntemplates++;
> 
>      templ_copy = g_new0(virTypedParamValidationTemplate, ntemplates);
>      memcpy(templ_copy, templates, sizeof(*templates) * ntemplates);

Reviewed-by: Jiri Denemark <jdenemar@redhat.com>