All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v3 00/17] misc: Replace alloca() by g_malloc()
@ 2021-05-07 14:42 Philippe Mathieu-Daudé
  2021-05-07 14:42 ` [PATCH v3 01/17] bsd-user/syscall: Replace alloca() by g_try_new() Philippe Mathieu-Daudé
                   ` (17 more replies)
  0 siblings, 18 replies; 27+ messages in thread
From: Philippe Mathieu-Daudé @ 2021-05-07 14:42 UTC (permalink / raw)
  To: qemu-devel
  Cc: Peter Maydell, Philippe Mathieu-Daudé,
	Laurent Vivier, qemu-arm, qemu-ppc, Gerd Hoffmann, Paolo Bonzini,
	Alex Bennée

The ALLOCA(3) man-page mentions its "use is discouraged".
Replace few calls by equivalent GLib malloc().

Since v2:
- linux-user calls converted
- bsd-user returns ENOMEM (Peter)
- built on PPC+KVM host (David)
- removed tpm mutex cleanup (Christophe)
- included Alex gdbstub patch
- added R-b tags

Since v1:
- Converted more uses (alsaaudio, tpm, pca9552)
- Reworked gdbstub (Alex)
- Simplified PPC/KVM (Greg)

Alex Bennée (1):
  gdbstub: Replace GdbCmdContext with plain g_array()

Philippe Mathieu-Daudé (16):
  bsd-user/syscall: Replace alloca() by g_try_new()
  linux-user/elfload: Replace alloca() by g_try_malloc()
  linux-user/syscall: Replace alloca() by g_try_new()
  linux-user/syscall: Replace alloca() by g_try_malloc()
  linux-user: Replace alloca() by g_try_new() in ppoll() syscall
  linux-user: Replace alloca() by g_try_malloc() in setsockopt() syscall
  linux-user: Replace alloca() by g_try_malloc() in various socket
    syscall
  linux-user/syscall: Move code around in do_sendrecvmsg_locked()
  linux-user/syscall: Replace alloca() by GLib alloc() in sendrecvmsg
  audio/alsaaudio: Replace ALSA alloca() by malloc() equivalent
  backends/tpm: Replace g_alloca() by g_malloc()
  gdbstub: Constify GdbCmdParseEntry
  hw/misc/pca9552: Replace g_newa() by g_new()
  target/ppc/kvm: Replace alloca() by g_malloc()
  configure: Prohibit alloca() by using -Walloca CPPFLAG
  configure: libSLiRP buildsys kludge

 configure                   |   8 +
 audio/alsaaudio.c           |  11 +-
 backends/tpm/tpm_emulator.c |   3 +-
 bsd-user/syscall.c          |   7 +-
 gdbstub.c                   | 322 +++++++++++++++++-------------------
 hw/misc/pca9552.c           |   2 +-
 linux-user/elfload.c        |  14 +-
 linux-user/syscall.c        | 137 ++++++++++-----
 target/ppc/kvm.c            |   4 +-
 9 files changed, 277 insertions(+), 231 deletions(-)

-- 
2.26.3




^ permalink raw reply	[flat|nested] 27+ messages in thread

* [PATCH v3 01/17] bsd-user/syscall: Replace alloca() by g_try_new()
  2021-05-07 14:42 [PATCH v3 00/17] misc: Replace alloca() by g_malloc() Philippe Mathieu-Daudé
@ 2021-05-07 14:42 ` Philippe Mathieu-Daudé
  2021-05-07 14:43 ` [PATCH v3 02/17] linux-user/elfload: Replace alloca() by g_try_malloc() Philippe Mathieu-Daudé
                   ` (16 subsequent siblings)
  17 siblings, 0 replies; 27+ messages in thread
From: Philippe Mathieu-Daudé @ 2021-05-07 14:42 UTC (permalink / raw)
  To: qemu-devel
  Cc: Peter Maydell, Philippe Mathieu-Daudé,
	Laurent Vivier, qemu-arm, qemu-ppc, Gerd Hoffmann, Kyle Evans,
	Paolo Bonzini, Alex Bennée, Warner Losh

The ALLOCA(3) man-page mentions its "use is discouraged".

Use autofree heap allocation instead (returning ENOMEM on failure).

Suggested-by: Peter Maydell <peter.maydell@linaro.org>
Signed-off-by: Philippe Mathieu-Daudé <philmd@redhat.com>
---
 bsd-user/syscall.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/bsd-user/syscall.c b/bsd-user/syscall.c
index 4abff796c76..6db05988f1b 100644
--- a/bsd-user/syscall.c
+++ b/bsd-user/syscall.c
@@ -355,9 +355,12 @@ abi_long do_freebsd_syscall(void *cpu_env, int num, abi_long arg1,
     case TARGET_FREEBSD_NR_writev:
         {
             int count = arg3;
-            struct iovec *vec;
+            g_autofree struct iovec *vec = g_try_new(struct iovec, count);
 
-            vec = alloca(count * sizeof(struct iovec));
+            if (!vec) {
+                ret = -TARGET_ENOMEM;
+                goto fail;
+            }
             if (lock_iovec(VERIFY_READ, vec, arg2, count, 1) < 0)
                 goto efault;
             ret = get_errno(writev(arg1, vec, count));
-- 
2.26.3



^ permalink raw reply related	[flat|nested] 27+ messages in thread

* [PATCH v3 02/17] linux-user/elfload: Replace alloca() by g_try_malloc()
  2021-05-07 14:42 [PATCH v3 00/17] misc: Replace alloca() by g_malloc() Philippe Mathieu-Daudé
  2021-05-07 14:42 ` [PATCH v3 01/17] bsd-user/syscall: Replace alloca() by g_try_new() Philippe Mathieu-Daudé
@ 2021-05-07 14:43 ` Philippe Mathieu-Daudé
  2021-05-07 14:43 ` [PATCH v3 03/17] linux-user/syscall: Replace alloca() by g_try_new() Philippe Mathieu-Daudé
                   ` (15 subsequent siblings)
  17 siblings, 0 replies; 27+ messages in thread
From: Philippe Mathieu-Daudé @ 2021-05-07 14:43 UTC (permalink / raw)
  To: qemu-devel
  Cc: Peter Maydell, Philippe Mathieu-Daudé,
	Laurent Vivier, qemu-arm, qemu-ppc, Gerd Hoffmann, Paolo Bonzini,
	Alex Bennée

The ALLOCA(3) man-page mentions its "use is discouraged".

Use autofree heap allocation instead (returning ENOMEM on failure).

Suggested-by: Peter Maydell <peter.maydell@linaro.org>
Signed-off-by: Philippe Mathieu-Daudé <philmd@redhat.com>
---
 linux-user/elfload.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)

diff --git a/linux-user/elfload.c b/linux-user/elfload.c
index c6731013fde..dad2dac24a4 100644
--- a/linux-user/elfload.c
+++ b/linux-user/elfload.c
@@ -2591,6 +2591,7 @@ static void load_elf_image(const char *image_name, int image_fd,
 {
     struct elfhdr *ehdr = (struct elfhdr *)bprm_buf;
     struct elf_phdr *phdr;
+    g_autofree void *phdr_alloc = NULL;
     abi_ulong load_addr, load_bias, loaddr, hiaddr, error;
     int i, retval, prot_exec;
     Error *err = NULL;
@@ -2610,7 +2611,12 @@ static void load_elf_image(const char *image_name, int image_fd,
     if (ehdr->e_phoff + i <= BPRM_BUF_SIZE) {
         phdr = (struct elf_phdr *)(bprm_buf + ehdr->e_phoff);
     } else {
-        phdr = (struct elf_phdr *) alloca(i);
+        phdr_alloc = g_try_malloc(i);
+        if (!phdr_alloc) {
+            error_setg(&err, "Not enough memory to load ELF program header");
+            goto exit_errmsg;
+        }
+        phdr = (struct elf_phdr *) phdr_alloc;
         retval = pread(image_fd, phdr, i, ehdr->e_phoff);
         if (retval != i) {
             goto exit_read;
@@ -2979,15 +2985,15 @@ static void load_symbols(struct elfhdr *hdr, int fd, abi_ulong load_bias)
 {
     int i, shnum, nsyms, sym_idx = 0, str_idx = 0;
     uint64_t segsz;
-    struct elf_shdr *shdr;
+    g_autofree struct elf_shdr *shdr;
     char *strings = NULL;
     struct syminfo *s = NULL;
     struct elf_sym *new_syms, *syms = NULL;
 
     shnum = hdr->e_shnum;
     i = shnum * sizeof(struct elf_shdr);
-    shdr = (struct elf_shdr *)alloca(i);
-    if (pread(fd, shdr, i, hdr->e_shoff) != i) {
+    shdr = (struct elf_shdr *)g_try_malloc(i);
+    if (shdr == NULL || pread(fd, shdr, i, hdr->e_shoff) != i) {
         return;
     }
 
-- 
2.26.3



^ permalink raw reply related	[flat|nested] 27+ messages in thread

* [PATCH v3 03/17] linux-user/syscall: Replace alloca() by g_try_new()
  2021-05-07 14:42 [PATCH v3 00/17] misc: Replace alloca() by g_malloc() Philippe Mathieu-Daudé
  2021-05-07 14:42 ` [PATCH v3 01/17] bsd-user/syscall: Replace alloca() by g_try_new() Philippe Mathieu-Daudé
  2021-05-07 14:43 ` [PATCH v3 02/17] linux-user/elfload: Replace alloca() by g_try_malloc() Philippe Mathieu-Daudé
@ 2021-05-07 14:43 ` Philippe Mathieu-Daudé
  2021-05-07 14:43 ` [PATCH v3 04/17] linux-user/syscall: Replace alloca() by g_try_malloc() Philippe Mathieu-Daudé
                   ` (14 subsequent siblings)
  17 siblings, 0 replies; 27+ messages in thread
From: Philippe Mathieu-Daudé @ 2021-05-07 14:43 UTC (permalink / raw)
  To: qemu-devel
  Cc: Peter Maydell, Philippe Mathieu-Daudé,
	Laurent Vivier, qemu-arm, qemu-ppc, Gerd Hoffmann, Paolo Bonzini,
	Alex Bennée

The ALLOCA(3) man-page mentions its "use is discouraged".

Use autofree heap allocation instead (returning ENOMEM on failure).

Suggested-by: Peter Maydell <peter.maydell@linaro.org>
Signed-off-by: Philippe Mathieu-Daudé <philmd@redhat.com>
---
 linux-user/syscall.c | 25 +++++++++++++++++--------
 1 file changed, 17 insertions(+), 8 deletions(-)

diff --git a/linux-user/syscall.c b/linux-user/syscall.c
index 95d79ddc437..08ab4cee805 100644
--- a/linux-user/syscall.c
+++ b/linux-user/syscall.c
@@ -11417,10 +11417,12 @@ static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1,
         {
             int gidsetsize = arg1;
             target_id *target_grouplist;
-            gid_t *grouplist;
+            g_autofree gid_t *grouplist = g_try_new(gid_t, gidsetsize);
             int i;
 
-            grouplist = alloca(gidsetsize * sizeof(gid_t));
+            if (!grouplist) {
+                return -TARGET_ENOMEM;
+            }
             ret = get_errno(getgroups(gidsetsize, grouplist));
             if (gidsetsize == 0)
                 return ret;
@@ -11438,10 +11440,13 @@ static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1,
         {
             int gidsetsize = arg1;
             target_id *target_grouplist;
-            gid_t *grouplist = NULL;
+            g_autofree gid_t *grouplist = NULL;
             int i;
             if (gidsetsize) {
-                grouplist = alloca(gidsetsize * sizeof(gid_t));
+                grouplist = g_try_new(gid_t, gidsetsize);
+                if (!grouplist) {
+                    return -TARGET_ENOMEM;
+                }
                 target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * sizeof(target_id), 1);
                 if (!target_grouplist) {
                     return -TARGET_EFAULT;
@@ -11736,10 +11741,12 @@ static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1,
         {
             int gidsetsize = arg1;
             uint32_t *target_grouplist;
-            gid_t *grouplist;
+            g_autofree gid_t *grouplist = g_try_new(gid_t, gidsetsize);
             int i;
 
-            grouplist = alloca(gidsetsize * sizeof(gid_t));
+            if (!grouplist) {
+                return -TARGET_ENOMEM;
+            }
             ret = get_errno(getgroups(gidsetsize, grouplist));
             if (gidsetsize == 0)
                 return ret;
@@ -11760,10 +11767,12 @@ static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1,
         {
             int gidsetsize = arg1;
             uint32_t *target_grouplist;
-            gid_t *grouplist;
+            g_autofree gid_t *grouplist = g_try_new(gid_t, gidsetsize);
             int i;
 
-            grouplist = alloca(gidsetsize * sizeof(gid_t));
+            if (!grouplist) {
+                return -TARGET_ENOMEM;
+            }
             target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 4, 1);
             if (!target_grouplist) {
                 return -TARGET_EFAULT;
-- 
2.26.3



^ permalink raw reply related	[flat|nested] 27+ messages in thread

* [PATCH v3 04/17] linux-user/syscall: Replace alloca() by g_try_malloc()
  2021-05-07 14:42 [PATCH v3 00/17] misc: Replace alloca() by g_malloc() Philippe Mathieu-Daudé
                   ` (2 preceding siblings ...)
  2021-05-07 14:43 ` [PATCH v3 03/17] linux-user/syscall: Replace alloca() by g_try_new() Philippe Mathieu-Daudé
@ 2021-05-07 14:43 ` Philippe Mathieu-Daudé
  2021-05-07 14:43 ` [PATCH v3 05/17] linux-user: Replace alloca() by g_try_new() in ppoll() syscall Philippe Mathieu-Daudé
                   ` (13 subsequent siblings)
  17 siblings, 0 replies; 27+ messages in thread
From: Philippe Mathieu-Daudé @ 2021-05-07 14:43 UTC (permalink / raw)
  To: qemu-devel
  Cc: Peter Maydell, Philippe Mathieu-Daudé,
	Laurent Vivier, qemu-arm, qemu-ppc, Gerd Hoffmann, Paolo Bonzini,
	Alex Bennée

The ALLOCA(3) man-page mentions its "use is discouraged".

Use autofree heap allocation instead (returning ENOMEM on failure).

Suggested-by: Peter Maydell <peter.maydell@linaro.org>
Signed-off-by: Philippe Mathieu-Daudé <philmd@redhat.com>
---
 linux-user/syscall.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)

diff --git a/linux-user/syscall.c b/linux-user/syscall.c
index 08ab4cee805..2fa6b89b3de 100644
--- a/linux-user/syscall.c
+++ b/linux-user/syscall.c
@@ -10630,7 +10630,7 @@ static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1,
     case TARGET_NR_sched_getaffinity:
         {
             unsigned int mask_size;
-            unsigned long *mask;
+            g_autofree unsigned long *mask = NULL;
 
             /*
              * sched_getaffinity needs multiples of ulong, so need to take
@@ -10641,8 +10641,10 @@ static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1,
             }
             mask_size = (arg2 + (sizeof(*mask) - 1)) & ~(sizeof(*mask) - 1);
 
-            mask = alloca(mask_size);
-            memset(mask, 0, mask_size);
+            mask = g_try_malloc0(mask_size);
+            if (!mask) {
+                return -TARGET_ENOMEM;
+            }
             ret = get_errno(sys_sched_getaffinity(arg1, mask_size, mask));
 
             if (!is_error(ret)) {
@@ -10670,7 +10672,7 @@ static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1,
     case TARGET_NR_sched_setaffinity:
         {
             unsigned int mask_size;
-            unsigned long *mask;
+            g_autofree unsigned long *mask = NULL;
 
             /*
              * sched_setaffinity needs multiples of ulong, so need to take
@@ -10680,7 +10682,10 @@ static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1,
                 return -TARGET_EINVAL;
             }
             mask_size = (arg2 + (sizeof(*mask) - 1)) & ~(sizeof(*mask) - 1);
-            mask = alloca(mask_size);
+            mask = g_try_malloc(mask_size);
+            if (!mask) {
+                return -TARGET_ENOMEM;
+            }
 
             ret = target_to_host_cpu_mask(mask, mask_size, arg3, arg2);
             if (ret) {
-- 
2.26.3



^ permalink raw reply related	[flat|nested] 27+ messages in thread

* [PATCH v3 05/17] linux-user: Replace alloca() by g_try_new() in ppoll() syscall
  2021-05-07 14:42 [PATCH v3 00/17] misc: Replace alloca() by g_malloc() Philippe Mathieu-Daudé
                   ` (3 preceding siblings ...)
  2021-05-07 14:43 ` [PATCH v3 04/17] linux-user/syscall: Replace alloca() by g_try_malloc() Philippe Mathieu-Daudé
@ 2021-05-07 14:43 ` Philippe Mathieu-Daudé
  2021-05-07 14:43 ` [PATCH v3 06/17] linux-user: Replace alloca() by g_try_malloc() in setsockopt() syscall Philippe Mathieu-Daudé
                   ` (12 subsequent siblings)
  17 siblings, 0 replies; 27+ messages in thread
From: Philippe Mathieu-Daudé @ 2021-05-07 14:43 UTC (permalink / raw)
  To: qemu-devel
  Cc: Peter Maydell, Philippe Mathieu-Daudé,
	Laurent Vivier, qemu-arm, qemu-ppc, Gerd Hoffmann, Paolo Bonzini,
	Alex Bennée

The ALLOCA(3) man-page mentions its "use is discouraged".

Use autofree heap allocation instead (returning ENOMEM on failure).

Suggested-by: Peter Maydell <peter.maydell@linaro.org>
Signed-off-by: Philippe Mathieu-Daudé <philmd@redhat.com>
---
 linux-user/syscall.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/linux-user/syscall.c b/linux-user/syscall.c
index 2fa6b89b3de..0bf4273fc7a 100644
--- a/linux-user/syscall.c
+++ b/linux-user/syscall.c
@@ -1605,11 +1605,10 @@ static abi_long do_ppoll(abi_long arg1, abi_long arg2, abi_long arg3,
 {
     struct target_pollfd *target_pfd;
     unsigned int nfds = arg2;
-    struct pollfd *pfd;
+    g_autofree struct pollfd *pfd = NULL;
     unsigned int i;
     abi_long ret;
 
-    pfd = NULL;
     target_pfd = NULL;
     if (nfds) {
         if (nfds > (INT_MAX / sizeof(struct target_pollfd))) {
@@ -1621,7 +1620,10 @@ static abi_long do_ppoll(abi_long arg1, abi_long arg2, abi_long arg3,
             return -TARGET_EFAULT;
         }
 
-        pfd = alloca(sizeof(struct pollfd) * nfds);
+        pfd = g_try_new(struct pollfd, nfds);
+        if (!pfd) {
+            return -TARGET_ENOMEM;
+        }
         for (i = 0; i < nfds; i++) {
             pfd[i].fd = tswap32(target_pfd[i].fd);
             pfd[i].events = tswap16(target_pfd[i].events);
-- 
2.26.3



^ permalink raw reply related	[flat|nested] 27+ messages in thread

* [PATCH v3 06/17] linux-user: Replace alloca() by g_try_malloc() in setsockopt() syscall
  2021-05-07 14:42 [PATCH v3 00/17] misc: Replace alloca() by g_malloc() Philippe Mathieu-Daudé
                   ` (4 preceding siblings ...)
  2021-05-07 14:43 ` [PATCH v3 05/17] linux-user: Replace alloca() by g_try_new() in ppoll() syscall Philippe Mathieu-Daudé
@ 2021-05-07 14:43 ` Philippe Mathieu-Daudé
  2021-05-07 14:43 ` [PATCH v3 07/17] linux-user: Replace alloca() by g_try_malloc() in various socket syscall Philippe Mathieu-Daudé
                   ` (11 subsequent siblings)
  17 siblings, 0 replies; 27+ messages in thread
From: Philippe Mathieu-Daudé @ 2021-05-07 14:43 UTC (permalink / raw)
  To: qemu-devel
  Cc: Peter Maydell, Philippe Mathieu-Daudé,
	Laurent Vivier, qemu-arm, qemu-ppc, Gerd Hoffmann, Paolo Bonzini,
	Alex Bennée

The ALLOCA(3) man-page mentions its "use is discouraged".

Use autofree heap allocation instead (returning ENOMEM on failure).

Suggested-by: Peter Maydell <peter.maydell@linaro.org>
Signed-off-by: Philippe Mathieu-Daudé <philmd@redhat.com>
---
 linux-user/syscall.c | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)

diff --git a/linux-user/syscall.c b/linux-user/syscall.c
index 0bf4273fc7a..a263aea85ff 100644
--- a/linux-user/syscall.c
+++ b/linux-user/syscall.c
@@ -2191,7 +2191,6 @@ static abi_long do_setsockopt(int sockfd, int level, int optname,
 {
     abi_long ret;
     int val;
-    struct ip_mreqn *ip_mreq;
     struct ip_mreq_source *ip_mreq_source;
 
     switch(level) {
@@ -2235,15 +2234,21 @@ static abi_long do_setsockopt(int sockfd, int level, int optname,
             break;
         case IP_ADD_MEMBERSHIP:
         case IP_DROP_MEMBERSHIP:
+        {
+            g_autofree struct ip_mreqn *ip_mreq = NULL;
+
             if (optlen < sizeof (struct target_ip_mreq) ||
                 optlen > sizeof (struct target_ip_mreqn))
                 return -TARGET_EINVAL;
 
-            ip_mreq = (struct ip_mreqn *) alloca(optlen);
+            ip_mreq = g_try_malloc(optlen);
+            if (!ip_mreq) {
+                return -TARGET_ENOMEM;
+            }
             target_to_host_ip_mreq(ip_mreq, optval_addr, optlen);
             ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq, optlen));
             break;
-
+        }
         case IP_BLOCK_SOURCE:
         case IP_UNBLOCK_SOURCE:
         case IP_ADD_SOURCE_MEMBERSHIP:
@@ -2492,7 +2497,8 @@ set_timeout:
         }
 	case TARGET_SO_BINDTODEVICE:
 	{
-		char *dev_ifname, *addr_ifname;
+                char *dev_ifname;
+                g_autofree char *addr_ifname = NULL;
 
 		if (optlen > IFNAMSIZ - 1) {
 		    optlen = IFNAMSIZ - 1;
@@ -2502,7 +2508,10 @@ set_timeout:
 		    return -TARGET_EFAULT;
 		}
 		optname = SO_BINDTODEVICE;
-		addr_ifname = alloca(IFNAMSIZ);
+                addr_ifname = g_try_malloc(IFNAMSIZ);
+                if (!addr_ifname) {
+                    return -TARGET_ENOMEM;
+                }
 		memcpy(addr_ifname, dev_ifname, optlen);
 		addr_ifname[optlen] = 0;
 		ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname,
-- 
2.26.3



^ permalink raw reply related	[flat|nested] 27+ messages in thread

* [PATCH v3 07/17] linux-user: Replace alloca() by g_try_malloc() in various socket syscall
  2021-05-07 14:42 [PATCH v3 00/17] misc: Replace alloca() by g_malloc() Philippe Mathieu-Daudé
                   ` (5 preceding siblings ...)
  2021-05-07 14:43 ` [PATCH v3 06/17] linux-user: Replace alloca() by g_try_malloc() in setsockopt() syscall Philippe Mathieu-Daudé
@ 2021-05-07 14:43 ` Philippe Mathieu-Daudé
  2021-05-07 14:43 ` [PATCH v3 08/17] linux-user/syscall: Move code around in do_sendrecvmsg_locked() Philippe Mathieu-Daudé
                   ` (10 subsequent siblings)
  17 siblings, 0 replies; 27+ messages in thread
From: Philippe Mathieu-Daudé @ 2021-05-07 14:43 UTC (permalink / raw)
  To: qemu-devel
  Cc: Peter Maydell, Philippe Mathieu-Daudé,
	Laurent Vivier, qemu-arm, qemu-ppc, Gerd Hoffmann, Paolo Bonzini,
	Alex Bennée

The ALLOCA(3) man-page mentions its "use is discouraged".

Use autofree heap allocation instead (returning ENOMEM on failure).

Suggested-by: Peter Maydell <peter.maydell@linaro.org>
Signed-off-by: Philippe Mathieu-Daudé <philmd@redhat.com>
---
 linux-user/syscall.c | 50 +++++++++++++++++++++++++++++++-------------
 1 file changed, 35 insertions(+), 15 deletions(-)

diff --git a/linux-user/syscall.c b/linux-user/syscall.c
index a263aea85ff..7c5c821f48d 100644
--- a/linux-user/syscall.c
+++ b/linux-user/syscall.c
@@ -3307,14 +3307,17 @@ static abi_long do_socket(int domain, int type, int protocol)
 static abi_long do_bind(int sockfd, abi_ulong target_addr,
                         socklen_t addrlen)
 {
-    void *addr;
+    g_autofree void *addr = NULL;
     abi_long ret;
 
     if ((int)addrlen < 0) {
         return -TARGET_EINVAL;
     }
 
-    addr = alloca(addrlen+1);
+    addr = g_try_malloc(addrlen + 1);
+    if (!addr) {
+        return -TARGET_ENOMEM;
+    }
 
     ret = target_to_host_sockaddr(sockfd, addr, target_addr, addrlen);
     if (ret)
@@ -3327,14 +3330,17 @@ static abi_long do_bind(int sockfd, abi_ulong target_addr,
 static abi_long do_connect(int sockfd, abi_ulong target_addr,
                            socklen_t addrlen)
 {
-    void *addr;
+    g_autofree void *addr = NULL;
     abi_long ret;
 
     if ((int)addrlen < 0) {
         return -TARGET_EINVAL;
     }
 
-    addr = alloca(addrlen+1);
+    addr = g_try_malloc(addrlen + 1);
+    if (!addr) {
+        return -TARGET_ENOMEM;
+    }
 
     ret = target_to_host_sockaddr(sockfd, addr, target_addr, addrlen);
     if (ret)
@@ -3519,7 +3525,7 @@ static abi_long do_accept4(int fd, abi_ulong target_addr,
                            abi_ulong target_addrlen_addr, int flags)
 {
     socklen_t addrlen, ret_addrlen;
-    void *addr;
+    g_autofree void *addr = NULL;
     abi_long ret;
     int host_flags;
 
@@ -3541,7 +3547,10 @@ static abi_long do_accept4(int fd, abi_ulong target_addr,
         return -TARGET_EFAULT;
     }
 
-    addr = alloca(addrlen);
+    addr = g_try_malloc(addrlen);
+    if (!addr) {
+        return -TARGET_ENOMEM;
+    }
 
     ret_addrlen = addrlen;
     ret = get_errno(safe_accept4(fd, addr, &ret_addrlen, host_flags));
@@ -3559,7 +3568,7 @@ static abi_long do_getpeername(int fd, abi_ulong target_addr,
                                abi_ulong target_addrlen_addr)
 {
     socklen_t addrlen, ret_addrlen;
-    void *addr;
+    g_autofree void *addr = NULL;
     abi_long ret;
 
     if (get_user_u32(addrlen, target_addrlen_addr))
@@ -3573,7 +3582,10 @@ static abi_long do_getpeername(int fd, abi_ulong target_addr,
         return -TARGET_EFAULT;
     }
 
-    addr = alloca(addrlen);
+    addr = g_try_malloc(addrlen);
+    if (!addr) {
+        return -TARGET_ENOMEM;
+    }
 
     ret_addrlen = addrlen;
     ret = get_errno(getpeername(fd, addr, &ret_addrlen));
@@ -3591,7 +3603,7 @@ static abi_long do_getsockname(int fd, abi_ulong target_addr,
                                abi_ulong target_addrlen_addr)
 {
     socklen_t addrlen, ret_addrlen;
-    void *addr;
+    g_autofree void *addr = NULL;
     abi_long ret;
 
     if (get_user_u32(addrlen, target_addrlen_addr))
@@ -3605,7 +3617,10 @@ static abi_long do_getsockname(int fd, abi_ulong target_addr,
         return -TARGET_EFAULT;
     }
 
-    addr = alloca(addrlen);
+    addr = g_try_malloc(addrlen);
+    if (!addr) {
+        return -TARGET_ENOMEM;
+    }
 
     ret_addrlen = addrlen;
     ret = get_errno(getsockname(fd, addr, &ret_addrlen));
@@ -3640,7 +3655,6 @@ static abi_long do_socketpair(int domain, int type, int protocol,
 static abi_long do_sendto(int fd, abi_ulong msg, size_t len, int flags,
                           abi_ulong target_addr, socklen_t addrlen)
 {
-    void *addr;
     void *host_msg;
     void *copy_msg = NULL;
     abi_long ret;
@@ -3662,7 +3676,11 @@ static abi_long do_sendto(int fd, abi_ulong msg, size_t len, int flags,
         }
     }
     if (target_addr) {
-        addr = alloca(addrlen+1);
+        g_autofree void *addr = g_try_malloc(addrlen + 1);
+
+        if (!addr) {
+            return -TARGET_ENOMEM;
+        }
         ret = target_to_host_sockaddr(fd, addr, target_addr, addrlen);
         if (ret) {
             goto fail;
@@ -3686,7 +3704,7 @@ static abi_long do_recvfrom(int fd, abi_ulong msg, size_t len, int flags,
                             abi_ulong target_addrlen)
 {
     socklen_t addrlen, ret_addrlen;
-    void *addr;
+    g_autofree void *addr = NULL;
     void *host_msg;
     abi_long ret;
 
@@ -3707,12 +3725,14 @@ static abi_long do_recvfrom(int fd, abi_ulong msg, size_t len, int flags,
             ret = -TARGET_EINVAL;
             goto fail;
         }
-        addr = alloca(addrlen);
+        addr = g_try_malloc(addrlen);
+        if (!addr) {
+            return -TARGET_ENOMEM;
+        }
         ret_addrlen = addrlen;
         ret = get_errno(safe_recvfrom(fd, host_msg, len, flags,
                                       addr, &ret_addrlen));
     } else {
-        addr = NULL; /* To keep compiler quiet.  */
         addrlen = 0; /* To keep compiler quiet.  */
         ret = get_errno(safe_recvfrom(fd, host_msg, len, flags, NULL, 0));
     }
-- 
2.26.3



^ permalink raw reply related	[flat|nested] 27+ messages in thread

* [PATCH v3 08/17] linux-user/syscall: Move code around in do_sendrecvmsg_locked()
  2021-05-07 14:42 [PATCH v3 00/17] misc: Replace alloca() by g_malloc() Philippe Mathieu-Daudé
                   ` (6 preceding siblings ...)
  2021-05-07 14:43 ` [PATCH v3 07/17] linux-user: Replace alloca() by g_try_malloc() in various socket syscall Philippe Mathieu-Daudé
@ 2021-05-07 14:43 ` Philippe Mathieu-Daudé
  2021-05-07 14:43 ` [PATCH v3 09/17] linux-user/syscall: Replace alloca() by GLib alloc() in sendrecvmsg Philippe Mathieu-Daudé
                   ` (9 subsequent siblings)
  17 siblings, 0 replies; 27+ messages in thread
From: Philippe Mathieu-Daudé @ 2021-05-07 14:43 UTC (permalink / raw)
  To: qemu-devel
  Cc: Peter Maydell, Philippe Mathieu-Daudé,
	Laurent Vivier, qemu-arm, qemu-ppc, Gerd Hoffmann, Paolo Bonzini,
	Alex Bennée

Avoid initializing variables too early, since there is
2 possible failure points before they get used. Move them
after the lock_iovec() call.

Signed-off-by: Philippe Mathieu-Daudé <philmd@redhat.com>
---
 linux-user/syscall.c | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)

diff --git a/linux-user/syscall.c b/linux-user/syscall.c
index 7c5c821f48d..593241362a9 100644
--- a/linux-user/syscall.c
+++ b/linux-user/syscall.c
@@ -3379,15 +3379,8 @@ static abi_long do_sendrecvmsg_locked(int fd, struct target_msghdr *msgp,
         msg.msg_name = NULL;
         msg.msg_namelen = 0;
     }
-    msg.msg_controllen = 2 * tswapal(msgp->msg_controllen);
-    msg.msg_control = alloca(msg.msg_controllen);
-    memset(msg.msg_control, 0, msg.msg_controllen);
-
-    msg.msg_flags = tswap32(msgp->msg_flags);
 
     count = tswapal(msgp->msg_iovlen);
-    target_vec = tswapal(msgp->msg_iov);
-
     if (count > IOV_MAX) {
         /* sendrcvmsg returns a different errno for this condition than
          * readv/writev, so we must catch it here before lock_iovec() does.
@@ -3396,14 +3389,20 @@ static abi_long do_sendrecvmsg_locked(int fd, struct target_msghdr *msgp,
         goto out2;
     }
 
+    target_vec = tswapal(msgp->msg_iov);
     vec = lock_iovec(send ? VERIFY_READ : VERIFY_WRITE,
                      target_vec, count, send);
     if (vec == NULL) {
         ret = -host_to_target_errno(errno);
         goto out2;
     }
+
     msg.msg_iovlen = count;
     msg.msg_iov = vec;
+    msg.msg_flags = tswap32(msgp->msg_flags);
+    msg.msg_controllen = 2 * tswapal(msgp->msg_controllen);
+    msg.msg_control = alloca(msg.msg_controllen);
+    memset(msg.msg_control, 0, msg.msg_controllen);
 
     if (send) {
         if (fd_trans_target_to_host_data(fd)) {
-- 
2.26.3



^ permalink raw reply related	[flat|nested] 27+ messages in thread

* [PATCH v3 09/17] linux-user/syscall: Replace alloca() by GLib alloc() in sendrecvmsg
  2021-05-07 14:42 [PATCH v3 00/17] misc: Replace alloca() by g_malloc() Philippe Mathieu-Daudé
                   ` (7 preceding siblings ...)
  2021-05-07 14:43 ` [PATCH v3 08/17] linux-user/syscall: Move code around in do_sendrecvmsg_locked() Philippe Mathieu-Daudé
@ 2021-05-07 14:43 ` Philippe Mathieu-Daudé
  2021-05-07 14:43 ` [PATCH v3 10/17] audio/alsaaudio: Replace ALSA alloca() by malloc() equivalent Philippe Mathieu-Daudé
                   ` (8 subsequent siblings)
  17 siblings, 0 replies; 27+ messages in thread
From: Philippe Mathieu-Daudé @ 2021-05-07 14:43 UTC (permalink / raw)
  To: qemu-devel
  Cc: Peter Maydell, Philippe Mathieu-Daudé,
	Laurent Vivier, qemu-arm, qemu-ppc, Gerd Hoffmann, Paolo Bonzini,
	Alex Bennée

The ALLOCA(3) man-page mentions its "use is discouraged".

Use autofree heap allocation instead (returning ENOMEM on failure).

Suggested-by: Peter Maydell <peter.maydell@linaro.org>
Signed-off-by: Philippe Mathieu-Daudé <philmd@redhat.com>
---
 linux-user/syscall.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/linux-user/syscall.c b/linux-user/syscall.c
index 593241362a9..c88e240ff93 100644
--- a/linux-user/syscall.c
+++ b/linux-user/syscall.c
@@ -3358,13 +3358,19 @@ static abi_long do_sendrecvmsg_locked(int fd, struct target_msghdr *msgp,
     abi_ulong count;
     struct iovec *vec;
     abi_ulong target_vec;
+    g_autofree void *msg_control = NULL;
 
     if (msgp->msg_name) {
         msg.msg_namelen = tswap32(msgp->msg_namelen);
-        msg.msg_name = alloca(msg.msg_namelen+1);
+        msg.msg_name = g_try_malloc(msg.msg_namelen + 1);
+        if (!msg.msg_name) {
+            ret = -TARGET_ENOMEM;
+            goto out2;
+        }
         ret = target_to_host_sockaddr(fd, msg.msg_name,
                                       tswapal(msgp->msg_name),
                                       msg.msg_namelen);
+        g_free(msg.msg_name);
         if (ret == -TARGET_EFAULT) {
             /* For connected sockets msg_name and msg_namelen must
              * be ignored, so returning EFAULT immediately is wrong.
@@ -3401,8 +3407,7 @@ static abi_long do_sendrecvmsg_locked(int fd, struct target_msghdr *msgp,
     msg.msg_iov = vec;
     msg.msg_flags = tswap32(msgp->msg_flags);
     msg.msg_controllen = 2 * tswapal(msgp->msg_controllen);
-    msg.msg_control = alloca(msg.msg_controllen);
-    memset(msg.msg_control, 0, msg.msg_controllen);
+    msg.msg_control = msg_control = g_malloc0(msg.msg_controllen);
 
     if (send) {
         if (fd_trans_target_to_host_data(fd)) {
-- 
2.26.3



^ permalink raw reply related	[flat|nested] 27+ messages in thread

* [PATCH v3 10/17] audio/alsaaudio: Replace ALSA alloca() by malloc() equivalent
  2021-05-07 14:42 [PATCH v3 00/17] misc: Replace alloca() by g_malloc() Philippe Mathieu-Daudé
                   ` (8 preceding siblings ...)
  2021-05-07 14:43 ` [PATCH v3 09/17] linux-user/syscall: Replace alloca() by GLib alloc() in sendrecvmsg Philippe Mathieu-Daudé
@ 2021-05-07 14:43 ` Philippe Mathieu-Daudé
  2021-05-07 14:43 ` [PATCH v3 11/17] backends/tpm: Replace g_alloca() by g_malloc() Philippe Mathieu-Daudé
                   ` (7 subsequent siblings)
  17 siblings, 0 replies; 27+ messages in thread
From: Philippe Mathieu-Daudé @ 2021-05-07 14:43 UTC (permalink / raw)
  To: qemu-devel
  Cc: Peter Maydell, Philippe Mathieu-Daudé,
	Laurent Vivier, qemu-arm, qemu-ppc, Gerd Hoffmann, Paolo Bonzini,
	Alex Bennée

The ALLOCA(3) man-page mentions its "use is discouraged".

Define the cleanup functions for the snd_pcm_[hw/sw]_params_t types,
and replace the ALSA alloca() calls by equivalent ALSA malloc().

Signed-off-by: Philippe Mathieu-Daudé <philmd@redhat.com>
---
 audio/alsaaudio.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/audio/alsaaudio.c b/audio/alsaaudio.c
index fcc2f62864f..f39061ebc42 100644
--- a/audio/alsaaudio.c
+++ b/audio/alsaaudio.c
@@ -70,6 +70,9 @@ struct alsa_params_obt {
     snd_pcm_uframes_t samples;
 };
 
+G_DEFINE_AUTOPTR_CLEANUP_FUNC(snd_pcm_hw_params_t, snd_pcm_hw_params_free)
+G_DEFINE_AUTOPTR_CLEANUP_FUNC(snd_pcm_sw_params_t, snd_pcm_sw_params_free)
+
 static void GCC_FMT_ATTR (2, 3) alsa_logerr (int err, const char *fmt, ...)
 {
     va_list ap;
@@ -410,9 +413,9 @@ static void alsa_dump_info (struct alsa_params_req *req,
 static void alsa_set_threshold (snd_pcm_t *handle, snd_pcm_uframes_t threshold)
 {
     int err;
-    snd_pcm_sw_params_t *sw_params;
+    g_autoptr(snd_pcm_sw_params_t) sw_params = NULL;
 
-    snd_pcm_sw_params_alloca (&sw_params);
+    snd_pcm_sw_params_malloc(&sw_params);
 
     err = snd_pcm_sw_params_current (handle, sw_params);
     if (err < 0) {
@@ -444,7 +447,7 @@ static int alsa_open(bool in, struct alsa_params_req *req,
     AudiodevAlsaOptions *aopts = &dev->u.alsa;
     AudiodevAlsaPerDirectionOptions *apdo = in ? aopts->in : aopts->out;
     snd_pcm_t *handle;
-    snd_pcm_hw_params_t *hw_params;
+    g_autoptr(snd_pcm_hw_params_t) hw_params = NULL;
     int err;
     unsigned int freq, nchannels;
     const char *pcm_name = apdo->has_dev ? apdo->dev : "default";
@@ -455,7 +458,7 @@ static int alsa_open(bool in, struct alsa_params_req *req,
     freq = req->freq;
     nchannels = req->nchannels;
 
-    snd_pcm_hw_params_alloca (&hw_params);
+    snd_pcm_hw_params_malloc(&hw_params);
 
     err = snd_pcm_open (
         &handle,
-- 
2.26.3



^ permalink raw reply related	[flat|nested] 27+ messages in thread

* [PATCH v3 11/17] backends/tpm: Replace g_alloca() by g_malloc()
  2021-05-07 14:42 [PATCH v3 00/17] misc: Replace alloca() by g_malloc() Philippe Mathieu-Daudé
                   ` (9 preceding siblings ...)
  2021-05-07 14:43 ` [PATCH v3 10/17] audio/alsaaudio: Replace ALSA alloca() by malloc() equivalent Philippe Mathieu-Daudé
@ 2021-05-07 14:43 ` Philippe Mathieu-Daudé
  2021-05-07 14:43 ` [PATCH v3 12/17] gdbstub: Constify GdbCmdParseEntry Philippe Mathieu-Daudé
                   ` (6 subsequent siblings)
  17 siblings, 0 replies; 27+ messages in thread
From: Philippe Mathieu-Daudé @ 2021-05-07 14:43 UTC (permalink / raw)
  To: qemu-devel
  Cc: Peter Maydell, Stefan Berger, Philippe Mathieu-Daudé,
	Laurent Vivier, qemu-arm, qemu-ppc, Gerd Hoffmann, Paolo Bonzini,
	Alex Bennée, Stefan Berger

The ALLOCA(3) man-page mentions its "use is discouraged".

Replace a g_alloca() call by a autofree g_malloc() one,
moving the allocation before the MUTEX guarded block.

Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>
Signed-off-by: Philippe Mathieu-Daudé <philmd@redhat.com>
---
 backends/tpm/tpm_emulator.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/backends/tpm/tpm_emulator.c b/backends/tpm/tpm_emulator.c
index a012adc1934..9553cdd891f 100644
--- a/backends/tpm/tpm_emulator.c
+++ b/backends/tpm/tpm_emulator.c
@@ -123,12 +123,11 @@ static int tpm_emulator_ctrlcmd(TPMEmulator *tpm, unsigned long cmd, void *msg,
     CharBackend *dev = &tpm->ctrl_chr;
     uint32_t cmd_no = cpu_to_be32(cmd);
     ssize_t n = sizeof(uint32_t) + msg_len_in;
-    uint8_t *buf = NULL;
+    g_autofree uint8_t *buf = g_malloc(n);
     int ret = -1;
 
     qemu_mutex_lock(&tpm->mutex);
 
-    buf = g_alloca(n);
     memcpy(buf, &cmd_no, sizeof(cmd_no));
     memcpy(buf + sizeof(cmd_no), msg, msg_len_in);
 
-- 
2.26.3



^ permalink raw reply related	[flat|nested] 27+ messages in thread

* [PATCH v3 12/17] gdbstub: Constify GdbCmdParseEntry
  2021-05-07 14:42 [PATCH v3 00/17] misc: Replace alloca() by g_malloc() Philippe Mathieu-Daudé
                   ` (10 preceding siblings ...)
  2021-05-07 14:43 ` [PATCH v3 11/17] backends/tpm: Replace g_alloca() by g_malloc() Philippe Mathieu-Daudé
@ 2021-05-07 14:43 ` Philippe Mathieu-Daudé
  2021-05-07 14:43 ` [PATCH v3 13/17] gdbstub: Replace GdbCmdContext with plain g_array() Philippe Mathieu-Daudé
                   ` (5 subsequent siblings)
  17 siblings, 0 replies; 27+ messages in thread
From: Philippe Mathieu-Daudé @ 2021-05-07 14:43 UTC (permalink / raw)
  To: qemu-devel
  Cc: Peter Maydell, Philippe Mathieu-Daudé,
	Laurent Vivier, qemu-arm, qemu-ppc, Gerd Hoffmann, Paolo Bonzini,
	Alex Bennée

Reviewed-by: Alex Bennée <alex.bennee@linaro.org>
Signed-off-by: Philippe Mathieu-Daudé <philmd@redhat.com>
---
 gdbstub.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/gdbstub.c b/gdbstub.c
index 9103ffc9028..83d47c67325 100644
--- a/gdbstub.c
+++ b/gdbstub.c
@@ -1981,7 +1981,7 @@ static void handle_v_kill(GdbCmdContext *gdb_ctx, void *user_ctx)
     exit(0);
 }
 
-static GdbCmdParseEntry gdb_v_commands_table[] = {
+static const GdbCmdParseEntry gdb_v_commands_table[] = {
     /* Order is important if has same prefix */
     {
         .handler = handle_v_cont_query,
@@ -2324,7 +2324,7 @@ static void handle_set_qemu_phy_mem_mode(GdbCmdContext *gdb_ctx, void *user_ctx)
 }
 #endif
 
-static GdbCmdParseEntry gdb_gen_query_set_common_table[] = {
+static const GdbCmdParseEntry gdb_gen_query_set_common_table[] = {
     /* Order is important if has same prefix */
     {
         .handler = handle_query_qemu_sstepbits,
@@ -2342,7 +2342,7 @@ static GdbCmdParseEntry gdb_gen_query_set_common_table[] = {
     },
 };
 
-static GdbCmdParseEntry gdb_gen_query_table[] = {
+static const GdbCmdParseEntry gdb_gen_query_table[] = {
     {
         .handler = handle_query_curr_tid,
         .cmd = "C",
@@ -2420,7 +2420,7 @@ static GdbCmdParseEntry gdb_gen_query_table[] = {
 #endif
 };
 
-static GdbCmdParseEntry gdb_gen_set_table[] = {
+static const GdbCmdParseEntry gdb_gen_set_table[] = {
     /* Order is important if has same prefix */
     {
         .handler = handle_set_qemu_sstep,
-- 
2.26.3



^ permalink raw reply related	[flat|nested] 27+ messages in thread

* [PATCH v3 13/17] gdbstub: Replace GdbCmdContext with plain g_array()
  2021-05-07 14:42 [PATCH v3 00/17] misc: Replace alloca() by g_malloc() Philippe Mathieu-Daudé
                   ` (11 preceding siblings ...)
  2021-05-07 14:43 ` [PATCH v3 12/17] gdbstub: Constify GdbCmdParseEntry Philippe Mathieu-Daudé
@ 2021-05-07 14:43 ` Philippe Mathieu-Daudé
  2021-05-07 14:43 ` [PATCH v3 14/17] hw/misc/pca9552: Replace g_newa() by g_new() Philippe Mathieu-Daudé
                   ` (4 subsequent siblings)
  17 siblings, 0 replies; 27+ messages in thread
From: Philippe Mathieu-Daudé @ 2021-05-07 14:43 UTC (permalink / raw)
  To: qemu-devel
  Cc: Peter Maydell, Philippe Mathieu-Daudé,
	Laurent Vivier, qemu-arm, qemu-ppc, Gerd Hoffmann, Paolo Bonzini,
	Alex Bennée

From: Alex Bennée <alex.bennee@linaro.org>

Instead of jumping through hoops let glib deal with both tracking the
number of elements and auto freeing the memory once we are done. This
allows is to drop the usage of ALLOCA(3) which the man-page mentions
its "use is discouraged".

Signed-off-by: Alex Bennée <alex.bennee@linaro.org>
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
Message-Id: <20210506160741.9841-1-alex.bennee@linaro.org>
Signed-off-by: Philippe Mathieu-Daudé <philmd@redhat.com>
---
 gdbstub.c | 314 +++++++++++++++++++++++++-----------------------------
 1 file changed, 146 insertions(+), 168 deletions(-)

diff --git a/gdbstub.c b/gdbstub.c
index 83d47c67325..5e9e8e3e006 100644
--- a/gdbstub.c
+++ b/gdbstub.c
@@ -1338,6 +1338,8 @@ typedef union GdbCmdVariant {
     } thread_id;
 } GdbCmdVariant;
 
+#define get_param(p, i)    (&g_array_index(p, GdbCmdVariant, i))
+
 static const char *cmd_next_param(const char *param, const char delimiter)
 {
     static const char all_delimiters[] = ",;:=";
@@ -1363,54 +1365,46 @@ static const char *cmd_next_param(const char *param, const char delimiter)
 }
 
 static int cmd_parse_params(const char *data, const char *schema,
-                            GdbCmdVariant *params, int *num_params)
+                            GArray *params)
 {
-    int curr_param;
     const char *curr_schema, *curr_data;
 
-    *num_params = 0;
-
-    if (!schema) {
-        return 0;
-    }
+    g_assert(schema);
+    g_assert(params->len == 0);
 
     curr_schema = schema;
-    curr_param = 0;
     curr_data = data;
     while (curr_schema[0] && curr_schema[1] && *curr_data) {
+        GdbCmdVariant this_param;
+
         switch (curr_schema[0]) {
         case 'l':
             if (qemu_strtoul(curr_data, &curr_data, 16,
-                             &params[curr_param].val_ul)) {
+                             &this_param.val_ul)) {
                 return -EINVAL;
             }
-            curr_param++;
             curr_data = cmd_next_param(curr_data, curr_schema[1]);
             break;
         case 'L':
             if (qemu_strtou64(curr_data, &curr_data, 16,
-                              (uint64_t *)&params[curr_param].val_ull)) {
+                              (uint64_t *)&this_param.val_ull)) {
                 return -EINVAL;
             }
-            curr_param++;
             curr_data = cmd_next_param(curr_data, curr_schema[1]);
             break;
         case 's':
-            params[curr_param].data = curr_data;
-            curr_param++;
+            this_param.data = curr_data;
             curr_data = cmd_next_param(curr_data, curr_schema[1]);
             break;
         case 'o':
-            params[curr_param].opcode = *(uint8_t *)curr_data;
-            curr_param++;
+            this_param.opcode = *(uint8_t *)curr_data;
             curr_data = cmd_next_param(curr_data, curr_schema[1]);
             break;
         case 't':
-            params[curr_param].thread_id.kind =
+            this_param.thread_id.kind =
                 read_thread_id(curr_data, &curr_data,
-                               &params[curr_param].thread_id.pid,
-                               &params[curr_param].thread_id.tid);
-            curr_param++;
+                               &this_param.thread_id.pid,
+                               &this_param.thread_id.tid);
             curr_data = cmd_next_param(curr_data, curr_schema[1]);
             break;
         case '?':
@@ -1419,19 +1413,14 @@ static int cmd_parse_params(const char *data, const char *schema,
         default:
             return -EINVAL;
         }
+        g_array_append_val(params, this_param);
         curr_schema += 2;
     }
 
-    *num_params = curr_param;
     return 0;
 }
 
-typedef struct GdbCmdContext {
-    GdbCmdVariant *params;
-    int num_params;
-} GdbCmdContext;
-
-typedef void (*GdbCmdHandler)(GdbCmdContext *gdb_ctx, void *user_ctx);
+typedef void (*GdbCmdHandler)(GArray *params, void *user_ctx);
 
 /*
  * cmd_startswith -> cmd is compared using startswith
@@ -1471,8 +1460,8 @@ static inline int startswith(const char *string, const char *pattern)
 static int process_string_cmd(void *user_ctx, const char *data,
                               const GdbCmdParseEntry *cmds, int num_cmds)
 {
-    int i, schema_len, max_num_params = 0;
-    GdbCmdContext gdb_ctx;
+    int i;
+    g_autoptr(GArray) params = g_array_new(false, true, sizeof(GdbCmdVariant));
 
     if (!cmds) {
         return -1;
@@ -1488,24 +1477,13 @@ static int process_string_cmd(void *user_ctx, const char *data,
         }
 
         if (cmd->schema) {
-            schema_len = strlen(cmd->schema);
-            if (schema_len % 2) {
-                return -2;
+            if (cmd_parse_params(&data[strlen(cmd->cmd)],
+                                 cmd->schema, params)) {
+                return -1;
             }
-
-            max_num_params = schema_len / 2;
         }
 
-        gdb_ctx.params =
-            (GdbCmdVariant *)alloca(sizeof(*gdb_ctx.params) * max_num_params);
-        memset(gdb_ctx.params, 0, sizeof(*gdb_ctx.params) * max_num_params);
-
-        if (cmd_parse_params(&data[strlen(cmd->cmd)], cmd->schema,
-                             gdb_ctx.params, &gdb_ctx.num_params)) {
-            return -1;
-        }
-
-        cmd->handler(&gdb_ctx, user_ctx);
+        cmd->handler(params, user_ctx);
         return 0;
     }
 
@@ -1528,18 +1506,18 @@ static void run_cmd_parser(const char *data, const GdbCmdParseEntry *cmd)
     }
 }
 
-static void handle_detach(GdbCmdContext *gdb_ctx, void *user_ctx)
+static void handle_detach(GArray *params, void *user_ctx)
 {
     GDBProcess *process;
     uint32_t pid = 1;
 
     if (gdbserver_state.multiprocess) {
-        if (!gdb_ctx->num_params) {
+        if (!params->len) {
             put_packet("E22");
             return;
         }
 
-        pid = gdb_ctx->params[0].val_ul;
+        pid = get_param(params, 0)->val_ul;
     }
 
     process = gdb_get_process(pid);
@@ -1562,22 +1540,22 @@ static void handle_detach(GdbCmdContext *gdb_ctx, void *user_ctx)
     put_packet("OK");
 }
 
-static void handle_thread_alive(GdbCmdContext *gdb_ctx, void *user_ctx)
+static void handle_thread_alive(GArray *params, void *user_ctx)
 {
     CPUState *cpu;
 
-    if (!gdb_ctx->num_params) {
+    if (!params->len) {
         put_packet("E22");
         return;
     }
 
-    if (gdb_ctx->params[0].thread_id.kind == GDB_READ_THREAD_ERR) {
+    if (get_param(params, 0)->thread_id.kind == GDB_READ_THREAD_ERR) {
         put_packet("E22");
         return;
     }
 
-    cpu = gdb_get_cpu(gdb_ctx->params[0].thread_id.pid,
-                      gdb_ctx->params[0].thread_id.tid);
+    cpu = gdb_get_cpu(get_param(params, 0)->thread_id.pid,
+                      get_param(params, 0)->thread_id.tid);
     if (!cpu) {
         put_packet("E22");
         return;
@@ -1586,17 +1564,17 @@ static void handle_thread_alive(GdbCmdContext *gdb_ctx, void *user_ctx)
     put_packet("OK");
 }
 
-static void handle_continue(GdbCmdContext *gdb_ctx, void *user_ctx)
+static void handle_continue(GArray *params, void *user_ctx)
 {
-    if (gdb_ctx->num_params) {
-        gdb_set_cpu_pc(gdb_ctx->params[0].val_ull);
+    if (params->len) {
+        gdb_set_cpu_pc(get_param(params, 0)->val_ull);
     }
 
     gdbserver_state.signal = 0;
     gdb_continue();
 }
 
-static void handle_cont_with_sig(GdbCmdContext *gdb_ctx, void *user_ctx)
+static void handle_cont_with_sig(GArray *params, void *user_ctx)
 {
     unsigned long signal = 0;
 
@@ -1604,8 +1582,8 @@ static void handle_cont_with_sig(GdbCmdContext *gdb_ctx, void *user_ctx)
      * Note: C sig;[addr] is currently unsupported and we simply
      *       omit the addr parameter
      */
-    if (gdb_ctx->num_params) {
-        signal = gdb_ctx->params[0].val_ul;
+    if (params->len) {
+        signal = get_param(params, 0)->val_ul;
     }
 
     gdbserver_state.signal = gdb_signal_to_target(signal);
@@ -1615,27 +1593,27 @@ static void handle_cont_with_sig(GdbCmdContext *gdb_ctx, void *user_ctx)
     gdb_continue();
 }
 
-static void handle_set_thread(GdbCmdContext *gdb_ctx, void *user_ctx)
+static void handle_set_thread(GArray *params, void *user_ctx)
 {
     CPUState *cpu;
 
-    if (gdb_ctx->num_params != 2) {
+    if (params->len != 2) {
         put_packet("E22");
         return;
     }
 
-    if (gdb_ctx->params[1].thread_id.kind == GDB_READ_THREAD_ERR) {
+    if (get_param(params, 1)->thread_id.kind == GDB_READ_THREAD_ERR) {
         put_packet("E22");
         return;
     }
 
-    if (gdb_ctx->params[1].thread_id.kind != GDB_ONE_THREAD) {
+    if (get_param(params, 1)->thread_id.kind != GDB_ONE_THREAD) {
         put_packet("OK");
         return;
     }
 
-    cpu = gdb_get_cpu(gdb_ctx->params[1].thread_id.pid,
-                      gdb_ctx->params[1].thread_id.tid);
+    cpu = gdb_get_cpu(get_param(params, 1)->thread_id.pid,
+                      get_param(params, 1)->thread_id.tid);
     if (!cpu) {
         put_packet("E22");
         return;
@@ -1645,7 +1623,7 @@ static void handle_set_thread(GdbCmdContext *gdb_ctx, void *user_ctx)
      * Note: This command is deprecated and modern gdb's will be using the
      *       vCont command instead.
      */
-    switch (gdb_ctx->params[0].opcode) {
+    switch (get_param(params, 0)->opcode) {
     case 'c':
         gdbserver_state.c_cpu = cpu;
         put_packet("OK");
@@ -1660,18 +1638,18 @@ static void handle_set_thread(GdbCmdContext *gdb_ctx, void *user_ctx)
     }
 }
 
-static void handle_insert_bp(GdbCmdContext *gdb_ctx, void *user_ctx)
+static void handle_insert_bp(GArray *params, void *user_ctx)
 {
     int res;
 
-    if (gdb_ctx->num_params != 3) {
+    if (params->len != 3) {
         put_packet("E22");
         return;
     }
 
-    res = gdb_breakpoint_insert(gdb_ctx->params[0].val_ul,
-                                gdb_ctx->params[1].val_ull,
-                                gdb_ctx->params[2].val_ull);
+    res = gdb_breakpoint_insert(get_param(params, 0)->val_ul,
+                                get_param(params, 1)->val_ull,
+                                get_param(params, 2)->val_ull);
     if (res >= 0) {
         put_packet("OK");
         return;
@@ -1683,18 +1661,18 @@ static void handle_insert_bp(GdbCmdContext *gdb_ctx, void *user_ctx)
     put_packet("E22");
 }
 
-static void handle_remove_bp(GdbCmdContext *gdb_ctx, void *user_ctx)
+static void handle_remove_bp(GArray *params, void *user_ctx)
 {
     int res;
 
-    if (gdb_ctx->num_params != 3) {
+    if (params->len != 3) {
         put_packet("E22");
         return;
     }
 
-    res = gdb_breakpoint_remove(gdb_ctx->params[0].val_ul,
-                                gdb_ctx->params[1].val_ull,
-                                gdb_ctx->params[2].val_ull);
+    res = gdb_breakpoint_remove(get_param(params, 0)->val_ul,
+                                get_param(params, 1)->val_ull,
+                                get_param(params, 2)->val_ull);
     if (res >= 0) {
         put_packet("OK");
         return;
@@ -1717,7 +1695,7 @@ static void handle_remove_bp(GdbCmdContext *gdb_ctx, void *user_ctx)
  * the remote gdb to fallback to older methods.
  */
 
-static void handle_set_reg(GdbCmdContext *gdb_ctx, void *user_ctx)
+static void handle_set_reg(GArray *params, void *user_ctx)
 {
     int reg_size;
 
@@ -1726,19 +1704,19 @@ static void handle_set_reg(GdbCmdContext *gdb_ctx, void *user_ctx)
         return;
     }
 
-    if (gdb_ctx->num_params != 2) {
+    if (params->len != 2) {
         put_packet("E22");
         return;
     }
 
-    reg_size = strlen(gdb_ctx->params[1].data) / 2;
-    hextomem(gdbserver_state.mem_buf, gdb_ctx->params[1].data, reg_size);
+    reg_size = strlen(get_param(params, 1)->data) / 2;
+    hextomem(gdbserver_state.mem_buf, get_param(params, 1)->data, reg_size);
     gdb_write_register(gdbserver_state.g_cpu, gdbserver_state.mem_buf->data,
-                       gdb_ctx->params[0].val_ull);
+                       get_param(params, 0)->val_ull);
     put_packet("OK");
 }
 
-static void handle_get_reg(GdbCmdContext *gdb_ctx, void *user_ctx)
+static void handle_get_reg(GArray *params, void *user_ctx)
 {
     int reg_size;
 
@@ -1747,14 +1725,14 @@ static void handle_get_reg(GdbCmdContext *gdb_ctx, void *user_ctx)
         return;
     }
 
-    if (!gdb_ctx->num_params) {
+    if (!params->len) {
         put_packet("E14");
         return;
     }
 
     reg_size = gdb_read_register(gdbserver_state.g_cpu,
                                  gdbserver_state.mem_buf,
-                                 gdb_ctx->params[0].val_ull);
+                                 get_param(params, 0)->val_ull);
     if (!reg_size) {
         put_packet("E14");
         return;
@@ -1766,22 +1744,22 @@ static void handle_get_reg(GdbCmdContext *gdb_ctx, void *user_ctx)
     put_strbuf();
 }
 
-static void handle_write_mem(GdbCmdContext *gdb_ctx, void *user_ctx)
+static void handle_write_mem(GArray *params, void *user_ctx)
 {
-    if (gdb_ctx->num_params != 3) {
+    if (params->len != 3) {
         put_packet("E22");
         return;
     }
 
     /* hextomem() reads 2*len bytes */
-    if (gdb_ctx->params[1].val_ull > strlen(gdb_ctx->params[2].data) / 2) {
+    if (get_param(params, 1)->val_ull > strlen(get_param(params, 2)->data) / 2) {
         put_packet("E22");
         return;
     }
 
-    hextomem(gdbserver_state.mem_buf, gdb_ctx->params[2].data,
-             gdb_ctx->params[1].val_ull);
-    if (target_memory_rw_debug(gdbserver_state.g_cpu, gdb_ctx->params[0].val_ull,
+    hextomem(gdbserver_state.mem_buf, get_param(params, 2)->data,
+             get_param(params, 1)->val_ull);
+    if (target_memory_rw_debug(gdbserver_state.g_cpu, get_param(params, 0)->val_ull,
                                gdbserver_state.mem_buf->data,
                                gdbserver_state.mem_buf->len, true)) {
         put_packet("E14");
@@ -1791,22 +1769,22 @@ static void handle_write_mem(GdbCmdContext *gdb_ctx, void *user_ctx)
     put_packet("OK");
 }
 
-static void handle_read_mem(GdbCmdContext *gdb_ctx, void *user_ctx)
+static void handle_read_mem(GArray *params, void *user_ctx)
 {
-    if (gdb_ctx->num_params != 2) {
+    if (params->len != 2) {
         put_packet("E22");
         return;
     }
 
     /* memtohex() doubles the required space */
-    if (gdb_ctx->params[1].val_ull > MAX_PACKET_LENGTH / 2) {
+    if (get_param(params, 1)->val_ull > MAX_PACKET_LENGTH / 2) {
         put_packet("E22");
         return;
     }
 
-    g_byte_array_set_size(gdbserver_state.mem_buf, gdb_ctx->params[1].val_ull);
+    g_byte_array_set_size(gdbserver_state.mem_buf, get_param(params, 1)->val_ull);
 
-    if (target_memory_rw_debug(gdbserver_state.g_cpu, gdb_ctx->params[0].val_ull,
+    if (target_memory_rw_debug(gdbserver_state.g_cpu, get_param(params, 0)->val_ull,
                                gdbserver_state.mem_buf->data,
                                gdbserver_state.mem_buf->len, false)) {
         put_packet("E14");
@@ -1818,19 +1796,19 @@ static void handle_read_mem(GdbCmdContext *gdb_ctx, void *user_ctx)
     put_strbuf();
 }
 
-static void handle_write_all_regs(GdbCmdContext *gdb_ctx, void *user_ctx)
+static void handle_write_all_regs(GArray *params, void *user_ctx)
 {
     target_ulong addr, len;
     uint8_t *registers;
     int reg_size;
 
-    if (!gdb_ctx->num_params) {
+    if (!params->len) {
         return;
     }
 
     cpu_synchronize_state(gdbserver_state.g_cpu);
-    len = strlen(gdb_ctx->params[0].data) / 2;
-    hextomem(gdbserver_state.mem_buf, gdb_ctx->params[0].data, len);
+    len = strlen(get_param(params, 0)->data) / 2;
+    hextomem(gdbserver_state.mem_buf, get_param(params, 0)->data, len);
     registers = gdbserver_state.mem_buf->data;
     for (addr = 0; addr < gdbserver_state.g_cpu->gdb_num_g_regs && len > 0;
          addr++) {
@@ -1841,7 +1819,7 @@ static void handle_write_all_regs(GdbCmdContext *gdb_ctx, void *user_ctx)
     put_packet("OK");
 }
 
-static void handle_read_all_regs(GdbCmdContext *gdb_ctx, void *user_ctx)
+static void handle_read_all_regs(GArray *params, void *user_ctx)
 {
     target_ulong addr, len;
 
@@ -1859,14 +1837,14 @@ static void handle_read_all_regs(GdbCmdContext *gdb_ctx, void *user_ctx)
     put_strbuf();
 }
 
-static void handle_file_io(GdbCmdContext *gdb_ctx, void *user_ctx)
+static void handle_file_io(GArray *params, void *user_ctx)
 {
-    if (gdb_ctx->num_params >= 1 && gdbserver_state.current_syscall_cb) {
+    if (params->len >= 1 && gdbserver_state.current_syscall_cb) {
         target_ulong ret, err;
 
-        ret = (target_ulong)gdb_ctx->params[0].val_ull;
-        if (gdb_ctx->num_params >= 2) {
-            err = (target_ulong)gdb_ctx->params[1].val_ull;
+        ret = (target_ulong)get_param(params, 0)->val_ull;
+        if (params->len >= 2) {
+            err = (target_ulong)get_param(params, 1)->val_ull;
         } else {
             err = 0;
         }
@@ -1874,7 +1852,7 @@ static void handle_file_io(GdbCmdContext *gdb_ctx, void *user_ctx)
         gdbserver_state.current_syscall_cb = NULL;
     }
 
-    if (gdb_ctx->num_params >= 3 && gdb_ctx->params[2].opcode == (uint8_t)'C') {
+    if (params->len >= 3 && get_param(params, 2)->opcode == (uint8_t)'C') {
         put_packet("T02");
         return;
     }
@@ -1882,23 +1860,23 @@ static void handle_file_io(GdbCmdContext *gdb_ctx, void *user_ctx)
     gdb_continue();
 }
 
-static void handle_step(GdbCmdContext *gdb_ctx, void *user_ctx)
+static void handle_step(GArray *params, void *user_ctx)
 {
-    if (gdb_ctx->num_params) {
-        gdb_set_cpu_pc((target_ulong)gdb_ctx->params[0].val_ull);
+    if (params->len) {
+        gdb_set_cpu_pc((target_ulong)get_param(params, 0)->val_ull);
     }
 
     cpu_single_step(gdbserver_state.c_cpu, get_sstep_flags());
     gdb_continue();
 }
 
-static void handle_backward(GdbCmdContext *gdb_ctx, void *user_ctx)
+static void handle_backward(GArray *params, void *user_ctx)
 {
     if (replay_mode != REPLAY_MODE_PLAY) {
         put_packet("E22");
     }
-    if (gdb_ctx->num_params == 1) {
-        switch (gdb_ctx->params[0].opcode) {
+    if (params->len == 1) {
+        switch (get_param(params, 0)->opcode) {
         case 's':
             if (replay_reverse_step()) {
                 gdb_continue();
@@ -1920,20 +1898,20 @@ static void handle_backward(GdbCmdContext *gdb_ctx, void *user_ctx)
     put_packet("");
 }
 
-static void handle_v_cont_query(GdbCmdContext *gdb_ctx, void *user_ctx)
+static void handle_v_cont_query(GArray *params, void *user_ctx)
 {
     put_packet("vCont;c;C;s;S");
 }
 
-static void handle_v_cont(GdbCmdContext *gdb_ctx, void *user_ctx)
+static void handle_v_cont(GArray *params, void *user_ctx)
 {
     int res;
 
-    if (!gdb_ctx->num_params) {
+    if (!params->len) {
         return;
     }
 
-    res = gdb_handle_vcont(gdb_ctx->params[0].data);
+    res = gdb_handle_vcont(get_param(params, 0)->data);
     if ((res == -EINVAL) || (res == -ERANGE)) {
         put_packet("E22");
     } else if (res) {
@@ -1941,17 +1919,17 @@ static void handle_v_cont(GdbCmdContext *gdb_ctx, void *user_ctx)
     }
 }
 
-static void handle_v_attach(GdbCmdContext *gdb_ctx, void *user_ctx)
+static void handle_v_attach(GArray *params, void *user_ctx)
 {
     GDBProcess *process;
     CPUState *cpu;
 
     g_string_assign(gdbserver_state.str_buf, "E22");
-    if (!gdb_ctx->num_params) {
+    if (!params->len) {
         goto cleanup;
     }
 
-    process = gdb_get_process(gdb_ctx->params[0].val_ul);
+    process = gdb_get_process(get_param(params, 0)->val_ul);
     if (!process) {
         goto cleanup;
     }
@@ -1972,7 +1950,7 @@ cleanup:
     put_strbuf();
 }
 
-static void handle_v_kill(GdbCmdContext *gdb_ctx, void *user_ctx)
+static void handle_v_kill(GArray *params, void *user_ctx)
 {
     /* Kill the target */
     put_packet("OK");
@@ -2007,43 +1985,43 @@ static const GdbCmdParseEntry gdb_v_commands_table[] = {
     },
 };
 
-static void handle_v_commands(GdbCmdContext *gdb_ctx, void *user_ctx)
+static void handle_v_commands(GArray *params, void *user_ctx)
 {
-    if (!gdb_ctx->num_params) {
+    if (!params->len) {
         return;
     }
 
-    if (process_string_cmd(NULL, gdb_ctx->params[0].data,
+    if (process_string_cmd(NULL, get_param(params, 0)->data,
                            gdb_v_commands_table,
                            ARRAY_SIZE(gdb_v_commands_table))) {
         put_packet("");
     }
 }
 
-static void handle_query_qemu_sstepbits(GdbCmdContext *gdb_ctx, void *user_ctx)
+static void handle_query_qemu_sstepbits(GArray *params, void *user_ctx)
 {
     g_string_printf(gdbserver_state.str_buf, "ENABLE=%x,NOIRQ=%x,NOTIMER=%x",
                     SSTEP_ENABLE, SSTEP_NOIRQ, SSTEP_NOTIMER);
     put_strbuf();
 }
 
-static void handle_set_qemu_sstep(GdbCmdContext *gdb_ctx, void *user_ctx)
+static void handle_set_qemu_sstep(GArray *params, void *user_ctx)
 {
-    if (!gdb_ctx->num_params) {
+    if (!params->len) {
         return;
     }
 
-    sstep_flags = gdb_ctx->params[0].val_ul;
+    sstep_flags = get_param(params, 0)->val_ul;
     put_packet("OK");
 }
 
-static void handle_query_qemu_sstep(GdbCmdContext *gdb_ctx, void *user_ctx)
+static void handle_query_qemu_sstep(GArray *params, void *user_ctx)
 {
     g_string_printf(gdbserver_state.str_buf, "0x%x", sstep_flags);
     put_strbuf();
 }
 
-static void handle_query_curr_tid(GdbCmdContext *gdb_ctx, void *user_ctx)
+static void handle_query_curr_tid(GArray *params, void *user_ctx)
 {
     CPUState *cpu;
     GDBProcess *process;
@@ -2060,7 +2038,7 @@ static void handle_query_curr_tid(GdbCmdContext *gdb_ctx, void *user_ctx)
     put_strbuf();
 }
 
-static void handle_query_threads(GdbCmdContext *gdb_ctx, void *user_ctx)
+static void handle_query_threads(GArray *params, void *user_ctx)
 {
     if (!gdbserver_state.query_cpu) {
         put_packet("l");
@@ -2073,25 +2051,25 @@ static void handle_query_threads(GdbCmdContext *gdb_ctx, void *user_ctx)
     gdbserver_state.query_cpu = gdb_next_attached_cpu(gdbserver_state.query_cpu);
 }
 
-static void handle_query_first_threads(GdbCmdContext *gdb_ctx, void *user_ctx)
+static void handle_query_first_threads(GArray *params, void *user_ctx)
 {
     gdbserver_state.query_cpu = gdb_first_attached_cpu();
-    handle_query_threads(gdb_ctx, user_ctx);
+    handle_query_threads(params, user_ctx);
 }
 
-static void handle_query_thread_extra(GdbCmdContext *gdb_ctx, void *user_ctx)
+static void handle_query_thread_extra(GArray *params, void *user_ctx)
 {
     g_autoptr(GString) rs = g_string_new(NULL);
     CPUState *cpu;
 
-    if (!gdb_ctx->num_params ||
-        gdb_ctx->params[0].thread_id.kind == GDB_READ_THREAD_ERR) {
+    if (!params->len ||
+        get_param(params, 0)->thread_id.kind == GDB_READ_THREAD_ERR) {
         put_packet("E22");
         return;
     }
 
-    cpu = gdb_get_cpu(gdb_ctx->params[0].thread_id.pid,
-                      gdb_ctx->params[0].thread_id.tid);
+    cpu = gdb_get_cpu(get_param(params, 0)->thread_id.pid,
+                      get_param(params, 0)->thread_id.tid);
     if (!cpu) {
         return;
     }
@@ -2116,7 +2094,7 @@ static void handle_query_thread_extra(GdbCmdContext *gdb_ctx, void *user_ctx)
 }
 
 #ifdef CONFIG_USER_ONLY
-static void handle_query_offsets(GdbCmdContext *gdb_ctx, void *user_ctx)
+static void handle_query_offsets(GArray *params, void *user_ctx)
 {
     TaskState *ts;
 
@@ -2131,17 +2109,17 @@ static void handle_query_offsets(GdbCmdContext *gdb_ctx, void *user_ctx)
     put_strbuf();
 }
 #else
-static void handle_query_rcmd(GdbCmdContext *gdb_ctx, void *user_ctx)
+static void handle_query_rcmd(GArray *params, void *user_ctx)
 {
     const guint8 zero = 0;
     int len;
 
-    if (!gdb_ctx->num_params) {
+    if (!params->len) {
         put_packet("E22");
         return;
     }
 
-    len = strlen(gdb_ctx->params[0].data);
+    len = strlen(get_param(params, 0)->data);
     if (len % 2) {
         put_packet("E01");
         return;
@@ -2149,7 +2127,7 @@ static void handle_query_rcmd(GdbCmdContext *gdb_ctx, void *user_ctx)
 
     g_assert(gdbserver_state.mem_buf->len == 0);
     len = len / 2;
-    hextomem(gdbserver_state.mem_buf, gdb_ctx->params[0].data, len);
+    hextomem(gdbserver_state.mem_buf, get_param(params, 0)->data, len);
     g_byte_array_append(gdbserver_state.mem_buf, &zero, 1);
     qemu_chr_be_write(gdbserver_state.mon_chr, gdbserver_state.mem_buf->data,
                       gdbserver_state.mem_buf->len);
@@ -2157,7 +2135,7 @@ static void handle_query_rcmd(GdbCmdContext *gdb_ctx, void *user_ctx)
 }
 #endif
 
-static void handle_query_supported(GdbCmdContext *gdb_ctx, void *user_ctx)
+static void handle_query_supported(GArray *params, void *user_ctx)
 {
     CPUClass *cc;
 
@@ -2178,8 +2156,8 @@ static void handle_query_supported(GdbCmdContext *gdb_ctx, void *user_ctx)
     }
 #endif
 
-    if (gdb_ctx->num_params &&
-        strstr(gdb_ctx->params[0].data, "multiprocess+")) {
+    if (params->len &&
+        strstr(get_param(params, 0)->data, "multiprocess+")) {
         gdbserver_state.multiprocess = true;
     }
 
@@ -2187,7 +2165,7 @@ static void handle_query_supported(GdbCmdContext *gdb_ctx, void *user_ctx)
     put_strbuf();
 }
 
-static void handle_query_xfer_features(GdbCmdContext *gdb_ctx, void *user_ctx)
+static void handle_query_xfer_features(GArray *params, void *user_ctx)
 {
     GDBProcess *process;
     CPUClass *cc;
@@ -2195,7 +2173,7 @@ static void handle_query_xfer_features(GdbCmdContext *gdb_ctx, void *user_ctx)
     const char *xml;
     const char *p;
 
-    if (gdb_ctx->num_params < 3) {
+    if (params->len < 3) {
         put_packet("E22");
         return;
     }
@@ -2208,15 +2186,15 @@ static void handle_query_xfer_features(GdbCmdContext *gdb_ctx, void *user_ctx)
     }
 
     gdb_has_xml = true;
-    p = gdb_ctx->params[0].data;
+    p = get_param(params, 0)->data;
     xml = get_feature_xml(p, &p, process);
     if (!xml) {
         put_packet("E00");
         return;
     }
 
-    addr = gdb_ctx->params[1].val_ul;
-    len = gdb_ctx->params[2].val_ul;
+    addr = get_param(params, 1)->val_ul;
+    len = get_param(params, 2)->val_ul;
     total_len = strlen(xml);
     if (addr > total_len) {
         put_packet("E00");
@@ -2240,18 +2218,18 @@ static void handle_query_xfer_features(GdbCmdContext *gdb_ctx, void *user_ctx)
 }
 
 #if defined(CONFIG_USER_ONLY) && defined(CONFIG_LINUX_USER)
-static void handle_query_xfer_auxv(GdbCmdContext *gdb_ctx, void *user_ctx)
+static void handle_query_xfer_auxv(GArray *params, void *user_ctx)
 {
     TaskState *ts;
     unsigned long offset, len, saved_auxv, auxv_len;
 
-    if (gdb_ctx->num_params < 2) {
+    if (params->len < 2) {
         put_packet("E22");
         return;
     }
 
-    offset = gdb_ctx->params[0].val_ul;
-    len = gdb_ctx->params[1].val_ul;
+    offset = get_param(params, 0)->val_ul;
+    len = get_param(params, 1)->val_ul;
     ts = gdbserver_state.c_cpu->opaque;
     saved_auxv = ts->info->saved_auxv;
     auxv_len = ts->info->auxv_len;
@@ -2286,12 +2264,12 @@ static void handle_query_xfer_auxv(GdbCmdContext *gdb_ctx, void *user_ctx)
 }
 #endif
 
-static void handle_query_attached(GdbCmdContext *gdb_ctx, void *user_ctx)
+static void handle_query_attached(GArray *params, void *user_ctx)
 {
     put_packet(GDB_ATTACHED);
 }
 
-static void handle_query_qemu_supported(GdbCmdContext *gdb_ctx, void *user_ctx)
+static void handle_query_qemu_supported(GArray *params, void *user_ctx)
 {
     g_string_printf(gdbserver_state.str_buf, "sstepbits;sstep");
 #ifndef CONFIG_USER_ONLY
@@ -2301,21 +2279,21 @@ static void handle_query_qemu_supported(GdbCmdContext *gdb_ctx, void *user_ctx)
 }
 
 #ifndef CONFIG_USER_ONLY
-static void handle_query_qemu_phy_mem_mode(GdbCmdContext *gdb_ctx,
+static void handle_query_qemu_phy_mem_mode(GArray *params,
                                            void *user_ctx)
 {
     g_string_printf(gdbserver_state.str_buf, "%d", phy_memory_mode);
     put_strbuf();
 }
 
-static void handle_set_qemu_phy_mem_mode(GdbCmdContext *gdb_ctx, void *user_ctx)
+static void handle_set_qemu_phy_mem_mode(GArray *params, void *user_ctx)
 {
-    if (!gdb_ctx->num_params) {
+    if (!params->len) {
         put_packet("E22");
         return;
     }
 
-    if (!gdb_ctx->params[0].val_ul) {
+    if (!get_param(params, 0)->val_ul) {
         phy_memory_mode = 0;
     } else {
         phy_memory_mode = 1;
@@ -2438,45 +2416,45 @@ static const GdbCmdParseEntry gdb_gen_set_table[] = {
 #endif
 };
 
-static void handle_gen_query(GdbCmdContext *gdb_ctx, void *user_ctx)
+static void handle_gen_query(GArray *params, void *user_ctx)
 {
-    if (!gdb_ctx->num_params) {
+    if (!params->len) {
         return;
     }
 
-    if (!process_string_cmd(NULL, gdb_ctx->params[0].data,
+    if (!process_string_cmd(NULL, get_param(params, 0)->data,
                             gdb_gen_query_set_common_table,
                             ARRAY_SIZE(gdb_gen_query_set_common_table))) {
         return;
     }
 
-    if (process_string_cmd(NULL, gdb_ctx->params[0].data,
+    if (process_string_cmd(NULL, get_param(params, 0)->data,
                            gdb_gen_query_table,
                            ARRAY_SIZE(gdb_gen_query_table))) {
         put_packet("");
     }
 }
 
-static void handle_gen_set(GdbCmdContext *gdb_ctx, void *user_ctx)
+static void handle_gen_set(GArray *params, void *user_ctx)
 {
-    if (!gdb_ctx->num_params) {
+    if (!params->len) {
         return;
     }
 
-    if (!process_string_cmd(NULL, gdb_ctx->params[0].data,
+    if (!process_string_cmd(NULL, get_param(params, 0)->data,
                             gdb_gen_query_set_common_table,
                             ARRAY_SIZE(gdb_gen_query_set_common_table))) {
         return;
     }
 
-    if (process_string_cmd(NULL, gdb_ctx->params[0].data,
+    if (process_string_cmd(NULL, get_param(params, 0)->data,
                            gdb_gen_set_table,
                            ARRAY_SIZE(gdb_gen_set_table))) {
         put_packet("");
     }
 }
 
-static void handle_target_halt(GdbCmdContext *gdb_ctx, void *user_ctx)
+static void handle_target_halt(GArray *params, void *user_ctx)
 {
     g_string_printf(gdbserver_state.str_buf, "T%02xthread:", GDB_SIGNAL_TRAP);
     gdb_append_thread_id(gdbserver_state.c_cpu, gdbserver_state.str_buf);
-- 
2.26.3



^ permalink raw reply related	[flat|nested] 27+ messages in thread

* [PATCH v3 14/17] hw/misc/pca9552: Replace g_newa() by g_new()
  2021-05-07 14:42 [PATCH v3 00/17] misc: Replace alloca() by g_malloc() Philippe Mathieu-Daudé
                   ` (12 preceding siblings ...)
  2021-05-07 14:43 ` [PATCH v3 13/17] gdbstub: Replace GdbCmdContext with plain g_array() Philippe Mathieu-Daudé
@ 2021-05-07 14:43 ` Philippe Mathieu-Daudé
  2021-05-10  5:59   ` Cédric Le Goater
  2021-05-07 14:43   ` Philippe Mathieu-Daudé
                   ` (3 subsequent siblings)
  17 siblings, 1 reply; 27+ messages in thread
From: Philippe Mathieu-Daudé @ 2021-05-07 14:43 UTC (permalink / raw)
  To: qemu-devel
  Cc: Peter Maydell, Andrew Jeffery, Philippe Mathieu-Daudé,
	Laurent Vivier, Joel Stanley, qemu-arm, qemu-ppc, Gerd Hoffmann,
	Paolo Bonzini, Alex Bennée, Cédric Le Goater

The ALLOCA(3) man-page mentions its "use is discouraged".

Use autofree heap allocation instead, replacing g_newa() by g_new().

Signed-off-by: Philippe Mathieu-Daudé <philmd@redhat.com>
---
 hw/misc/pca9552.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/hw/misc/pca9552.c b/hw/misc/pca9552.c
index b7686e27d7f..facf103cbfb 100644
--- a/hw/misc/pca9552.c
+++ b/hw/misc/pca9552.c
@@ -71,7 +71,7 @@ static void pca955x_display_pins_status(PCA955xState *s,
         return;
     }
     if (trace_event_get_state_backends(TRACE_PCA955X_GPIO_STATUS)) {
-        char *buf = g_newa(char, k->pin_count + 1);
+        g_autofree char *buf = g_new(char, k->pin_count + 1);
 
         for (i = 0; i < k->pin_count; i++) {
             if (extract32(pins_status, i, 1)) {
-- 
2.26.3



^ permalink raw reply related	[flat|nested] 27+ messages in thread

* [PATCH v3 15/17] target/ppc/kvm: Replace alloca() by g_malloc()
  2021-05-07 14:42 [PATCH v3 00/17] misc: Replace alloca() by g_malloc() Philippe Mathieu-Daudé
@ 2021-05-07 14:43   ` Philippe Mathieu-Daudé
  2021-05-07 14:43 ` [PATCH v3 02/17] linux-user/elfload: Replace alloca() by g_try_malloc() Philippe Mathieu-Daudé
                     ` (16 subsequent siblings)
  17 siblings, 0 replies; 27+ messages in thread
From: Philippe Mathieu-Daudé @ 2021-05-07 14:43 UTC (permalink / raw)
  To: qemu-devel
  Cc: Laurent Vivier, Paolo Bonzini, qemu-ppc, Peter Maydell,
	Alex Bennée, Gerd Hoffmann, qemu-arm,
	Philippe Mathieu-Daudé,
	Greg Kurz, David Gibson, open list:Overall KVM CPUs

The ALLOCA(3) man-page mentions its "use is discouraged".

Use autofree heap allocation instead, replacing it by a g_malloc call.

Reviewed-by: Greg Kurz <groug@kaod.org>
Signed-off-by: Philippe Mathieu-Daudé <philmd@redhat.com>
---
 target/ppc/kvm.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/target/ppc/kvm.c b/target/ppc/kvm.c
index 104a308abb5..23c4ea377e8 100644
--- a/target/ppc/kvm.c
+++ b/target/ppc/kvm.c
@@ -2698,11 +2698,11 @@ int kvmppc_save_htab(QEMUFile *f, int fd, size_t bufsize, int64_t max_ns)
 int kvmppc_load_htab_chunk(QEMUFile *f, int fd, uint32_t index,
                            uint16_t n_valid, uint16_t n_invalid, Error **errp)
 {
-    struct kvm_get_htab_header *buf;
+    g_autofree struct kvm_get_htab_header *buf = NULL;
     size_t chunksize = sizeof(*buf) + n_valid * HASH_PTE_SIZE_64;
     ssize_t rc;
 
-    buf = alloca(chunksize);
+    buf = g_malloc(chunksize);
     buf->index = index;
     buf->n_valid = n_valid;
     buf->n_invalid = n_invalid;
-- 
2.26.3


^ permalink raw reply related	[flat|nested] 27+ messages in thread

* [PATCH v3 15/17] target/ppc/kvm: Replace alloca() by g_malloc()
@ 2021-05-07 14:43   ` Philippe Mathieu-Daudé
  0 siblings, 0 replies; 27+ messages in thread
From: Philippe Mathieu-Daudé @ 2021-05-07 14:43 UTC (permalink / raw)
  To: qemu-devel
  Cc: Peter Maydell, open list:Overall KVM CPUs,
	Philippe Mathieu-Daudé,
	Laurent Vivier, Greg Kurz, qemu-arm, qemu-ppc, Gerd Hoffmann,
	Paolo Bonzini, Alex Bennée, David Gibson

The ALLOCA(3) man-page mentions its "use is discouraged".

Use autofree heap allocation instead, replacing it by a g_malloc call.

Reviewed-by: Greg Kurz <groug@kaod.org>
Signed-off-by: Philippe Mathieu-Daudé <philmd@redhat.com>
---
 target/ppc/kvm.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/target/ppc/kvm.c b/target/ppc/kvm.c
index 104a308abb5..23c4ea377e8 100644
--- a/target/ppc/kvm.c
+++ b/target/ppc/kvm.c
@@ -2698,11 +2698,11 @@ int kvmppc_save_htab(QEMUFile *f, int fd, size_t bufsize, int64_t max_ns)
 int kvmppc_load_htab_chunk(QEMUFile *f, int fd, uint32_t index,
                            uint16_t n_valid, uint16_t n_invalid, Error **errp)
 {
-    struct kvm_get_htab_header *buf;
+    g_autofree struct kvm_get_htab_header *buf = NULL;
     size_t chunksize = sizeof(*buf) + n_valid * HASH_PTE_SIZE_64;
     ssize_t rc;
 
-    buf = alloca(chunksize);
+    buf = g_malloc(chunksize);
     buf->index = index;
     buf->n_valid = n_valid;
     buf->n_invalid = n_invalid;
-- 
2.26.3



^ permalink raw reply related	[flat|nested] 27+ messages in thread

* [PATCH v3 16/17] configure: Prohibit alloca() by using -Walloca CPPFLAG
  2021-05-07 14:42 [PATCH v3 00/17] misc: Replace alloca() by g_malloc() Philippe Mathieu-Daudé
                   ` (14 preceding siblings ...)
  2021-05-07 14:43   ` Philippe Mathieu-Daudé
@ 2021-05-07 14:43 ` Philippe Mathieu-Daudé
  2021-05-07 17:14   ` Philippe Mathieu-Daudé
  2021-05-07 14:43 ` [NOTFORMERGE PATCH v3 17/17] configure: libSLiRP buildsys kludge Philippe Mathieu-Daudé
  2021-05-07 17:19 ` [PATCH v3 18/17] tests/unit/test-char: Replace g_alloca() by buffer on the stack Philippe Mathieu-Daudé
  17 siblings, 1 reply; 27+ messages in thread
From: Philippe Mathieu-Daudé @ 2021-05-07 14:43 UTC (permalink / raw)
  To: qemu-devel
  Cc: Peter Maydell, Philippe Mathieu-Daudé,
	Laurent Vivier, qemu-arm, qemu-ppc, Gerd Hoffmann, Paolo Bonzini,
	Alex Bennée

Now that we removed all alloca() calls in the repository, add the
-Walloca CPPFLAG to trigger a build failure if such stack allocation
is used.

Rationale: The ALLOCA(3) man-page mentions its "use is discouraged".

Signed-off-by: Philippe Mathieu-Daudé <philmd@redhat.com>
---
As there is an alloca() call in libslirp, this patch is pending
on the following libslirp patch to be merged:
https://lists.freedesktop.org/archives/slirp/2021-May/000150.html
(and the submodule updated).
---
 configure | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/configure b/configure
index 4f374b48890..67cb6d5421c 100755
--- a/configure
+++ b/configure
@@ -552,7 +552,7 @@ ARFLAGS="${ARFLAGS-rv}"
 # provides these semantics.)
 QEMU_CFLAGS="-fno-strict-aliasing -fno-common -fwrapv $QEMU_CFLAGS"
 QEMU_CFLAGS="-Wundef -Wwrite-strings -Wmissing-prototypes $QEMU_CFLAGS"
-QEMU_CFLAGS="-Wstrict-prototypes -Wredundant-decls $QEMU_CFLAGS"
+QEMU_CFLAGS="-Wstrict-prototypes -Wredundant-decls -Walloca $QEMU_CFLAGS"
 QEMU_CFLAGS="-D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE $QEMU_CFLAGS"
 
 # Flags that are needed during configure but later taken care of by Meson
-- 
2.26.3



^ permalink raw reply related	[flat|nested] 27+ messages in thread

* [NOTFORMERGE PATCH v3 17/17] configure: libSLiRP buildsys kludge
  2021-05-07 14:42 [PATCH v3 00/17] misc: Replace alloca() by g_malloc() Philippe Mathieu-Daudé
                   ` (15 preceding siblings ...)
  2021-05-07 14:43 ` [PATCH v3 16/17] configure: Prohibit alloca() by using -Walloca CPPFLAG Philippe Mathieu-Daudé
@ 2021-05-07 14:43 ` Philippe Mathieu-Daudé
  2021-05-07 17:15   ` Philippe Mathieu-Daudé
  2021-05-07 17:19 ` [PATCH v3 18/17] tests/unit/test-char: Replace g_alloca() by buffer on the stack Philippe Mathieu-Daudé
  17 siblings, 1 reply; 27+ messages in thread
From: Philippe Mathieu-Daudé @ 2021-05-07 14:43 UTC (permalink / raw)
  To: qemu-devel
  Cc: Peter Maydell, Philippe Mathieu-Daudé,
	Laurent Vivier, qemu-arm, qemu-ppc, Gerd Hoffmann, Paolo Bonzini,
	Alex Bennée

Only enable -Walloca when libSLiRP is not built.

Signed-off-by: Philippe Mathieu-Daudé <philmd@redhat.com>
---
 configure | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/configure b/configure
index 67cb6d5421c..ab5b6248253 100755
--- a/configure
+++ b/configure
@@ -552,7 +552,7 @@ ARFLAGS="${ARFLAGS-rv}"
 # provides these semantics.)
 QEMU_CFLAGS="-fno-strict-aliasing -fno-common -fwrapv $QEMU_CFLAGS"
 QEMU_CFLAGS="-Wundef -Wwrite-strings -Wmissing-prototypes $QEMU_CFLAGS"
-QEMU_CFLAGS="-Wstrict-prototypes -Wredundant-decls -Walloca $QEMU_CFLAGS"
+QEMU_CFLAGS="-Wstrict-prototypes -Wredundant-decls $QEMU_CFLAGS"
 QEMU_CFLAGS="-D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE $QEMU_CFLAGS"
 
 # Flags that are needed during configure but later taken care of by Meson
@@ -5255,6 +5255,14 @@ case "$slirp" in
     ;;
 esac
 
+# Kludge pending an alloca() call removed from libSLiRP, see:
+# https://lists.freedesktop.org/archives/slirp/2021-May/000150.html
+case "$slirp" in
+  internal | disabled)
+    QEMU_CFLAGS="-Walloca $QEMU_CFLAGS"
+    ;;
+esac
+
 ##########################################
 # check for usable __NR_keyctl syscall
 
-- 
2.26.3



^ permalink raw reply related	[flat|nested] 27+ messages in thread

* Re: [PATCH v3 16/17] configure: Prohibit alloca() by using -Walloca CPPFLAG
  2021-05-07 14:43 ` [PATCH v3 16/17] configure: Prohibit alloca() by using -Walloca CPPFLAG Philippe Mathieu-Daudé
@ 2021-05-07 17:14   ` Philippe Mathieu-Daudé
  0 siblings, 0 replies; 27+ messages in thread
From: Philippe Mathieu-Daudé @ 2021-05-07 17:14 UTC (permalink / raw)
  To: qemu-devel
  Cc: Peter Maydell, Laurent Vivier, qemu-arm, qemu-ppc, Gerd Hoffmann,
	Paolo Bonzini, Alex Bennée

On 5/7/21 4:43 PM, Philippe Mathieu-Daudé wrote:
> Now that we removed all alloca() calls in the repository, add the
> -Walloca CPPFLAG to trigger a build failure if such stack allocation
> is used.
> 
> Rationale: The ALLOCA(3) man-page mentions its "use is discouraged".
> 
> Signed-off-by: Philippe Mathieu-Daudé <philmd@redhat.com>
> ---
> As there is an alloca() call in libslirp, this patch is pending
> on the following libslirp patch to be merged:
> https://lists.freedesktop.org/archives/slirp/2021-May/000150.html
> (and the submodule updated).
> ---
>  configure | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/configure b/configure
> index 4f374b48890..67cb6d5421c 100755
> --- a/configure
> +++ b/configure
> @@ -552,7 +552,7 @@ ARFLAGS="${ARFLAGS-rv}"
>  # provides these semantics.)
>  QEMU_CFLAGS="-fno-strict-aliasing -fno-common -fwrapv $QEMU_CFLAGS"
>  QEMU_CFLAGS="-Wundef -Wwrite-strings -Wmissing-prototypes $QEMU_CFLAGS"
> -QEMU_CFLAGS="-Wstrict-prototypes -Wredundant-decls $QEMU_CFLAGS"
> +QEMU_CFLAGS="-Wstrict-prototypes -Wredundant-decls -Walloca $QEMU_CFLAGS"
>  QEMU_CFLAGS="-D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE $QEMU_CFLAGS"

Sigh I forgot one Clang version (7.0.1) doesn't recognize this warning.

I'll replace this patch by:

-- >8 --
diff --git a/configure b/configure
index 4f374b48890..4330245fa1f 100755
--- a/configure
+++ b/configure
@@ -2083,2 +2083,3 @@ add_to warn_flags -Wexpansion-to-defined
 add_to warn_flags -Wimplicit-fallthrough=2
+add_to warn_flags -Walloca

---

Regards,

Phil.



^ permalink raw reply related	[flat|nested] 27+ messages in thread

* Re: [NOTFORMERGE PATCH v3 17/17] configure: libSLiRP buildsys kludge
  2021-05-07 14:43 ` [NOTFORMERGE PATCH v3 17/17] configure: libSLiRP buildsys kludge Philippe Mathieu-Daudé
@ 2021-05-07 17:15   ` Philippe Mathieu-Daudé
  0 siblings, 0 replies; 27+ messages in thread
From: Philippe Mathieu-Daudé @ 2021-05-07 17:15 UTC (permalink / raw)
  To: qemu-devel
  Cc: Peter Maydell, Laurent Vivier, qemu-arm, qemu-ppc, Gerd Hoffmann,
	Paolo Bonzini, Alex Bennée

On 5/7/21 4:43 PM, Philippe Mathieu-Daudé wrote:
> Only enable -Walloca when libSLiRP is not built.
> 
> Signed-off-by: Philippe Mathieu-Daudé <philmd@redhat.com>
> ---
>  configure | 10 +++++++++-
>  1 file changed, 9 insertions(+), 1 deletion(-)

> +# Kludge pending an alloca() call removed from libSLiRP, see:
> +# https://lists.freedesktop.org/archives/slirp/2021-May/000150.html

FYI it has already been merged, thanks to Marc-André :)



^ permalink raw reply	[flat|nested] 27+ messages in thread

* [PATCH v3 18/17] tests/unit/test-char: Replace g_alloca() by buffer on the stack
  2021-05-07 14:42 [PATCH v3 00/17] misc: Replace alloca() by g_malloc() Philippe Mathieu-Daudé
                   ` (16 preceding siblings ...)
  2021-05-07 14:43 ` [NOTFORMERGE PATCH v3 17/17] configure: libSLiRP buildsys kludge Philippe Mathieu-Daudé
@ 2021-05-07 17:19 ` Philippe Mathieu-Daudé
  2021-05-07 20:44   ` Marc-André Lureau
  17 siblings, 1 reply; 27+ messages in thread
From: Philippe Mathieu-Daudé @ 2021-05-07 17:19 UTC (permalink / raw)
  Cc: Marc-André Lureau, Thomas Huth, Philippe Mathieu-Daudé,
	qemu-devel, Paolo Bonzini

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;
     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



^ permalink raw reply related	[flat|nested] 27+ messages in thread

* Re: [PATCH v3 18/17] tests/unit/test-char: Replace g_alloca() by buffer on the stack
  2021-05-07 17:19 ` [PATCH v3 18/17] tests/unit/test-char: Replace g_alloca() by buffer on the stack Philippe Mathieu-Daudé
@ 2021-05-07 20:44   ` Marc-André Lureau
  2021-05-07 21:25     ` Richard Henderson
  0 siblings, 1 reply; 27+ messages in thread
From: Marc-André Lureau @ 2021-05-07 20:44 UTC (permalink / raw)
  To: Philippe Mathieu-Daudé; +Cc: Paolo Bonzini, Thomas Huth, QEMU

[-- Attachment #1: Type: text/plain, Size: 1315 bytes --]

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
>
>
>

-- 
Marc-André Lureau

[-- Attachment #2: Type: text/html, Size: 2057 bytes --]

^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: [PATCH v3 18/17] tests/unit/test-char: Replace g_alloca() by buffer on the stack
  2021-05-07 20:44   ` Marc-André Lureau
@ 2021-05-07 21:25     ` Richard Henderson
  0 siblings, 0 replies; 27+ messages in thread
From: Richard Henderson @ 2021-05-07 21:25 UTC (permalink / raw)
  To: Marc-André Lureau, Philippe Mathieu-Daudé
  Cc: Paolo Bonzini, Thomas Huth, QEMU

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~


^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: [PATCH v3 15/17] target/ppc/kvm: Replace alloca() by g_malloc()
  2021-05-07 14:43   ` Philippe Mathieu-Daudé
@ 2021-05-10  5:38     ` David Gibson
  -1 siblings, 0 replies; 27+ messages in thread
From: David Gibson @ 2021-05-10  5:38 UTC (permalink / raw)
  To: Philippe Mathieu-Daudé
  Cc: qemu-devel, Laurent Vivier, Paolo Bonzini, qemu-ppc,
	Peter Maydell, Alex Bennée, Gerd Hoffmann, qemu-arm,
	Greg Kurz, open list:Overall KVM CPUs

[-- Attachment #1: Type: text/plain, Size: 1429 bytes --]

On Fri, May 07, 2021 at 04:43:13PM +0200, Philippe Mathieu-Daudé wrote:
> The ALLOCA(3) man-page mentions its "use is discouraged".
> 
> Use autofree heap allocation instead, replacing it by a g_malloc call.
> 
> Reviewed-by: Greg Kurz <groug@kaod.org>
> Signed-off-by: Philippe Mathieu-Daudé <philmd@redhat.com>

Acked-by: David Gibson <david@gibson.dropbear.id.au>

> ---
>  target/ppc/kvm.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/target/ppc/kvm.c b/target/ppc/kvm.c
> index 104a308abb5..23c4ea377e8 100644
> --- a/target/ppc/kvm.c
> +++ b/target/ppc/kvm.c
> @@ -2698,11 +2698,11 @@ int kvmppc_save_htab(QEMUFile *f, int fd, size_t bufsize, int64_t max_ns)
>  int kvmppc_load_htab_chunk(QEMUFile *f, int fd, uint32_t index,
>                             uint16_t n_valid, uint16_t n_invalid, Error **errp)
>  {
> -    struct kvm_get_htab_header *buf;
> +    g_autofree struct kvm_get_htab_header *buf = NULL;
>      size_t chunksize = sizeof(*buf) + n_valid * HASH_PTE_SIZE_64;
>      ssize_t rc;
>  
> -    buf = alloca(chunksize);
> +    buf = g_malloc(chunksize);
>      buf->index = index;
>      buf->n_valid = n_valid;
>      buf->n_invalid = n_invalid;

-- 
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: [PATCH v3 15/17] target/ppc/kvm: Replace alloca() by g_malloc()
@ 2021-05-10  5:38     ` David Gibson
  0 siblings, 0 replies; 27+ messages in thread
From: David Gibson @ 2021-05-10  5:38 UTC (permalink / raw)
  To: Philippe Mathieu-Daudé
  Cc: Peter Maydell, open list:Overall KVM CPUs, qemu-devel,
	Laurent Vivier, Greg Kurz, qemu-arm, qemu-ppc, Gerd Hoffmann,
	Paolo Bonzini, Alex Bennée

[-- Attachment #1: Type: text/plain, Size: 1429 bytes --]

On Fri, May 07, 2021 at 04:43:13PM +0200, Philippe Mathieu-Daudé wrote:
> The ALLOCA(3) man-page mentions its "use is discouraged".
> 
> Use autofree heap allocation instead, replacing it by a g_malloc call.
> 
> Reviewed-by: Greg Kurz <groug@kaod.org>
> Signed-off-by: Philippe Mathieu-Daudé <philmd@redhat.com>

Acked-by: David Gibson <david@gibson.dropbear.id.au>

> ---
>  target/ppc/kvm.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/target/ppc/kvm.c b/target/ppc/kvm.c
> index 104a308abb5..23c4ea377e8 100644
> --- a/target/ppc/kvm.c
> +++ b/target/ppc/kvm.c
> @@ -2698,11 +2698,11 @@ int kvmppc_save_htab(QEMUFile *f, int fd, size_t bufsize, int64_t max_ns)
>  int kvmppc_load_htab_chunk(QEMUFile *f, int fd, uint32_t index,
>                             uint16_t n_valid, uint16_t n_invalid, Error **errp)
>  {
> -    struct kvm_get_htab_header *buf;
> +    g_autofree struct kvm_get_htab_header *buf = NULL;
>      size_t chunksize = sizeof(*buf) + n_valid * HASH_PTE_SIZE_64;
>      ssize_t rc;
>  
> -    buf = alloca(chunksize);
> +    buf = g_malloc(chunksize);
>      buf->index = index;
>      buf->n_valid = n_valid;
>      buf->n_invalid = n_invalid;

-- 
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: [PATCH v3 14/17] hw/misc/pca9552: Replace g_newa() by g_new()
  2021-05-07 14:43 ` [PATCH v3 14/17] hw/misc/pca9552: Replace g_newa() by g_new() Philippe Mathieu-Daudé
@ 2021-05-10  5:59   ` Cédric Le Goater
  0 siblings, 0 replies; 27+ messages in thread
From: Cédric Le Goater @ 2021-05-10  5:59 UTC (permalink / raw)
  To: Philippe Mathieu-Daudé, qemu-devel
  Cc: Peter Maydell, Andrew Jeffery, Laurent Vivier, qemu-arm,
	qemu-ppc, Gerd Hoffmann, Paolo Bonzini, Alex Bennée,
	Joel Stanley

On 5/7/21 4:43 PM, Philippe Mathieu-Daudé wrote:
> The ALLOCA(3) man-page mentions its "use is discouraged".
> 
> Use autofree heap allocation instead, replacing g_newa() by g_new().
> 
> Signed-off-by: Philippe Mathieu-Daudé <philmd@redhat.com>

Reviewed-by: Cédric Le Goater <clg@kaod.org>


> ---
>  hw/misc/pca9552.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/hw/misc/pca9552.c b/hw/misc/pca9552.c
> index b7686e27d7f..facf103cbfb 100644
> --- a/hw/misc/pca9552.c
> +++ b/hw/misc/pca9552.c
> @@ -71,7 +71,7 @@ static void pca955x_display_pins_status(PCA955xState *s,
>          return;
>      }
>      if (trace_event_get_state_backends(TRACE_PCA955X_GPIO_STATUS)) {
> -        char *buf = g_newa(char, k->pin_count + 1);
> +        g_autofree char *buf = g_new(char, k->pin_count + 1);
>  
>          for (i = 0; i < k->pin_count; i++) {
>              if (extract32(pins_status, i, 1)) {
> 



^ permalink raw reply	[flat|nested] 27+ messages in thread

end of thread, other threads:[~2021-05-10  6:01 UTC | newest]

Thread overview: 27+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-05-07 14:42 [PATCH v3 00/17] misc: Replace alloca() by g_malloc() Philippe Mathieu-Daudé
2021-05-07 14:42 ` [PATCH v3 01/17] bsd-user/syscall: Replace alloca() by g_try_new() Philippe Mathieu-Daudé
2021-05-07 14:43 ` [PATCH v3 02/17] linux-user/elfload: Replace alloca() by g_try_malloc() Philippe Mathieu-Daudé
2021-05-07 14:43 ` [PATCH v3 03/17] linux-user/syscall: Replace alloca() by g_try_new() Philippe Mathieu-Daudé
2021-05-07 14:43 ` [PATCH v3 04/17] linux-user/syscall: Replace alloca() by g_try_malloc() Philippe Mathieu-Daudé
2021-05-07 14:43 ` [PATCH v3 05/17] linux-user: Replace alloca() by g_try_new() in ppoll() syscall Philippe Mathieu-Daudé
2021-05-07 14:43 ` [PATCH v3 06/17] linux-user: Replace alloca() by g_try_malloc() in setsockopt() syscall Philippe Mathieu-Daudé
2021-05-07 14:43 ` [PATCH v3 07/17] linux-user: Replace alloca() by g_try_malloc() in various socket syscall Philippe Mathieu-Daudé
2021-05-07 14:43 ` [PATCH v3 08/17] linux-user/syscall: Move code around in do_sendrecvmsg_locked() Philippe Mathieu-Daudé
2021-05-07 14:43 ` [PATCH v3 09/17] linux-user/syscall: Replace alloca() by GLib alloc() in sendrecvmsg Philippe Mathieu-Daudé
2021-05-07 14:43 ` [PATCH v3 10/17] audio/alsaaudio: Replace ALSA alloca() by malloc() equivalent Philippe Mathieu-Daudé
2021-05-07 14:43 ` [PATCH v3 11/17] backends/tpm: Replace g_alloca() by g_malloc() Philippe Mathieu-Daudé
2021-05-07 14:43 ` [PATCH v3 12/17] gdbstub: Constify GdbCmdParseEntry Philippe Mathieu-Daudé
2021-05-07 14:43 ` [PATCH v3 13/17] gdbstub: Replace GdbCmdContext with plain g_array() Philippe Mathieu-Daudé
2021-05-07 14:43 ` [PATCH v3 14/17] hw/misc/pca9552: Replace g_newa() by g_new() Philippe Mathieu-Daudé
2021-05-10  5:59   ` Cédric Le Goater
2021-05-07 14:43 ` [PATCH v3 15/17] target/ppc/kvm: Replace alloca() by g_malloc() Philippe Mathieu-Daudé
2021-05-07 14:43   ` Philippe Mathieu-Daudé
2021-05-10  5:38   ` David Gibson
2021-05-10  5:38     ` David Gibson
2021-05-07 14:43 ` [PATCH v3 16/17] configure: Prohibit alloca() by using -Walloca CPPFLAG Philippe Mathieu-Daudé
2021-05-07 17:14   ` Philippe Mathieu-Daudé
2021-05-07 14:43 ` [NOTFORMERGE PATCH v3 17/17] configure: libSLiRP buildsys kludge Philippe Mathieu-Daudé
2021-05-07 17:15   ` Philippe Mathieu-Daudé
2021-05-07 17:19 ` [PATCH v3 18/17] tests/unit/test-char: Replace g_alloca() by buffer on the stack Philippe Mathieu-Daudé
2021-05-07 20:44   ` Marc-André Lureau
2021-05-07 21:25     ` Richard Henderson

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.