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

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
122static void sym_validate_range(struct symbol *sym)122static void sym_validate_range(struct symbol *sym)
123{123{
124 struct property *prop;124 struct property *prop;
125 struct symbol *range_sym;
125 int base;126 int base;
126 long long val, val2;127 long long val, val2;
127 char str[64];
128 128
129 switch (sym->type) {129 switch (sym->type) {
130 case S_INT:130 case S_INT:
@@ -140,17 +140,15 @@ static void sym_validate_range(struct symbol *sym) · 9 unmodified lines
140 if (!prop)140 if (!prop)
141 return;141 return;
142 val = strtoll(sym->curr.val, NULL, base);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 if (val >= val2) {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 if (val <= val2)148 if (val <= val2)
147 return;149 return;
148 }150 }
149 if (sym->type == S_INT)151 sym->curr.val = range_sym->curr.val;
150 sprintf(str, "%lld", val2);
151 else
152 sprintf(str, "0x%llx", val2);
153 sym->curr.val = xstrdup(str);
154}152}
155 153
156static void sym_set_changed(struct symbol *sym)154static void sym_set_changed(struct symbol *sym)