cppcheck report: gdbstub.c:1781: error: Memory leak: s Rearranging of the code avoids the leak.
The patch also slightly cleans the g_malloc0 statement which was touched by that change (no type cast, easier code review). Signed-off-by: Stefan Weil <s...@weilnetz.de> --- gdbstub.c | 14 ++++++++------ 1 files changed, 8 insertions(+), 6 deletions(-) diff --git a/gdbstub.c b/gdbstub.c index 4009058..34746f2 100644 --- a/gdbstub.c +++ b/gdbstub.c @@ -1768,12 +1768,6 @@ void gdb_register_coprocessor(CPUState * env, GDBRegisterState **p; static int last_reg = NUM_CORE_REGS; - s = (GDBRegisterState *)g_malloc0(sizeof(GDBRegisterState)); - s->base_reg = last_reg; - s->num_regs = num_regs; - s->get_reg = get_reg; - s->set_reg = set_reg; - s->xml = xml; p = &env->gdb_regs; while (*p) { /* Check for duplicates. */ @@ -1781,6 +1775,14 @@ void gdb_register_coprocessor(CPUState * env, return; p = &(*p)->next; } + + s = g_malloc0(sizeof(*s)); + s->base_reg = last_reg; + s->num_regs = num_regs; + s->get_reg = get_reg; + s->set_reg = set_reg; + s->xml = xml; + /* Add to end of list. */ last_reg += num_regs; *p = s; -- 1.7.2.5