Skip to contentSkip to conversation

kconfig: remove 'const' from the return type of sym_escape_string()

PATCH In review Masahiro Yamada 2 participants 2 messages Sep 27, 2021 – Sep 28, 2021

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kconfig: remove 'const' from the return type of sym_escape_string()

sym_escape_string() returns a malloc'ed memory, so it must be freed when it is done.

Currently, sym_escape_string() returns the malloc'ed memory as (const char *), then it is casted to (void *) when it is passed to free(). This is odd.

The return type of sym_escape_string() should be (char *).

---

 scripts/kconfig/conf.c      | 10 +++++-----
 scripts/kconfig/confdata.c  |  8 ++++----
 scripts/kconfig/lkc_proto.h |  2 +-
 scripts/kconfig/symbol.c    |  3 ++-
 4 files changed, 12 insertions(+), 11 deletions(-)
scripts/kconfig/conf.c +5 -5
index a6dad4a2e7a2..d8e1994bfed0 100644
@@ -647,15 +647,15 @@ static void check_conf(struct menu *menu)
 		switch (input_mode) {
 		case listnewconfig:
 			if (sym->name) {
-				const char *str;
-
 				if (sym->type == S_STRING) {
+					char *str;
+
 					str = sym_escape_string(sym);
 					printf("%s%s=%s\n", CONFIG_, sym->name, str);
-					free((void *)str);
+					free(str);
 				} else {
-					str = sym_get_string_value(sym);
-					printf("%s%s=%s\n", CONFIG_, sym->name, str);
+					printf("%s%s=%s\n", CONFIG_, sym->name,
+					       sym_get_string_value(sym));
 				}
 			}
 			break;
scripts/kconfig/confdata.c +4 -4
index 4e053f2477f9..f7eac4beb128 100644
@@ -728,7 +728,7 @@ static struct conf_printer header_printer_cb =
 static void conf_write_symbol(FILE *fp, struct symbol *sym,
 			      struct conf_printer *printer, void *printer_arg)
 {
-	const char *str;
+	char *str;
 
 	switch (sym->type) {
 	case S_UNKNOWN:
@@ -736,11 +736,11 @@ static void conf_write_symbol(FILE *fp, struct symbol *sym,
 	case S_STRING:
 		str = sym_escape_string(sym);
 		printer->print_symbol(fp, sym, str, printer_arg);
-		free((void *)str);
+		free(str);
 		break;
 	default:
-		str = sym_get_string_value(sym);
-		printer->print_symbol(fp, sym, str, printer_arg);
+		printer->print_symbol(fp, sym, sym_get_string_value(sym),
+				      printer_arg);
 	}
 }
 
scripts/kconfig/lkc_proto.h +1 -1
index 035cc522808b..7ce4b666bba8 100644
@@ -18,7 +18,7 @@ extern struct symbol * symbol_hash[SYMBOL_HASHSIZE];
 
 struct symbol * sym_lookup(const char *name, int flags);
 struct symbol * sym_find(const char *name);
-const char * sym_escape_string(struct symbol *sym);
+char *sym_escape_string(struct symbol *sym);
 struct symbol ** sym_re_search(const char *pattern);
 const char * sym_type_name(enum symbol_type type);
 void sym_calc_value(struct symbol *sym);
scripts/kconfig/symbol.c +2 -1
index 4a31bb943f79..57189a1ad797 100644
@@ -871,7 +871,8 @@ struct symbol *sym_find(const char *name)
 	return symbol;
 }
 
-const char *sym_escape_string(struct symbol *sym)
+/* the returned pointer must be freed on the caller side */
+char *sym_escape_string(struct symbol *sym)
 {
 	const char *in, *p;
 	size_t reslen;
Signature · 1 line
2.30.2
Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines · Masahiro Yamada

sym_escape_string() returns a malloc'ed memory, so it must be freed when it is done.

Currently, sym_escape_string() returns the malloc'ed memory as (const char *), then it is casted to (void *) when it is passed to free(). This is odd.

The return type of sym_escape_string() should be (char *).

Quoted 91 lines

---

 scripts/kconfig/conf.c      | 10 +++++-----
 scripts/kconfig/confdata.c  |  8 ++++----
 scripts/kconfig/lkc_proto.h |  2 +-
 scripts/kconfig/symbol.c    |  3 ++-
 4 files changed, 12 insertions(+), 11 deletions(-)
scripts/kconfig/conf.c +5 -5
index a6dad4a2e7a2..d8e1994bfed0 100644
@@ -647,15 +647,15 @@ static void check_conf(struct menu *menu)
 		switch (input_mode) {
 		case listnewconfig:
 			if (sym->name) {
-				const char *str;
-
 				if (sym->type == S_STRING) {
+					char *str;
+
 					str = sym_escape_string(sym);
 					printf("%s%s=%s\n", CONFIG_, sym->name, str);
-					free((void *)str);
+					free(str);
 				} else {
-					str = sym_get_string_value(sym);
-					printf("%s%s=%s\n", CONFIG_, sym->name, str);
+					printf("%s%s=%s\n", CONFIG_, sym->name,
+					       sym_get_string_value(sym));
 				}
 			}
 			break;
scripts/kconfig/confdata.c +4 -4
index 4e053f2477f9..f7eac4beb128 100644
@@ -728,7 +728,7 @@ static struct conf_printer header_printer_cb =
 static void conf_write_symbol(FILE *fp, struct symbol *sym,
 			      struct conf_printer *printer, void *printer_arg)
 {
-	const char *str;
+	char *str;
 
 	switch (sym->type) {
 	case S_UNKNOWN:
@@ -736,11 +736,11 @@ static void conf_write_symbol(FILE *fp, struct symbol *sym,
 	case S_STRING:
 		str = sym_escape_string(sym);
 		printer->print_symbol(fp, sym, str, printer_arg);
-		free((void *)str);
+		free(str);
 		break;
 	default:
-		str = sym_get_string_value(sym);
-		printer->print_symbol(fp, sym, str, printer_arg);
+		printer->print_symbol(fp, sym, sym_get_string_value(sym),
+				      printer_arg);
 	}
 }
 
scripts/kconfig/lkc_proto.h +1 -1
index 035cc522808b..7ce4b666bba8 100644
@@ -18,7 +18,7 @@ extern struct symbol * symbol_hash[SYMBOL_HASHSIZE];
 
 struct symbol * sym_lookup(const char *name, int flags);
 struct symbol * sym_find(const char *name);
-const char * sym_escape_string(struct symbol *sym);
+char *sym_escape_string(struct symbol *sym);
 struct symbol ** sym_re_search(const char *pattern);
 const char * sym_type_name(enum symbol_type type);
 void sym_calc_value(struct symbol *sym);
scripts/kconfig/symbol.c +2 -1
index 4a31bb943f79..57189a1ad797 100644
@@ -871,7 +871,8 @@ struct symbol *sym_find(const char *name)
 	return symbol;
 }
 
-const char *sym_escape_string(struct symbol *sym)
+/* the returned pointer must be freed on the caller side */
+char *sym_escape_string(struct symbol *sym)
 {
 	const char *in, *p;
 	size_t reslen;
Signature · 1 line
2.30.2