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

jiputer posted 1 patch 2 weeks ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20261108193423.225720-1-jpaulma23@gmail.com
Maintainers: "Alex Bennée" <alex.bennee@linaro.org>
semihosting/uaccess.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
[PATCH] semihosting/uaccess: Convert malloc, free to g_try_malloc, g_free
Posted by jiputer 2 weeks ago
Convert malloc to g_try_malloc and free to g_free in
uaccess_lock_user.
Convert free to g_free in uaccess_unlock_user.

Signed-off-by: jiputer <jpaulma23@gmail.com>
---

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(-)

diff --git a/semihosting/uaccess.c b/semihosting/uaccess.c
index ff944d8c2f..71d7cc9006 100644
--- a/semihosting/uaccess.c
+++ b/semihosting/uaccess.c
@@ -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);
 }
-- 
2.43.0
Re: [PATCH] semihosting/uaccess: Convert malloc, free to g_try_malloc, g_free
Posted by Peter Maydell 1 week, 6 days ago
On Sun, 8 Nov 2026 at 20:39, jiputer <jpaulma23@gmail.com> wrote:
>
> Convert malloc to g_try_malloc and free to g_free in
> uaccess_lock_user.
> Convert free to g_free in uaccess_unlock_user.
>
> Signed-off-by: jiputer <jpaulma23@gmail.com>
> ---
>
> 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