diff mbox

[6/6] softmmu-semi: fix lock_user* functions not to deref NULL upon OOM

Message ID 1337173681-25891-7-git-send-email-jim@meyering.net
State New
Headers show

Commit Message

Jim Meyering May 16, 2012, 1:08 p.m. UTC
From: Jim Meyering <meyering@redhat.com>

Use g_malloc/g_free in place of malloc/free.

Signed-off-by: Jim Meyering <meyering@redhat.com>
---
 softmmu-semi.h | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

Comments

Matthew Fernandez May 19, 2012, 7:13 a.m. UTC | #1
On 16 May 2012 23:08, Jim Meyering <jim@meyering.net> wrote:
> From: Jim Meyering <meyering@redhat.com>
>
> Use g_malloc/g_free in place of malloc/free.
>
> Signed-off-by: Jim Meyering <meyering@redhat.com>

Acked-by: Matthew Fernandez <matthew.fernandez@gmail.com>

> ---
>  softmmu-semi.h | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)
>
> diff --git a/softmmu-semi.h b/softmmu-semi.h
> index 648cb95..996e0f7 100644
> --- a/softmmu-semi.h
> +++ b/softmmu-semi.h
> @@ -39,7 +39,7 @@ static void *softmmu_lock_user(CPUArchState *env, uint32_t addr, uint32_t len,
>  {
>     uint8_t *p;
>     /* TODO: Make this something that isn't fixed size.  */
> -    p = malloc(len);
> +    p = g_malloc(len);
>     if (copy)
>         cpu_memory_rw_debug(env, addr, p, len, 0);
>     return p;
> @@ -51,7 +51,7 @@ static char *softmmu_lock_user_string(CPUArchState *env, uint32_t addr)
>     char *s;
>     uint8_t c;
>     /* TODO: Make this something that isn't fixed size.  */
> -    s = p = malloc(1024);
> +    s = p = g_malloc(1024);
>     do {
>         cpu_memory_rw_debug(env, addr, &c, 1, 0);
>         addr++;
> @@ -65,6 +65,6 @@ static void softmmu_unlock_user(CPUArchState *env, void *p, target_ulong addr,
>  {
>     if (len)
>         cpu_memory_rw_debug(env, addr, p, len, 1);
> -    free(p);
> +    g_free(p);
>  }
>  #define unlock_user(s, args, len) softmmu_unlock_user(env, s, args, len)
> --
> 1.7.10.2.520.g6a4a482
>
Peter Maydell May 19, 2012, 3:46 p.m. UTC | #2
On 16 May 2012 14:08, Jim Meyering <jim@meyering.net> wrote:
> From: Jim Meyering <meyering@redhat.com>
>
> Use g_malloc/g_free in place of malloc/free.
>
> Signed-off-by: Jim Meyering <meyering@redhat.com>
> ---
>  softmmu-semi.h | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)
>
> diff --git a/softmmu-semi.h b/softmmu-semi.h
> index 648cb95..996e0f7 100644
> --- a/softmmu-semi.h
> +++ b/softmmu-semi.h
> @@ -39,7 +39,7 @@ static void *softmmu_lock_user(CPUArchState *env, uint32_t addr, uint32_t len,
>  {
>     uint8_t *p;
>     /* TODO: Make this something that isn't fixed size.  */
> -    p = malloc(len);
> +    p = g_malloc(len);
>     if (copy)
>         cpu_memory_rw_debug(env, addr, p, len, 0);
>     return p;

Nak. This function is called with a length passed from the guest, so
killing qemu if the length is too large is a bad idea. The callers
should handle it returning NULL on failure. (Most of them do already,
if any do not that's a bug.) The bug in this function is passing
NULL to cpu_memory_rw_debug().

-- PMM
Jim Meyering May 24, 2012, 2:46 p.m. UTC | #3
Peter Maydell wrote:

> On 16 May 2012 14:08, Jim Meyering <jim@meyering.net> wrote:
>> From: Jim Meyering <meyering@redhat.com>
>>
>> Use g_malloc/g_free in place of malloc/free.
>>
>> Signed-off-by: Jim Meyering <meyering@redhat.com>
>> ---
>>  softmmu-semi.h | 6 +++---
>>  1 file changed, 3 insertions(+), 3 deletions(-)
>>
>> diff --git a/softmmu-semi.h b/softmmu-semi.h
>> index 648cb95..996e0f7 100644
>> --- a/softmmu-semi.h
>> +++ b/softmmu-semi.h
>> @@ -39,7 +39,7 @@ static void *softmmu_lock_user(CPUArchState *env,
>> uint32_t addr, uint32_t len,
>>  {
>>     uint8_t *p;
>>     /* TODO: Make this something that isn't fixed size.  */
>> -    p = malloc(len);
>> +    p = g_malloc(len);
>>     if (copy)
>>         cpu_memory_rw_debug(env, addr, p, len, 0);
>>     return p;
>
> Nak. This function is called with a length passed from the guest, so
> killing qemu if the length is too large is a bad idea. The callers
> should handle it returning NULL on failure. (Most of them do already,
> if any do not that's a bug.) The bug in this function is passing
> NULL to cpu_memory_rw_debug().

That makes sense.
I've adjusted as you suggest and posted a V2.
diff mbox

Patch

diff --git a/softmmu-semi.h b/softmmu-semi.h
index 648cb95..996e0f7 100644
--- a/softmmu-semi.h
+++ b/softmmu-semi.h
@@ -39,7 +39,7 @@  static void *softmmu_lock_user(CPUArchState *env, uint32_t addr, uint32_t len,
 {
     uint8_t *p;
     /* TODO: Make this something that isn't fixed size.  */
-    p = malloc(len);
+    p = g_malloc(len);
     if (copy)
         cpu_memory_rw_debug(env, addr, p, len, 0);
     return p;
@@ -51,7 +51,7 @@  static char *softmmu_lock_user_string(CPUArchState *env, uint32_t addr)
     char *s;
     uint8_t c;
     /* TODO: Make this something that isn't fixed size.  */
-    s = p = malloc(1024);
+    s = p = g_malloc(1024);
     do {
         cpu_memory_rw_debug(env, addr, &c, 1, 0);
         addr++;
@@ -65,6 +65,6 @@  static void softmmu_unlock_user(CPUArchState *env, void *p, target_ulong addr,
 {
     if (len)
         cpu_memory_rw_debug(env, addr, p, len, 1);
-    free(p);
+    g_free(p);
 }
 #define unlock_user(s, args, len) softmmu_unlock_user(env, s, args, len)