diff mbox series

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

Message ID 430a9a42f7a34b799ee4d2f3d7ab2e23@tpw09926dag18e.domain1.systemhost.net
State New
Headers show
Series [Qemu-trivial] test: Use g_strndup instead of plain strndup | expand

Commit Message

Tony Nguyen June 7, 2019, 2:07 p.m. UTC
Due to memory management rules. See HACKING.
    
Signed-off-by: Tony Nguyen <tony.nguyen@bt.com>

Comments

Markus Armbruster June 7, 2019, 3:09 p.m. UTC | #1
<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().
diff mbox series

Patch

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;