v1-> Convert malloc()/calloc() calls to g_malloc()/g_try_malloc()/g_new0()
in linux-user/syscall.c file

v1->v2  convert the free() call in host_to_target_semarray()
to g_free() and calls g_try_malloc(count)  instead of
g_try_malloc(sizeof(count))

Signed-off-by: Harmandeep Kaur <write.harmand...@gmail.com>
---
v2->v3 used g_try_new() and friends to avoid overflow issues.

 linux-user/syscall.c | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)

diff --git a/linux-user/syscall.c b/linux-user/syscall.c
index d1d3eb2..5cfb7dd 100644
--- a/linux-user/syscall.c
+++ b/linux-user/syscall.c
@@ -1554,7 +1554,7 @@ set_timeout:
                 }

                 fprog.len = tswap16(tfprog->len);
-                filter = malloc(fprog.len * sizeof(*filter));
+                filter = g_try_new(struct sock_filter, fprog.len);
                 if (filter == NULL) {
                     unlock_user_struct(tfilter, tfprog->filter, 1);
                     unlock_user_struct(tfprog, optval_addr, 1);
@@ -1570,7 +1570,7 @@ set_timeout:

                 ret = get_errno(setsockopt(sockfd, SOL_SOCKET,
                                 SO_ATTACH_FILTER, &fprog, sizeof(fprog)));
-                free(filter);
+                g_free(filter);

                 unlock_user_struct(tfilter, tfprog->filter, 1);
                 unlock_user_struct(tfprog, optval_addr, 1);
@@ -1881,7 +1881,7 @@ static struct iovec *lock_iovec(int type, abi_ulong
target_addr,
         return NULL;
     }

-    vec = calloc(count, sizeof(struct iovec));
+    vec = g_try_new0(struct iovec, count);
     if (vec == NULL) {
         errno = ENOMEM;
         return NULL;
@@ -1945,7 +1945,7 @@ static struct iovec *lock_iovec(int type, abi_ulong
target_addr,
     }
     unlock_user(target_vec, target_addr, 0);
  fail2:
-    free(vec);
+    g_free(vec);
     errno = err;
     return NULL;
 }
@@ -2672,14 +2672,14 @@ static inline abi_long target_to_host_semarray(int
semid, unsigned short **host_

     nsems = semid_ds.sem_nsems;

-    *host_array = malloc(nsems*sizeof(unsigned short));
+    *host_array = g_try_new(unsigned short, nsems);
     if (!*host_array) {
         return -TARGET_ENOMEM;
     }
     array = lock_user(VERIFY_READ, target_addr,
                       nsems*sizeof(unsigned short), 1);
     if (!array) {
-        free(*host_array);
+        g_free(*host_array);
         return -TARGET_EFAULT;
     }

@@ -2975,7 +2975,7 @@ static inline abi_long do_msgsnd(int msqid, abi_long
msgp,

     if (!lock_user_struct(VERIFY_READ, target_mb, msgp, 0))
         return -TARGET_EFAULT;
-    host_mb = malloc(msgsz+sizeof(long));
+    host_mb = g_try_malloc(msgsz + sizeof(long));
     if (!host_mb) {
         unlock_user_struct(target_mb, msgp, 0);
         return -TARGET_ENOMEM;
@@ -2983,7 +2983,7 @@ static inline abi_long do_msgsnd(int msqid, abi_long
msgp,
     host_mb->mtype = (abi_long) tswapal(target_mb->mtype);
     memcpy(host_mb->mtext, target_mb->mtext, msgsz);
     ret = get_errno(msgsnd(msqid, host_mb, msgsz, msgflg));
-    free(host_mb);
+    g_free(host_mb);
     unlock_user_struct(target_mb, msgp, 0);

     return ret;
@@ -7625,7 +7625,7 @@ abi_long do_syscall(void *cpu_env, int num, abi_long
arg1,
             struct linux_dirent *dirp;
             abi_long count = arg3;

-        dirp = malloc(count);
+        dirp = g_try_malloc(sizeof(count));
         if (!dirp) {
                 ret = -TARGET_ENOMEM;
                 goto fail;
@@ -7662,7 +7662,7 @@ abi_long do_syscall(void *cpu_env, int num, abi_long
arg1,
         ret = count1;
                 unlock_user(target_dirp, arg2, ret);
             }
-        free(dirp);
+        g_free(dirp);
         }
 #else
         {
-- 
1.9.1

Reply via email to