Skip to contentSkip to conversation

linux-user/elfload.c: Don't memset(NULL..) if malloc() failed

PATCH Applied Peter Maydell 2 participants 2 messages Nov 9, 2011 – Nov 11, 2011

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 linux-user/elfload.c: Don't memset(NULL..) if malloc() failed

If a malloc() in copy_elf_strings() failed we would call memset() before the "did malloc fail?" check. Fix this by moving to the glib alloc/free routines for this memory so we can use g_try_malloc0 rather than having a separate memset(). Spotted by Coverity (see bug 887883).

---
We could obviously also fix this by just moving the memset after the null check, but I think we want to move towards consistently using the glib memory routines everywhere anyway.

 linux-user/elfload.c   |    5 ++---
 linux-user/linuxload.c |    2 +-
 2 files changed, 3 insertions(+), 4 deletions(-)
linux-user/elfload.c +2 -3
index a413976..4635bb2 100644
@@ -1105,8 +1105,7 @@ static abi_ulong copy_elf_strings(int argc,char ** argv, void **page,
                 offset = p % TARGET_PAGE_SIZE;
                 pag = (char *)page[p/TARGET_PAGE_SIZE];
                 if (!pag) {
-                    pag = (char *)malloc(TARGET_PAGE_SIZE);
-                    memset(pag, 0, TARGET_PAGE_SIZE);
+                    pag = g_try_malloc0(TARGET_PAGE_SIZE);
                     page[p/TARGET_PAGE_SIZE] = pag;
                     if (!pag)
                         return 0;
@@ -1164,7 +1163,7 @@ static abi_ulong setup_arg_pages(abi_ulong p, struct linux_binprm *bprm,
             info->rss++;
             /* FIXME - check return value of memcpy_to_target() for failure */
             memcpy_to_target(stack_base, bprm->page[i], TARGET_PAGE_SIZE);
-            free(bprm->page[i]);
+            g_free(bprm->page[i]);
         }
         stack_base += TARGET_PAGE_SIZE;
     }
linux-user/linuxload.c +1 -1
index 62ebc7e..b47025f 100644
@@ -178,7 +178,7 @@ int loader_exec(const char * filename, char ** argv, char ** envp,
 
     /* Something went wrong, return the inode and free the argument pages*/
     for (i=0 ; i<MAX_ARG_PAGES ; i++) {
-        free(bprm->page[i]);
+        g_free(bprm->page[i]);
     }
     return(retval);
 }
Signature · 1 line
1.7.1
#1 lore raw
Quoted 7 lines · Peter Maydell

If a malloc() in copy_elf_strings() failed we would call memset() before the "did malloc fail?" check. Fix this by moving to the glib alloc/free routines for this memory so we can use g_try_malloc0 rather than having a separate memset(). Spotted by Coverity (see bug 887883).

Applied. Thanks.

Regards,

Anthony Liguori

Quoted 45 lines

---
We could obviously also fix this by just moving the memset after the null check, but I think we want to move towards consistently using the glib memory routines everywhere anyway.

  linux-user/elfload.c   |    5 ++---
  linux-user/linuxload.c |    2 +-
  2 files changed, 3 insertions(+), 4 deletions(-)
linux-user/elfload.c +2 -3
index a413976..4635bb2 100644
@@ -1105,8 +1105,7 @@ static abi_ulong copy_elf_strings(int argc,char ** argv, void **page,
                  offset = p % TARGET_PAGE_SIZE;
                  pag = (char *)page[p/TARGET_PAGE_SIZE];
                  if (!pag) {
-                    pag = (char *)malloc(TARGET_PAGE_SIZE);
-                    memset(pag, 0, TARGET_PAGE_SIZE);
+                    pag = g_try_malloc0(TARGET_PAGE_SIZE);
                      page[p/TARGET_PAGE_SIZE] = pag;
                      if (!pag)
                          return 0;
@@ -1164,7 +1163,7 @@ static abi_ulong setup_arg_pages(abi_ulong p, struct linux_binprm *bprm,
              info->rss++;
              /* FIXME - check return value of memcpy_to_target() for failure */
              memcpy_to_target(stack_base, bprm->page[i], TARGET_PAGE_SIZE);
-            free(bprm->page[i]);
+            g_free(bprm->page[i]);
          }
          stack_base += TARGET_PAGE_SIZE;
      }
linux-user/linuxload.c +1 -1
index 62ebc7e..b47025f 100644
@@ -178,7 +178,7 @@ int loader_exec(const char * filename, char ** argv, char ** envp,
 
      /* Something went wrong, return the inode and free the argument pages*/
      for (i=0 ; i<MAX_ARG_PAGES ; i++) {
-        free(bprm->page[i]);
+        g_free(bprm->page[i]);
      }
      return(retval);
  }