History

kconfig: fix memory leak from range properties

Currently, sym_validate_range() duplicates the range string using xstrdup(), which is overwritten by a subsequent sym_calc_value() call. It results in a memory leak. Instead, only the pointer should be copied. Below is a test case, with a summary from Valgrind. [Test Kconfig] config FOO int "foo" range 10 20 [Test .config] CONFIG_FOO=0 [Before] LEAK SUMMARY: definitely lost: 3 bytes in 1 blocks indirectly lost: 0 bytes in 0 blocks possibly lost: 0 bytes in 0 blocks still reachable: 17,465 bytes in 21 blocks suppressed: 0 bytes in 0 blocks [After] LEAK SUMMARY: definitely lost: 0 bytes in 0 blocks indirectly lost: 0 bytes in 0 blocks possibly lost: 0 bytes in 0 blocks still reachable: 17,462 bytes in 20 blocks suppressed: 0 bytes in 0 blocks

Masahiro Yamada committed 2 years ago

Commit ae1eff01 parent 76020731 file · +6 −8Browse files

Showing the changes to scripts (1 of 1 file). Show all

Unified · Split

scripts/kconfig/symbol.c+6−8
@@ -122,9 +122,9 @@ static long long sym_get_range_val(struct symbol *sym, int base) · 121 unmodified lines
122122 static void sym_validate_range(struct symbol *sym)
123123 {
124124 struct property *prop;
125+ struct symbol *range_sym;
125126 int base;
126127 long long val, val2;
127− char str[64];
128128
129129 switch (sym->type) {
130130 case S_INT:
@@ -140,17 +140,15 @@ static void sym_validate_range(struct symbol *sym) · 9 unmodified lines
140140 if (!prop)
141141 return;
142142 val = strtoll(sym->curr.val, NULL, base);
143− val2 = sym_get_range_val(prop->expr->left.sym, base);
143+ range_sym = prop->expr->left.sym;
144+ val2 = sym_get_range_val(range_sym, base);
144145 if (val >= val2) {
145− val2 = sym_get_range_val(prop->expr->right.sym, base);
146+ range_sym = prop->expr->right.sym;
147+ val2 = sym_get_range_val(range_sym, base);
146148 if (val <= val2)
147149 return;
148150 }
149− if (sym->type == S_INT)
150− sprintf(str, "%lld", val2);
151− else
152− sprintf(str, "0x%llx", val2);
153− sym->curr.val = xstrdup(str);
151+ sym->curr.val = range_sym->curr.val;
154152 }
155153
156154 static void sym_set_changed(struct symbol *sym)