Skip to contentSkip to conversation

semihosting/uaccess: Convert malloc, free to g_try_malloc, g_free

PATCH In review jiputer 2 participants 2 messages Nov 8 – Nov 9

lore
Share & tools
lore

jiputer

author
#0 lore raw

PATCH 1/1 semihosting/uaccess: Convert malloc, free to g_try_malloc, g_free

Convert malloc to g_try_malloc and free to g_free in uaccess_lock_user.
Convert free to g_free in uaccess_unlock_user.

---

Something to note, when len == 0 in uaccess_lock_user, it will return a NULL pointer, instead of a non-null pointer from malloc. Any read or writes on *p will report EFAULT since it would be considered a bad address.

Patch is for this issue:
https://gitlab.com/qemu-project/qemu/-/work_items/1798

 semihosting/uaccess.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
semihosting/uaccess.c +3 -3
index ff944d8c2f..71d7cc9006 100644
@@ -17,10 +17,10 @@
 void *uaccess_lock_user(CPUArchState *env, vaddr addr,
                         size_t len, bool copy)
 {
-    void *p = malloc(len);
+    void *p = g_try_malloc(len);
     if (p && copy) {
         if (cpu_memory_rw_debug(env_cpu(env), addr, p, len, 0)) {
-            free(p);
+            g_free(p);
             p = NULL;
         }
     }
@@ -90,5 +90,5 @@ void uaccess_unlock_user(CPUArchState *env, void *p,
     if (len) {
         cpu_memory_rw_debug(env_cpu(env), addr, p, len, 1);
     }
-    free(p);
+    g_free(p);
 }
Signature · 1 line
2.43.0
#1 lore raw
Quoted 5 lines · jiputer

Convert malloc to g_try_malloc and free to g_free in uaccess_lock_user.
Convert free to g_free in uaccess_unlock_user.

Quoted 7 lines

---

Something to note, when len == 0 in uaccess_lock_user, it will return a NULL pointer, instead of a non-null pointer from malloc. Any read or writes on *p will report EFAULT since it would be considered a bad address.

Do you have any concrete examples of where this is a behaviour change for callers to this function? I don't think we ought to rely on malloc(0) behaviour, but if we do somewhere it might be wise to fix that up first...

thanks
-- PMM