From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:44651) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zlcok-0001bS-0O for qemu-devel@nongnu.org; Mon, 12 Oct 2015 09:07:10 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Zlcog-0005W1-JE for qemu-devel@nongnu.org; Mon, 12 Oct 2015 09:07:05 -0400 Received: from jessie.kos.to ([212.47.231.226]:49622 helo=pilvi.kos.to) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zlcog-0005UZ-AP for qemu-devel@nongnu.org; Mon, 12 Oct 2015 09:07:02 -0400 From: Riku Voipio Date: Mon, 12 Oct 2015 16:06:53 +0300 MIME-Version: 1.0 Message-ID: In-Reply-To: References: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: quoted-printable Subject: Re: [Qemu-devel] =?iso-8859-1?q?=5BPATCH_v6=5D_linux-user/syscall=2Ec?= =?iso-8859-1?q?=3A_malloc=28=29/calloc=28=29_to_g=5Fmalloc=28=29/g?= =?iso-8859-1?q?=5Ftry=5Fmalloc=28=29/g=5Fnew0=28=29?= List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Harmandeep Kaur Cc: Stefan Hajnoczi , qemu-devel On tiistaina 6. lokakuuta 2015 19.17.12 EEST, Harmandeep Kaur wrote: > Convert malloc()/ calloc() calls to g_malloc()/ g_try_malloc()/ g_new0() > > All heap memory allocation should go through glib so that we can take > advantage of a single memory allocator and its debugging/tracing features. Please use git send-email next time, I needed to manually fix the patch to=20= apply. Applied to linux-user, thanks.=20 > Signed-off-by: Harmandeep Kaur > --- > v1->v2 convert the free() call in host_to_target_semarray() > to g_free() and calls g_try_malloc(count) instead of=20 > g_try_malloc(sizeof(count)) > > v2->v3 used g_try_new() and friends to avoid overflow issues > > v3->v4 use g_free for unlock_iovec() and host_to_target_semarray(). > > v4->v5 one missing malloc() is converted and one converted is fixed. > > v5->v6 new improved commit description. > > --- > linux-user/syscall.c | 28 ++++++++++++++-------------- > 1 file changed, 14 insertions(+), 14 deletions(-) > > diff --git a/linux-user/syscall.c b/linux-user/syscall.c > index 98b5766..267aaa8 100644 > --- a/linux-user/syscall.c > +++ b/linux-user/syscall.c > @@ -1559,7 +1559,7 @@ set_timeout: > } > =20 > fprog.len =3D tswap16(tfprog->len); > - filter =3D malloc(fprog.len * sizeof(*filter)); > + filter =3D g_try_new(struct sock_filter, fprog.len); > if (filter =3D=3D NULL) { > unlock_user_struct(tfilter, tfprog->filter, 1); > unlock_user_struct(tfprog, optval_addr, 1); > @@ -1575,7 +1575,7 @@ set_timeout: > =20 > ret =3D get_errno(setsockopt(sockfd, SOL_SOCKET, > SO_ATTACH_FILTER, &fprog, sizeof(fprog)));= > - free(filter); > + g_free(filter); > =20 > unlock_user_struct(tfilter, tfprog->filter, 1); > unlock_user_struct(tfprog, optval_addr, 1); > @@ -1886,7 +1886,7 @@ static struct iovec *lock_iovec(int type,=20 > abi_ulong target_addr, > return NULL; > } > =20 > - vec =3D calloc(count, sizeof(struct iovec)); > + vec =3D g_try_new0(struct iovec, count); > if (vec =3D=3D NULL) { > errno =3D ENOMEM; > return NULL; > @@ -1950,7 +1950,7 @@ static struct iovec *lock_iovec(int type,=20 > abi_ulong target_addr, > } > unlock_user(target_vec, target_addr, 0); > fail2: > - free(vec); > + g_free(vec); > errno =3D err; > return NULL; > } > @@ -1975,7 +1975,7 @@ static void unlock_iovec(struct iovec=20 > *vec, abi_ulong target_addr, > unlock_user(target_vec, target_addr, 0); > } > =20 > - free(vec); > + g_free(vec); > } > =20 > static inline int target_to_host_sock_type(int *type) > @@ -2677,14 +2677,14 @@ static inline abi_long=20 > target_to_host_semarray(int semid, unsigned short **host_ > =20 > nsems =3D semid_ds.sem_nsems; > =20 > - *host_array =3D malloc(nsems*sizeof(unsigned short)); > + *host_array =3D g_try_new(unsigned short, nsems); > if (!*host_array) { > return -TARGET_ENOMEM; > } > array =3D lock_user(VERIFY_READ, target_addr, > nsems*sizeof(unsigned short), 1); > if (!array) { > - free(*host_array); > + g_free(*host_array); > return -TARGET_EFAULT; > } > =20 > @@ -2721,7 +2721,7 @@ static inline abi_long=20 > host_to_target_semarray(int semid, abi_ulong target_addr, > for(i=3D0; i __put_user((*host_array)[i], &array[i]); > } > - free(*host_array); > + g_free(*host_array); > unlock_user(array, target_addr, 1); > =20 > return 0; > @@ -2980,7 +2980,7 @@ static inline abi_long do_msgsnd(int=20 > msqid, abi_long msgp, > =20 > if (!lock_user_struct(VERIFY_READ, target_mb, msgp, 0)) > return -TARGET_EFAULT; > - host_mb =3D malloc(msgsz+sizeof(long)); > + host_mb =3D g_try_malloc(msgsz + sizeof(long)); > if (!host_mb) { > unlock_user_struct(target_mb, msgp, 0); > return -TARGET_ENOMEM; > @@ -2988,7 +2988,7 @@ static inline abi_long do_msgsnd(int=20 > msqid, abi_long msgp, > host_mb->mtype =3D (abi_long) tswapal(target_mb->mtype); > memcpy(host_mb->mtext, target_mb->mtext, msgsz); > ret =3D get_errno(msgsnd(msqid, host_mb, msgsz, msgflg)); > - free(host_mb); > + g_free(host_mb); > unlock_user_struct(target_mb, msgp, 0); > =20 > return ret; > @@ -3416,7 +3416,7 @@ static abi_long=20 > do_ioctl_fs_ioc_fiemap(const IOCTLEntry *ie, uint8_t *buf_temp, > /* We can't fit all the extents into the fixed size buffer. > * Allocate one that is large enough and use it instead. > */ > - fm =3D malloc(outbufsz); > + fm =3D g_try_malloc(outbufsz); > if (!fm) { > return -TARGET_ENOMEM; > } > @@ -3451,7 +3451,7 @@ static abi_long=20 > do_ioctl_fs_ioc_fiemap(const IOCTLEntry *ie, uint8_t *buf_temp, > } > } > if (free_fm) { > - free(fm); > + g_free(fm); > } > return ret; > } > @@ -7723,7 +7723,7 @@ abi_long do_syscall(void *cpu_env, int=20 > num, abi_long arg1, > struct linux_dirent *dirp; > abi_long count =3D arg3; > =20 > - dirp =3D malloc(count); > + dirp =3D g_try_malloc(count); > if (!dirp) { > ret =3D -TARGET_ENOMEM; > goto fail; > @@ -7760,7 +7760,7 @@ abi_long do_syscall(void *cpu_env, int=20 > num, abi_long arg1, > ret =3D count1; > unlock_user(target_dirp, arg2, ret); > } > - free(dirp); > + g_free(dirp); > } > #else > {