Skip to contentSkip to conversation

Trivial cleanups around g_malloc()

PATCH Applied Markus Armbruster 2 participants 8 messages Feb 4, 2015 – Feb 8, 2015

lore
Share & tools
lore
#0 lore raw

v2 cover Trivial cleanups around g_malloc()

I'm routing these patches through qemu-trivial, even though some of them touch actively maintained code, and could go through the respective tree instead:

* PATCH 1 block (Kevin, Stefan)

* PATCH 3 KVM (Paolo)

* PATCH 4 migration (Juan, Amit)

* PATCH 5 VNC (Gerd)

If you want me to reroute any of them, let me know.

v2:
* Trivially rebased, R-bys retained

Markus Armbruster (6):
onenand: g_malloc() can't fail, bury dead error handling
rtl8139: g_malloc() can't fail, bury dead error handling
kvm: g_malloc() can't fail, bury dead error handling
rdma: g_malloc0() can't fail, bury dead error handling
vnc: g_realloc() can't fail, bury dead error handling
translate-all: Use g_try_malloc() for dynamic translator buffer

 hw/block/onenand.c |  8 +-------
 hw/net/rtl8139.c   | 14 --------------
 kvm-all.c          |  4 ----
 migration/rdma.c   |  3 ---
 translate-all.c    |  2 +-
 ui/vnc.c           |  4 ----
 6 files changed, 2 insertions(+), 33 deletions(-)
Signature · 1 line
1.9.3
#1 lore raw

PATCH v2 1/6 onenand: g_malloc() can't fail, bury dead error handling

---
 hw/block/onenand.c | 8 +-------
 1 file changed, 1 insertion(+), 7 deletions(-)
hw/block/onenand.c +1 -7
index 348630d..1b2c893 100644
@@ -346,15 +346,9 @@ static inline int onenand_prog_spare(OneNANDState *s, int sec, int secn,
 static inline int onenand_erase(OneNANDState *s, int sec, int num)
 {
     uint8_t *blankbuf, *tmpbuf;
+
     blankbuf = g_malloc(512);
-    if (!blankbuf) {
-        return 1;
-    }
     tmpbuf = g_malloc(512);
-    if (!tmpbuf) {
-        g_free(blankbuf);
-        return 1;
-    }
     memset(blankbuf, 0xff, 512);
     for (; num > 0; num--, sec++) {
         if (s->blk_cur) {
Signature · 1 line
1.9.3
#2 lore raw

PATCH v2 2/6 rtl8139: g_malloc() can't fail, bury dead error handling

In reply to Markus Armbruster
---
 hw/net/rtl8139.c | 14 --------------
 1 file changed, 14 deletions(-)
hw/net/rtl8139.c +0 -14
index 6fa9e0a..b55e438 100644
@@ -2075,20 +2075,6 @@ static int rtl8139_cplus_transmit_one(RTL8139State *s)
                 "length to %d\n", txsize);
     }
 
-    if (!s->cplus_txbuffer)
-    {
-        /* out of memory */
-
-        DPRINTF("+++ C+ mode transmiter failed to reallocate %d bytes\n",
-            s->cplus_txbuffer_len);
-
-        /* update tally counter */
-        ++s->tally_counters.TxERR;
-        ++s->tally_counters.TxAbt;
-
-        return 0;
-    }
-
     /* append more data to the packet */
 
     DPRINTF("+++ C+ mode transmit reading %d bytes from host memory at "
Signature · 1 line
1.9.3
#3 lore raw

PATCH v2 3/6 kvm: g_malloc() can't fail, bury dead error handling

In reply to Markus Armbruster
---
 kvm-all.c | 4 ----
 1 file changed, 4 deletions(-)
kvm-all.c +0 -4
index 2f21a4e..05a79c2 100644
@@ -2070,10 +2070,6 @@ int kvm_insert_breakpoint(CPUState *cpu, target_ulong addr,
         }
 
         bp = g_malloc(sizeof(struct kvm_sw_breakpoint));
-        if (!bp) {
-            return -ENOMEM;
-        }
-
         bp->pc = addr;
         bp->use_count = 1;
         err = kvm_arch_insert_sw_breakpoint(cpu, bp);
Signature · 1 line
1.9.3
#4 lore raw

PATCH v2 4/6 rdma: g_malloc0() can't fail, bury dead error handling

In reply to Markus Armbruster
---
 migration/rdma.c | 3 ---
 1 file changed, 3 deletions(-)
migration/rdma.c +0 -3
index b32dbdf..306570d 100644
@@ -1147,9 +1147,6 @@ static int qemu_rdma_register_and_get_keys(RDMAContext *rdma,
     /* allocate memory to store chunk MRs */
     if (!block->pmr) {
         block->pmr = g_malloc0(block->nb_chunks * sizeof(struct ibv_mr *));
-        if (!block->pmr) {
-            return -1;
-        }
     }
 
     /*
Signature · 1 line
1.9.3
#5 lore raw

PATCH v2 5/6 vnc: g_realloc() can't fail, bury dead error handling

In reply to Markus Armbruster
---
 ui/vnc.c | 4 ----
 1 file changed, 4 deletions(-)
ui/vnc.c +0 -4
index a742c90..02552ee 100644
@@ -659,10 +659,6 @@ void buffer_reserve(Buffer *buffer, size_t len)
     if ((buffer->capacity - buffer->offset) < len) {
         buffer->capacity += (len + 1024);
         buffer->buffer = g_realloc(buffer->buffer, buffer->capacity);
-        if (buffer->buffer == NULL) {
-            fprintf(stderr, "vnc: out of memory\n");
-            exit(1);
-        }
     }
 }
 
Signature · 1 line
1.9.3
#6 lore raw

PATCH v2 6/6 translate-all: Use g_try_malloc() for dynamic translator buffer

In reply to Markus Armbruster

The USE_MMAP code can fail, and the caller handles the failure already. Let the !USE_MMAP code fail as well, for consistency.

---
 translate-all.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
translate-all.c +1 -1
index 4a1b64f..9f47ce7 100644
@@ -631,7 +631,7 @@ static inline void *alloc_code_gen_buffer(void)
 #else
 static inline void *alloc_code_gen_buffer(void)
 {
-    void *buf = g_malloc(tcg_ctx.code_gen_buffer_size);
+    void *buf = g_try_malloc(tcg_ctx.code_gen_buffer_size);
 
     if (buf == NULL) {
         return NULL;
Signature · 1 line
1.9.3
#7 lore raw
In reply to Markus Armbruster
Quoted 17 lines · Markus Armbruster

I'm routing these patches through qemu-trivial, even though some of them touch actively maintained code, and could go through the respective tree instead:

* PATCH 1 block (Kevin, Stefan)

* PATCH 3 KVM (Paolo)

* PATCH 4 migration (Juan, Amit)

* PATCH 5 VNC (Gerd)

If you want me to reroute any of them, let me know.

v2:
* Trivially rebased, R-bys retained

Quoted 7 lines

Markus Armbruster (6):
onenand: g_malloc() can't fail, bury dead error handling
rtl8139: g_malloc() can't fail, bury dead error handling
kvm: g_malloc() can't fail, bury dead error handling
rdma: g_malloc0() can't fail, bury dead error handling
vnc: g_realloc() can't fail, bury dead error handling
translate-all: Use g_try_malloc() for dynamic translator buffer

Applied all to -trivial, thank you!

/mjt