Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

+6 -8
+6 -8
scripts/kconfig/symbol.c
··· 122 122 static void sym_validate_range(struct symbol *sym) 123 123 { 124 124 struct property *prop; 125 + struct symbol *range_sym; 125 126 int base; 126 127 long long val, val2; 127 - char str[64]; 128 128 129 129 switch (sym->type) { 130 130 case S_INT: ··· 140 140 if (!prop) 141 141 return; 142 142 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); 144 145 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); 146 148 if (val <= val2) 147 149 return; 148 150 } 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; 154 152 } 155 153 156 154 static void sym_set_changed(struct symbol *sym)