Skip to contentSkip to conversation

xtensa: use g_malloc/g_new/g_free

PATCH In review Paolo Bonzini 4 participants 4 messages May 8, 2017

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xtensa: use g_malloc/g_new/g_free

Replace malloc/free with g_malloc/g_free to get a program exit on out of memory.

Replace g_malloc with g_new when allocating the MemoryRegion to get more type safety.

Reported by Coverity.

---
 hw/xtensa/sim.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
hw/xtensa/sim.c +3 -3
index d2d1d3a6fd..97307728f7 100644
@@ -41,13 +41,13 @@ static void xtensa_create_memory_regions(const XtensaMemory *memory,
                                          const char *name)
 {
     unsigned i;
-    char *num_name = malloc(strlen(name) + sizeof(i) * 3 + 1);
+    char *num_name = g_malloc(strlen(name) + sizeof(i) * 3 + 1);
 
     for (i = 0; i < memory->num; ++i) {
         MemoryRegion *m;
 
         sprintf(num_name, "%s%u", name, i);
-        m = g_malloc(sizeof(*m));
+        m = g_new(MemoryRegion, 1);
         memory_region_init_ram(m, NULL, num_name,
                                memory->location[i].size,
                                &error_fatal);
@@ -55,7 +55,7 @@ static void xtensa_create_memory_regions(const XtensaMemory *memory,
         memory_region_add_subregion(get_system_memory(),
                                     memory->location[i].addr, m);
     }
-    free(num_name);
+    g_free(num_name);
 }
 
 static uint64_t translate_phys_addr(void *opaque, uint64_t addr)
Signature · 1 line
2.12.2
Reviewed-by on v1 1/1
#1 lore raw
Quoted 11 lines · Paolo Bonzini

Replace malloc/free with g_malloc/g_free to get a program exit on out of memory.

Replace g_malloc with g_new when allocating the MemoryRegion to get more type safety.

Reported by Coverity.

Quoted 34 lines
---
 hw/xtensa/sim.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
hw/xtensa/sim.c +3 -3
index d2d1d3a6fd..97307728f7 100644
@@ -41,13 +41,13 @@ static void xtensa_create_memory_regions(const XtensaMemory *memory,
                                          const char *name)
 {
     unsigned i;
-    char *num_name = malloc(strlen(name) + sizeof(i) * 3 + 1);
+    char *num_name = g_malloc(strlen(name) + sizeof(i) * 3 + 1);
 
     for (i = 0; i < memory->num; ++i) {
         MemoryRegion *m;
 
         sprintf(num_name, "%s%u", name, i);
-        m = g_malloc(sizeof(*m));
+        m = g_new(MemoryRegion, 1);
         memory_region_init_ram(m, NULL, num_name,
                                memory->location[i].size,
                                &error_fatal);
@@ -55,7 +55,7 @@ static void xtensa_create_memory_regions(const XtensaMemory *memory,
         memory_region_add_subregion(get_system_memory(),
                                     memory->location[i].addr, m);
     }
-    free(num_name);
+    g_free(num_name);
 }
 
 static uint64_t translate_phys_addr(void *opaque, uint64_t addr)
#2 lore raw
In reply to Paolo Bonzini
Quoted 20 lines · Paolo Bonzini

Replace malloc/free with g_malloc/g_free to get a program exit on out of memory.

Replace g_malloc with g_new when allocating the MemoryRegion to get more type safety.

Reported by Coverity.

---
 hw/xtensa/sim.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
hw/xtensa/sim.c +0 -0
index d2d1d3a6fd..97307728f7 100644

hw/xtensa/sim.c static void xtensa_create_memory_regions(const XtensaMemory *memory, · line 44

Quoted 5 lines
hw/xtensa/sim.c +1 -1
@@ -41,13 +41,13 @@ static void xtensa_create_memory_regions(const XtensaMemory *memory,
                                          const char *name)
 {
     unsigned i;
-    char *num_name = malloc(strlen(name) + sizeof(i) * 3 + 1);
+    char *num_name = g_malloc(strlen(name) + sizeof(i) * 3 + 1);

It seems a little sketchy using the size of the storage type as a proxy for the number of bytes used by the format specifier in a format string. In this sort of situation I'd just use Glib and stop worrying about it:

  GString num_name = g_string_new(NULL);
  ...
      g_string_printf(num_name, "%s%u", name, i);
      m = g_new(MemoryRegion, 1);
      memory_region_init_ram(m, NULL, num_name->str,
                             memory->location[i].size,
                             &error_fatal);

      ...
  g_string_free(name, TRUE);
Quoted 11 lines
hw/xtensa/sim.c +1 -1
     for (i = 0; i < memory->num; ++i) {
         MemoryRegion *m;
 
         sprintf(num_name, "%s%u", name, i);
-        m = g_malloc(sizeof(*m));
+        m = g_new(MemoryRegion, 1);
         memory_region_init_ram(m, NULL, num_name,
                                memory->location[i].size,
                                &error_fatal);

hw/xtensa/sim.c static void xtensa_create_memory_regions(const XtensaMemory *memory, · line 61

Quoted 8 lines
hw/xtensa/sim.c +1 -1
@@ -55,7 +55,7 @@ static void xtensa_create_memory_regions(const XtensaMemory *memory,
         memory_region_add_subregion(get_system_memory(),
                                     memory->location[i].addr, m);
     }
-    free(num_name);
+    g_free(num_name);
 }
 
 static uint64_t translate_phys_addr(void *opaque, uint64_t addr)

--
Alex Bennée

#3 lore raw
Quoted 6 lines · Alex Bennée
Quoted 6 lines
hw/xtensa/sim.c +0 -0
index d2d1d3a6fd..97307728f7 100644
@@ -41,13 +41,13 @@ static void xtensa_create_memory_regions(const XtensaMemory *memory,
                                          const char *name)
Quoted 8 lines

hw/xtensa/sim.c static void xtensa_create_memory_regions(const XtensaMemory *memory, · line 44

Quoted 4 lines
hw/xtensa/sim.c +1 -1
 {
     unsigned i;
-    char *num_name = malloc(strlen(name) + sizeof(i) * 3 + 1);
+    char *num_name = g_malloc(strlen(name) + sizeof(i) * 3 + 1);

It seems a little sketchy using the size of the storage type as a proxy for the number of bytes used by the format specifier in a format string. In this sort of situation I'd just use Glib and stop worrying about it:

Ok, I'll send a patch doing it that way.

Signature · 2 lines
Thanks.
-- Max