[PATCH v2] target/xtensa: Replace malloc() with g_strdup_printf()

Dmitry Frolov posted 1 patch 3 months, 2 weeks ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20250730062142.1665980-1-frolov@swemel.ru
Maintainers: Max Filippov <jcmvbkbc@gmail.com>
target/xtensa/translate.c | 11 +++--------
1 file changed, 3 insertions(+), 8 deletions(-)
[PATCH v2] target/xtensa: Replace malloc() with g_strdup_printf()
Posted by Dmitry Frolov 3 months, 2 weeks ago
malloc() return value is used without a check.

Found by Linux Verification Center (linuxtesting.org) with SVACE.

v2: Also replaced strdup() with g_strdup()

Signed-off-by: Dmitry Frolov <frolov@swemel.ru>
---
 target/xtensa/translate.c | 11 +++--------
 1 file changed, 3 insertions(+), 8 deletions(-)

diff --git a/target/xtensa/translate.c b/target/xtensa/translate.c
index 34ae2f4e16..4faa5ae213 100644
--- a/target/xtensa/translate.c
+++ b/target/xtensa/translate.c
@@ -112,17 +112,12 @@ void xtensa_collect_sr_names(const XtensaConfig *config)
 
             if (*pname) {
                 if (strstr(*pname, name) == NULL) {
-                    char *new_name =
-                        malloc(strlen(*pname) + strlen(name) + 2);
-
-                    strcpy(new_name, *pname);
-                    strcat(new_name, "/");
-                    strcat(new_name, name);
-                    free(*pname);
+                    char *new_name = g_strdup_printf("%s/%s", *pname, name);
+                    g_free(*pname);
                     *pname = new_name;
                 }
             } else {
-                *pname = strdup(name);
+                *pname = g_strdup(name);
             }
         }
     }
-- 
2.34.1
Re: [PATCH v2] target/xtensa: Replace malloc() with g_strdup_printf()
Posted by Max Filippov 3 months, 2 weeks ago
On Tue, Jul 29, 2025 at 11:22 PM Dmitry Frolov <frolov@swemel.ru> wrote:
>
> malloc() return value is used without a check.
>
> Found by Linux Verification Center (linuxtesting.org) with SVACE.
>
> v2: Also replaced strdup() with g_strdup()
>
> Signed-off-by: Dmitry Frolov <frolov@swemel.ru>
> ---
>  target/xtensa/translate.c | 11 +++--------
>  1 file changed, 3 insertions(+), 8 deletions(-)

Reviewed-by: Max Filippov <jcmvbkbc@gmail.com>

-- 
Thanks.
-- Max