From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:34976) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XFhXf-0005vu-UF for qemu-devel@nongnu.org; Fri, 08 Aug 2014 06:37:04 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1XFhXb-0002Eu-46 for qemu-devel@nongnu.org; Fri, 08 Aug 2014 06:36:59 -0400 Received: from szxga03-in.huawei.com ([119.145.14.66]:3990) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XFhXa-0002EA-HV for qemu-devel@nongnu.org; Fri, 08 Aug 2014 06:36:55 -0400 Message-ID: <53E4A806.8070102@huawei.com> Date: Fri, 8 Aug 2014 18:35:50 +0800 From: zhanghailiang MIME-Version: 1.0 References: <1407489672-12212-1-git-send-email-zhang.zhanghailiang@huawei.com> <1407489672-12212-6-git-send-email-zhang.zhanghailiang@huawei.com> <87oavvjozf.fsf@linaro.org> In-Reply-To: <87oavvjozf.fsf@linaro.org> Content-Type: text/plain; charset="UTF-8"; format=flowed Content-Transfer-Encoding: 8bit Subject: Re: [Qemu-devel] [PATCH v4 05/10] util/path: check return value of malloc() List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: =?UTF-8?B?QWxleCBCZW5uw6ll?= Cc: kwolf@redhat.com, lkurusa@redhat.com, mst@redhat.com, jan.kiszka@siemens.com, riku.voipio@iki.fi, mjt@tls.msk.ru, qemu-devel@nongnu.org, lcapitulino@redhat.com, stefanha@redhat.com, luonengjun@huawei.com, pbonzini@redhat.com, peter.huangpeng@huawei.com, rth@twiddle.net On 2014/8/8 17:36, Alex Bennée wrote: > > zhanghailiang writes: > >> Reviewed-by: Gonglei >> Signed-off-by: zhanghailiang >> --- >> util/path.c | 9 ++++++--- >> 1 file changed, 6 insertions(+), 3 deletions(-) >> >> diff --git a/util/path.c b/util/path.c >> index 5c59d9f..df1653f 100644 >> --- a/util/path.c >> +++ b/util/path.c >> @@ -46,9 +46,12 @@ static struct pathelem *new_entry(const char *root, >> const char *name) >> { >> struct pathelem *new = malloc(sizeof(*new)); >> - new->name = strdup(name); >> - new->pathname = g_strdup_printf("%s/%s", root, name); >> - new->num_entries = 0; > > Erm... isn't that malloc wrong as sizeof(*new) would be the size of a > pointer? > No, this is OK! It is equal to sizeof(struct pathelem). >> + >> + if (new) { >> + new->name = strdup(name); >> + new->pathname = g_strdup_printf("%s/%s", root, name); >> + new->num_entries = 0; >> + } >> return new; >> } > > A better approach may be to just g_malloc which would abort on failure > (which would be fine for setup). > Hmm, Good idea! It is more quickly to know what happen when it fails. I will change to it, Thanks, Alex. > static struct pathelem *new_entry(const char *root, > struct pathelem *parent, > const char *name) > { > struct pathelem *new = g_malloc(sizeof(pathelem)); > new->name = g_strdup(name); > new->pathname = g_strdup_printf("%s/%s", root, name); > new->num_entries = 0; > return new; > } > > >