diff mbox

[v4,06/10] slirp/misc: check return value of malloc()

Message ID 1407489672-12212-7-git-send-email-zhang.zhanghailiang@huawei.com
State New
Headers show

Commit Message

Zhanghailiang Aug. 8, 2014, 9:21 a.m. UTC
Signed-off-by: zhanghailiang <zhang.zhanghailiang@huawei.com>
---
 slirp/misc.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

Comments

Alex Bennée Aug. 8, 2014, 9:43 a.m. UTC | #1
zhanghailiang writes:

> Signed-off-by: zhanghailiang <zhang.zhanghailiang@huawei.com>
> ---
>  slirp/misc.c | 9 +++++++--
>  1 file changed, 7 insertions(+), 2 deletions(-)
>
> diff --git a/slirp/misc.c b/slirp/misc.c
> index b8eb74c..9b457ad 100644
> --- a/slirp/misc.c
> +++ b/slirp/misc.c
> @@ -55,6 +55,10 @@ int add_exec(struct ex_list **ex_ptr, int do_pty, char *exec,
>  
>  	tmp_ptr = *ex_ptr;
>  	*ex_ptr = (struct ex_list *)malloc(sizeof(struct ex_list));
> +    if (*ex_ptr == NULL) {
> +        fprintf(stderr, "Error: malloc failed\n");
> +        return -1;
> +    }

Your indenting has gone a bit weird there.

>  	(*ex_ptr)->ex_fport = port;
>  	(*ex_ptr)->ex_addr = addr;
>  	(*ex_ptr)->ex_pty = do_pty;
> @@ -236,8 +240,9 @@ strdup(str)
>  	char *bptr;
>  
>  	bptr = (char *)malloc(strlen(str)+1);
> -	strcpy(bptr, str);
> -
> +    if (bptr) {
> +        strcpy(bptr, str);
> +    }
>  	return bptr;
>  }
>  #endif

Again use of g_malloc would remove the need for this. HACKING section 3
says:

3. Low level memory management

Use of the malloc/free/realloc/calloc/valloc/memalign/posix_memalign
APIs is not allowed in the QEMU codebase. Instead of these routines,
use the GLib memory allocation routines g_malloc/g_malloc0/g_new/
g_new0/g_realloc/g_free or QEMU's qemu_memalign/qemu_blockalign/qemu_vfree
APIs.

Please note that g_malloc will exit on allocation failure, so there
is no need to test for failure (as you would have to with malloc).
Calling g_malloc with a zero size is valid and will return NULL.
Zhanghailiang Aug. 8, 2014, 10:44 a.m. UTC | #2
On 2014/8/8 17:43, Alex Bennée wrote:
>
> zhanghailiang writes:
>
>> Signed-off-by: zhanghailiang<zhang.zhanghailiang@huawei.com>
>> ---
>>   slirp/misc.c | 9 +++++++--
>>   1 file changed, 7 insertions(+), 2 deletions(-)
>>
>> diff --git a/slirp/misc.c b/slirp/misc.c
>> index b8eb74c..9b457ad 100644
>> --- a/slirp/misc.c
>> +++ b/slirp/misc.c
>> @@ -55,6 +55,10 @@ int add_exec(struct ex_list **ex_ptr, int do_pty, char *exec,
>>
>>   	tmp_ptr = *ex_ptr;
>>   	*ex_ptr = (struct ex_list *)malloc(sizeof(struct ex_list));
>> +    if (*ex_ptr == NULL) {
>> +        fprintf(stderr, "Error: malloc failed\n");
>> +        return -1;
>> +    }
>
> Your indenting has gone a bit weird there.

Hmm, this file has some places that use tab key as indent.
Here i used spaces as indent, otherwise the patch can not pass the check 
of '/scripts/checkpatch.pl'.

What's your opinion? Use tab as what it does? Thanks!

>
>>   	(*ex_ptr)->ex_fport = port;
>>   	(*ex_ptr)->ex_addr = addr;
>>   	(*ex_ptr)->ex_pty = do_pty;
>> @@ -236,8 +240,9 @@ strdup(str)
>>   	char *bptr;
>>
>>   	bptr = (char *)malloc(strlen(str)+1);
>> -	strcpy(bptr, str);
>> -
>> +    if (bptr) {
>> +        strcpy(bptr, str);
>> +    }
>>   	return bptr;
>>   }
>>   #endif
>
> Again use of g_malloc would remove the need for this. HACKING section 3
> says:
>

OK, Thanks!

> 3. Low level memory management
>
> Use of the malloc/free/realloc/calloc/valloc/memalign/posix_memalign
> APIs is not allowed in the QEMU codebase. Instead of these routines,
> use the GLib memory allocation routines g_malloc/g_malloc0/g_new/
> g_new0/g_realloc/g_free or QEMU's qemu_memalign/qemu_blockalign/qemu_vfree
> APIs.
>
> Please note that g_malloc will exit on allocation failure, so there
> is no need to test for failure (as you would have to with malloc).
> Calling g_malloc with a zero size is valid and will return NULL.
>
>
Alex Bennée Aug. 8, 2014, 1:24 p.m. UTC | #3
zhanghailiang writes:

> On 2014/8/8 17:43, Alex Bennée wrote:
>>
>> zhanghailiang writes:
>>
>>> Signed-off-by: zhanghailiang<zhang.zhanghailiang@huawei.com>
>>> ---
>>>   slirp/misc.c | 9 +++++++--
>>>   1 file changed, 7 insertions(+), 2 deletions(-)
>>>
<snip>
>>
>> Your indenting has gone a bit weird there.
>
> Hmm, this file has some places that use tab key as indent.
> Here i used spaces as indent, otherwise the patch can not pass the check 
> of '/scripts/checkpatch.pl'.
>
> What's your opinion? Use tab as what it does? Thanks!

Welcome to the world of QEMU's inconsistent whitespace ;-)

You have two choices:

  * two patches: 1st to clean up whitespace for that function, 2nd to
    fix
  * keep to using tabs for that particular fix

Eventually the code base will get to a consistent state we hope...

>>>   	(*ex_ptr)->ex_fport = port;
>>>   	(*ex_ptr)->ex_addr = addr;
>>>   	(*ex_ptr)->ex_pty = do_pty;
>>> @@ -236,8 +240,9 @@ strdup(str)
>>>   	char *bptr;
>>>
>>>   	bptr = (char *)malloc(strlen(str)+1);
>>> -	strcpy(bptr, str);
>>> -
>>> +    if (bptr) {
>>> +        strcpy(bptr, str);
>>> +    }
>>>   	return bptr;
>>>   }
>>>   #endif
>>
>> Again use of g_malloc would remove the need for this. HACKING section 3
>> says:
>>
>
> OK, Thanks!
>
>> 3. Low level memory management
>>
>> Use of the malloc/free/realloc/calloc/valloc/memalign/posix_memalign
>> APIs is not allowed in the QEMU codebase. Instead of these routines,
>> use the GLib memory allocation routines g_malloc/g_malloc0/g_new/
>> g_new0/g_realloc/g_free or QEMU's qemu_memalign/qemu_blockalign/qemu_vfree
>> APIs.
>>
>> Please note that g_malloc will exit on allocation failure, so there
>> is no need to test for failure (as you would have to with malloc).
>> Calling g_malloc with a zero size is valid and will return NULL.
>>
>>
Zhanghailiang Aug. 11, 2014, 7:18 a.m. UTC | #4
On 2014/8/8 21:24, Alex Bennée wrote:
>
> zhanghailiang writes:
>
>> On 2014/8/8 17:43, Alex Bennée wrote:
>>>
>>> zhanghailiang writes:
>>>
>>>> Signed-off-by: zhanghailiang<zhang.zhanghailiang@huawei.com>
>>>> ---
>>>>    slirp/misc.c | 9 +++++++--
>>>>    1 file changed, 7 insertions(+), 2 deletions(-)
>>>>
> <snip>
>>>
>>> Your indenting has gone a bit weird there.
>>
>> Hmm, this file has some places that use tab key as indent.
>> Here i used spaces as indent, otherwise the patch can not pass the check
>> of '/scripts/checkpatch.pl'.
>>
>> What's your opinion? Use tab as what it does? Thanks!
>
> Welcome to the world of QEMU's inconsistent whitespace ;-)
>
> You have two choices:
>
>    * two patches: 1st to clean up whitespace for that function, 2nd to
>      fix
>    * keep to using tabs for that particular fix
>
> Eventually the code base will get to a consistent state we hope...
>

OK, I will choose the second way! Thanks, Alex.

>>>>    	(*ex_ptr)->ex_fport = port;
>>>>    	(*ex_ptr)->ex_addr = addr;
>>>>    	(*ex_ptr)->ex_pty = do_pty;
>>>> @@ -236,8 +240,9 @@ strdup(str)
>>>>    	char *bptr;
>>>>
>>>>    	bptr = (char *)malloc(strlen(str)+1);
>>>> -	strcpy(bptr, str);
>>>> -
>>>> +    if (bptr) {
>>>> +        strcpy(bptr, str);
>>>> +    }
>>>>    	return bptr;
>>>>    }
>>>>    #endif
>>>
>>> Again use of g_malloc would remove the need for this. HACKING section 3
>>> says:
>>>
>>
>> OK, Thanks!
>>
>>> 3. Low level memory management
>>>
>>> Use of the malloc/free/realloc/calloc/valloc/memalign/posix_memalign
>>> APIs is not allowed in the QEMU codebase. Instead of these routines,
>>> use the GLib memory allocation routines g_malloc/g_malloc0/g_new/
>>> g_new0/g_realloc/g_free or QEMU's qemu_memalign/qemu_blockalign/qemu_vfree
>>> APIs.
>>>
>>> Please note that g_malloc will exit on allocation failure, so there
>>> is no need to test for failure (as you would have to with malloc).
>>> Calling g_malloc with a zero size is valid and will return NULL.
>>>
>>>
>
diff mbox

Patch

diff --git a/slirp/misc.c b/slirp/misc.c
index b8eb74c..9b457ad 100644
--- a/slirp/misc.c
+++ b/slirp/misc.c
@@ -55,6 +55,10 @@  int add_exec(struct ex_list **ex_ptr, int do_pty, char *exec,
 
 	tmp_ptr = *ex_ptr;
 	*ex_ptr = (struct ex_list *)malloc(sizeof(struct ex_list));
+    if (*ex_ptr == NULL) {
+        fprintf(stderr, "Error: malloc failed\n");
+        return -1;
+    }
 	(*ex_ptr)->ex_fport = port;
 	(*ex_ptr)->ex_addr = addr;
 	(*ex_ptr)->ex_pty = do_pty;
@@ -236,8 +240,9 @@  strdup(str)
 	char *bptr;
 
 	bptr = (char *)malloc(strlen(str)+1);
-	strcpy(bptr, str);
-
+    if (bptr) {
+        strcpy(bptr, str);
+    }
 	return bptr;
 }
 #endif