Skip to content

Commit ae1eff0

Browse files
committed
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 Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
1 parent 7602073 commit ae1eff0

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

scripts/kconfig/symbol.c

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,9 @@ static long long sym_get_range_val(struct symbol *sym, int base)
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)
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)

0 commit comments

Comments
 (0)