diff mbox series

[v3,18/17] tests/unit/test-char: Replace g_alloca() by buffer on the stack

Message ID 20210507171948.2062076-1-philmd@redhat.com (mailing list archive)
State New, archived
Headers show
Series misc: Replace alloca() by g_malloc() | expand

Commit Message

Philippe Mathieu-Daudé May 7, 2021, 5:19 p.m. UTC
The ALLOCA(3) man-page mentions its "use is discouraged".

Directly reserve the CharBackend on the stack.

Signed-off-by: Philippe Mathieu-Daudé <philmd@redhat.com>
---
 tests/unit/test-char.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

Comments

Marc-André Lureau May 7, 2021, 8:44 p.m. UTC | #1
Hi

On Fri, May 7, 2021 at 9:22 PM Philippe Mathieu-Daudé <philmd@redhat.com>
wrote:

> The ALLOCA(3) man-page mentions its "use is discouraged".
>
> Directly reserve the CharBackend on the stack.
>
> Signed-off-by: Philippe Mathieu-Daudé <philmd@redhat.com>
> ---
>  tests/unit/test-char.c | 3 +--
>  1 file changed, 1 insertion(+), 2 deletions(-)
>
> diff --git a/tests/unit/test-char.c b/tests/unit/test-char.c
> index 5b3b48ebacd..54ce26226b3 100644
> --- a/tests/unit/test-char.c
> +++ b/tests/unit/test-char.c
> @@ -574,7 +574,7 @@ static void char_udp_test_internal(Chardev *reuse_chr,
> int sock)
>      struct sockaddr_in other;
>      SocketIdleData d = { 0, };
>      Chardev *chr;
> -    CharBackend *be;
> +    CharBackend tmpbe, *be = &tmpbe;
>

Why introduce tmpbe? to avoid some code churn? I would rather update the
code to use be. or &be.

     socklen_t alen = sizeof(other);
>      int ret;
>      char buf[10];
> @@ -590,7 +590,6 @@ static void char_udp_test_internal(Chardev *reuse_chr,
> int sock)
>          chr = qemu_chr_new("client", tmp, NULL);
>          g_assert_nonnull(chr);
>
> -        be = g_alloca(sizeof(CharBackend));
>          qemu_chr_fe_init(be, chr, &error_abort);
>      }
>
> --
> 2.26.3
>
>
>
Richard Henderson May 7, 2021, 9:25 p.m. UTC | #2
On 5/7/21 1:44 PM, Marc-André Lureau wrote:
>     -    CharBackend *be;
>     +    CharBackend tmpbe, *be = &tmpbe;
> 
> 
> Why introduce tmpbe? to avoid some code churn? I would rather update the code 
> to use be. or &be.

There's a branch with "be = reuse_chr->be"


r~
diff mbox series

Patch

diff --git a/tests/unit/test-char.c b/tests/unit/test-char.c
index 5b3b48ebacd..54ce26226b3 100644
--- a/tests/unit/test-char.c
+++ b/tests/unit/test-char.c
@@ -574,7 +574,7 @@  static void char_udp_test_internal(Chardev *reuse_chr, int sock)
     struct sockaddr_in other;
     SocketIdleData d = { 0, };
     Chardev *chr;
-    CharBackend *be;
+    CharBackend tmpbe, *be = &tmpbe;
     socklen_t alen = sizeof(other);
     int ret;
     char buf[10];
@@ -590,7 +590,6 @@  static void char_udp_test_internal(Chardev *reuse_chr, int sock)
         chr = qemu_chr_new("client", tmp, NULL);
         g_assert_nonnull(chr);
 
-        be = g_alloca(sizeof(CharBackend));
         qemu_chr_fe_init(be, chr, &error_abort);
     }