Skip to content

Commit 8c60845

Browse files
authored
Fix memory leaks
1 parent d41623e commit 8c60845

File tree

25 files changed

+73
-40
lines changed

25 files changed

+73
-40
lines changed

libr/anal/dwarf_process.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,10 @@ static st32 parse_type(Context *ctx, const ut64 offset, RStrBuf *strbuf, ut64 *s
272272
}
273273
if (visited && set_u_contains (*visited, offset)) {
274274
R_LOG_WARN ("anal.dwarf.parse_type: infinite recursion detected");
275+
if (root) {
276+
set_u_free (*visited);
277+
free (visited);
278+
}
275279
return -1;
276280
}
277281
set_u_add (*visited, offset);
@@ -483,6 +487,7 @@ static RAnalEnumCase *parse_enumerator(Context *ctx, ut64 idx, RAnalEnumCase *re
483487
RBinDwarfAttrValue *value = &die->attr_values[i];
484488
switch (die->attr_values[i].attr_name) {
485489
case DW_AT_name:
490+
free (name);
486491
name = get_die_name (die);
487492
if (!name) {
488493
goto cleanup;
@@ -497,7 +502,10 @@ static RAnalEnumCase *parse_enumerator(Context *ctx, ut64 idx, RAnalEnumCase *re
497502
}
498503
}
499504

500-
result->name = name;
505+
if (result->name != name) {
506+
free (result->name);
507+
result->name = name;
508+
}
501509
result->val = (int)val;
502510
return result;
503511
cleanup:
@@ -616,7 +624,7 @@ static void parse_enum_type(Context *ctx, ut64 idx) {
616624
base_type->type = r_strbuf_drain_nofree (&strbuf);
617625
}
618626

619-
RAnalEnumCase cas;
627+
RAnalEnumCase cas = {0};
620628
if (die->has_children) {
621629
int child_depth = 1; // Direct children of the node
622630
size_t j;
@@ -634,6 +642,7 @@ static void parse_enum_type(Context *ctx, ut64 idx) {
634642
enum_type_case_free (result, NULL);
635643
goto cleanup;
636644
}
645+
cas.name = NULL;
637646
}
638647
if (child_die->has_children) {
639648
child_depth++;

libr/anal/sign.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1332,6 +1332,8 @@ static double matchBytes(RSignItem *a, RSignItem *b) {
13321332
}
13331333
if (a->bytes->mask) {
13341334
memcpy (combined_mask, a->bytes->mask, min_size);
1335+
} else {
1336+
memset (combined_mask, 0xff, min_size);
13351337
}
13361338
if (b->bytes->mask) {
13371339
int i;

libr/arch/p/arm/armass64.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1618,7 +1618,7 @@ static bool parseOperands(char *str, ArmOp *op) {
16181618
int mem_opt = 0;
16191619
int msr_op_index = 0;
16201620
size_t index_bound = strcspn (t, "]");
1621-
if (!token) {
1621+
if (!t) {
16221622
return false;
16231623
}
16241624

@@ -1634,6 +1634,7 @@ static bool parseOperands(char *str, ArmOp *op) {
16341634
}
16351635
if (operand >= MAX_OPERANDS) {
16361636
R_LOG_ERROR ("Too many operands");
1637+
free (t);
16371638
return false;
16381639
}
16391640
op->operands[operand].type = ARM_NOTYPE;
@@ -1723,6 +1724,7 @@ static bool parseOperands(char *str, ArmOp *op) {
17231724
token++;
17241725
}
17251726
if (!*token || !isdigit ((unsigned char)*token)) {
1727+
free (t);
17261728
return false;
17271729
}
17281730
op->operands[operand].shift_amount = r_num_math (NULL, token);
@@ -1748,6 +1750,7 @@ static bool parseOperands(char *str, ArmOp *op) {
17481750
}
17491751
if (!*token || !isdigit ((unsigned char)*token)) {
17501752
if (present) {
1753+
free (t);
17511754
return false;
17521755
}
17531756
op->operands[operand].shift_amount = 0;

libr/arch/p/cosmac/pseudo.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ static char *parse(RAsmPluginSession *aps, const char *data) {
9494
#endif
9595
const char *op0 = buf;
9696
if (!strcmp (op0, "ret") || !strcmp (op0, "sret")) {
97+
free (buf);
9798
return strdup ("return r0");
9899
}
99100
char *str = malloc (strlen (data) + 128);

libr/arch/p/x86_nz/pseudo.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ static char *parse(RAsmPluginSession *aps, const char *data) {
192192
*w0 = *w1 = *w2 = *w3 = '\0';
193193
if (strchr (data, '(')) {
194194
// avoid double-pseudo calls
195+
free (buf);
195196
return NULL;
196197
}
197198
char *str = NULL;
@@ -378,7 +379,7 @@ static char *patch(RAsmPluginSession *aps, RAnalOp *aop, const char *op) {
378379
R_LOG_ERROR ("Cant fit a nop in here");
379380
return false;
380381
}
381-
char *hcmd = malloc ((size * 2) + 5);
382+
hcmd = malloc ((size * 2) + 5);
382383
if (!hcmd) {
383384
return false;
384385
}

libr/asm/parse.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,9 @@ R_API char *r_asm_parse_immbase(RAsm *a, const char *_opstr, int base) {
101101
}
102102
}
103103
r_strbuf_append (sb, last);
104-
return r_strbuf_drain (sb);
104+
char *result = r_strbuf_drain (sb);
105+
free (opstr);
106+
return result;
105107
}
106108

107109
// TODO : make them internal?

libr/bin/format/elf/elf.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5054,6 +5054,7 @@ static RVecRBinElfSymbol *_load_additional_imported_symbols(ELFOBJ *eo, ImportIn
50545054
if (symbol->is_imported) {
50555055
if (limit > 0 && count++ > limit) {
50565056
R_LOG_WARN ("eo.limit reached for imports");
5057+
free (isym);
50575058
break;
50585059
}
50595060
RVecRBinElfSymbol_push_back (imports, symbol);

libr/bin/format/mach0/mach0.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3099,13 +3099,14 @@ static void parse_symbols(RBinFile *bf, struct MACH0_(obj_t) *mo, HtPP *symcache
30993099
sym->name = r_bin_name_new (symbol.name);
31003100
} else {
31013101
char *name = r_str_newf ("entry%u", (ut32)i);
3102-
sym->name = r_bin_name_new (symbol.name);
3102+
sym->name = r_bin_name_new (name);
31033103
free (name);
31043104
}
31053105
sym->type = symbol.type == R_BIN_MACH0_SYMBOL_TYPE_LOCAL? "LOCAL": "EXT";
31063106
sym->is_imported = symbol.is_imported;
31073107
sym->ordinal = ordinal++;
31083108
_enrich_symbol (bf, mo, symcache, sym);
3109+
free (symbol.name);
31093110
}
31103111
}
31113112

libr/bin/format/objc/mach0_classes.c

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -331,9 +331,6 @@ static void get_ivar_list(RBinFile *bf, RBinClass *klass, mach0_ut p) {
331331
return;
332332
}
333333
field = R_NEW0 (RBinField);
334-
if (!field) {
335-
break;
336-
}
337334
memset (&i, '\0', sizeof (struct MACH0_(SIVar)));
338335
if (r + left < r || r + sizeof (struct MACH0_(SIVar)) < r) {
339336
goto error;
@@ -452,6 +449,8 @@ static void get_ivar_list(RBinFile *bf, RBinClass *klass, mach0_ut p) {
452449
field = NULL;
453450
} else {
454451
R_LOG_WARN ("field name is empty");
452+
r_bin_field_free (field);
453+
field = NULL;
455454
}
456455
} else {
457456
R_LOG_DEBUG ("va2pa error");
@@ -462,16 +461,14 @@ static void get_ivar_list(RBinFile *bf, RBinClass *klass, mach0_ut p) {
462461
}
463462
r_list_sort (klass->fields, sort_by_offset);
464463
RBinField *isa = R_NEW0 (RBinField);
465-
if (isa) {
466-
isa->name = r_bin_name_new ("isa");
467-
isa->size = sizeof (mach0_ut);
468-
isa->type = r_bin_name_new ("struct objc_class *");
469-
// TODO r_bin_name_demangled (isa->type, "ObjC.Class*");
470-
isa->kind = R_BIN_FIELD_KIND_VARIABLE;
471-
isa->vaddr = 0;
472-
isa->offset = 0;
473-
r_list_prepend (klass->fields, isa);
474-
}
464+
isa->name = r_bin_name_new ("isa");
465+
isa->size = sizeof (mach0_ut);
466+
isa->type = r_bin_name_new ("struct objc_class *");
467+
// TODO r_bin_name_demangled (isa->type, "ObjC.Class*");
468+
isa->kind = R_BIN_FIELD_KIND_VARIABLE;
469+
isa->vaddr = 0;
470+
isa->offset = 0;
471+
r_list_prepend (klass->fields, isa);
475472
return;
476473
error:
477474
r_bin_field_free (field);
@@ -530,10 +527,7 @@ static void get_objc_property_list(RBinFile *bf, RBinClass *klass, mach0_ut p) {
530527
return;
531528
}
532529

533-
if (!(property = R_NEW0 (RBinField))) {
534-
// retain just for debug
535-
return;
536-
}
530+
property = R_NEW0 (RBinField);
537531

538532
memset (&op, '\0', sizeof (struct MACH0_(SObjcProperty)));
539533

@@ -767,10 +761,6 @@ static void get_method_list(RBinFile *bf, RBinClass *klass, const char *class_na
767761
}
768762

769763
method = R_NEW0 (RBinSymbol);
770-
if (!method) {
771-
// retain just for debug
772-
return;
773-
}
774764
struct MACH0_(SMethod) m = {0};
775765
if (r + left < r || r + read_size < r) {
776766
goto error;
@@ -1659,9 +1649,6 @@ static void parse_type(RBinFile *bf, RList *list, SwiftType st, HtUP *symbols_ht
16591649
break;
16601650
}
16611651
RBinField *field = R_NEW0 (RBinField);
1662-
if (!field) {
1663-
break;
1664-
}
16651652
ut64 field_name_addr = st.fieldmd.addr + (d * 4) + st.fieldmd_data[d];
16661653
ut64 field_type_addr = st.fieldmd.addr + (d * 4) + st.fieldmd_data[d - 1] - 4;
16671654
ut64 field_method_addr = field_name_addr;

libr/bin/mangling/cxx/cp-demangle.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4674,6 +4674,7 @@ d_print_comp_inner (struct d_print_info *dpi, int options,
46744674
struct d_print_mod adpm[4];
46754675
unsigned int i;
46764676
struct d_print_template dpt;
4677+
int is_template;
46774678

46784679
/* Pass the name down to the type so that it can be printed in
46794680
the right place for the type. We also have to pass down
@@ -4747,7 +4748,8 @@ d_print_comp_inner (struct d_print_info *dpi, int options,
47474748

47484749
/* If typed_name is a template, then it applies to the
47494750
function type as well. */
4750-
if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
4751+
is_template = typed_name->type == DEMANGLE_COMPONENT_TEMPLATE;
4752+
if (is_template)
47514753
{
47524754
dpt.next = dpi->templates;
47534755
dpi->templates = &dpt;
@@ -4756,7 +4758,7 @@ d_print_comp_inner (struct d_print_info *dpi, int options,
47564758

47574759
d_print_comp (dpi, options, d_right (dc));
47584760

4759-
if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
4761+
if (is_template)
47604762
dpi->templates = dpt.next;
47614763

47624764
/* If the modifiers didn't get printed by the type, print them

0 commit comments

Comments
 (0)