This is an automated email from the ASF dual-hosted git repository.

archer pushed a commit to branch 24021901
in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git

commit 84424e9abe0bef380188e4db9b3488e98ff05fc8
Author: chao an <anc...@lixiang.com>
AuthorDate: Thu Feb 8 15:49:47 2024 +0800

    nsh/uname: alloc dynamic heap on non-GNU compilers
    
    The alloca() function is machine- and compiler-dependent.  For certain
    applications, its use can improve efficiency compared to the use of
    malloc(3) plus free(3).  In certain cases, it can also simplify memory
    deallocation in applications that use longjmp(3) or siglongjmp(3).
    Otherwise, its use is discouraged.
    
    Signed-off-by: chao an <anc...@lixiang.com>
---
 nshlib/nsh_syscmds.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/nshlib/nsh_syscmds.c b/nshlib/nsh_syscmds.c
index b5fa7af08..2e708b8cc 100644
--- a/nshlib/nsh_syscmds.c
+++ b/nshlib/nsh_syscmds.c
@@ -687,6 +687,7 @@ int cmd_rptun(FAR struct nsh_vtbl_s *vtbl, int argc, FAR 
char **argv)
 #ifndef CONFIG_NSH_DISABLE_UNAME
 int cmd_uname(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
 {
+  FAR void *putsname;
   FAR const char *str;
   struct lib_memoutstream_s stream;
   struct utsname info;
@@ -780,8 +781,12 @@ int cmd_uname(FAR struct nsh_vtbl_s *vtbl, int argc, FAR 
char **argv)
   /* Process each option */
 
   first = true;
-  lib_memoutstream(&stream, alloca(sizeof(struct utsname)),
-                   sizeof(struct utsname));
+#ifdef __GNUC__
+  putsname = alloca(sizeof(struct utsname));
+#else
+  putsname = malloc(sizeof(struct utsname));
+#endif
+  lib_memoutstream(&stream, putsname, sizeof(struct utsname));
   for (i = 0; set != 0; i++)
     {
       unsigned int mask = (1 << i);
@@ -836,6 +841,9 @@ int cmd_uname(FAR struct nsh_vtbl_s *vtbl, int argc, FAR 
char **argv)
 
   lib_stream_putc(&stream, '\n');
   nsh_write(vtbl, stream.buffer, stream.common.nput);
+#ifndef __GNUC__
+  free(putsname);
+#endif
   return OK;
 }
 #endif

Reply via email to