[Qemu-devel] [Qemu-trivial] [PATCH] test: Use g_strndup instead of plain strndup

tony.nguyen@bt.com posted 1 patch 4 years, 11 months ago
Test s390x passed
Test checkpatch passed
Test asan passed
Test docker-mingw@fedora passed
Test docker-clang@ubuntu passed
Test FreeBSD passed
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/430a9a42f7a34b799ee4d2f3d7ab2e23@tpw09926dag18e.domain1.systemhost.net
Maintainers: Markus Armbruster <armbru@redhat.com>, Laurent Vivier <laurent@vivier.eu>, Michael Tokarev <mjt@tls.msk.ru>
There is a newer version of this series
[Qemu-devel] [Qemu-trivial] [PATCH] test: Use g_strndup instead of plain strndup
Posted by tony.nguyen@bt.com 4 years, 11 months ago
Due to memory management rules. See HACKING.
    
Signed-off-by: Tony Nguyen <tony.nguyen@bt.com>

diff --git a/tests/check-qjson.c b/tests/check-qjson.c
index fa2afcc..07a773e 100644
--- a/tests/check-qjson.c
+++ b/tests/check-qjson.c
@@ -767,7 +767,7 @@ static void utf8_string(void)
                     if (*end == ' ') {
                         end++;
                     }
-                    in = strndup(tail, end - tail);
+                    in = g_strndup(tail, end - tail);
                     str = from_json_str(in, j, NULL);
                     g_assert(!str);
                     g_free(in);
diff --git a/tests/migration/stress.c b/tests/migration/stress.c
index 49a03aa..72c4012 100644
--- a/tests/migration/stress.c
+++ b/tests/migration/stress.c
@@ -104,7 +104,7 @@ static int get_command_arg_str(const char *name,
     }
 
     if (end)
-        *val = strndup(start, end - start);
+        *val = g_strndup(start, end - start);
     else
         *val = strdup(start);
     return 1;
Re: [Qemu-devel] [Qemu-trivial] [PATCH] test: Use g_strndup instead of plain strndup
Posted by Markus Armbruster 4 years, 11 months ago
<tony.nguyen@bt.com> writes:

> Due to memory management rules. See HACKING.
>     
> Signed-off-by: Tony Nguyen <tony.nguyen@bt.com>
>
> diff --git a/tests/check-qjson.c b/tests/check-qjson.c
> index fa2afcc..07a773e 100644
> --- a/tests/check-qjson.c
> +++ b/tests/check-qjson.c
> @@ -767,7 +767,7 @@ static void utf8_string(void)
>                      if (*end == ' ') {
>                          end++;
>                      }
> -                    in = strndup(tail, end - tail);
> +                    in = g_strndup(tail, end - tail);
>                      str = from_json_str(in, j, NULL);
>                      g_assert(!str);
>                      g_free(in);

This one is fine.

> diff --git a/tests/migration/stress.c b/tests/migration/stress.c
> index 49a03aa..72c4012 100644
> --- a/tests/migration/stress.c
> +++ b/tests/migration/stress.c
> @@ -104,7 +104,7 @@ static int get_command_arg_str(const char *name,
>      }
>  
>      if (end)
> -        *val = strndup(start, end - start);
> +        *val = g_strndup(start, end - start);
>      else
>          *val = strdup(start);
>      return 1;

This one isn't.  The caller passes *val to free().  You have to change
*both* allocations in get_command_arg_str() *and* the deallocation in
get_command_arg_ull().