Parcourir la source

[settings] Eliminate call to fetchf_named_setting() in expand_settings()

Use parse_setting_name() and fetchf_setting_copy() in
expand_settings(), to eliminate the call to fetchf_named_setting().

This change also eliminates the potentially large stack-allocated
buffer in expand_settings().

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown il y a 11 ans
Parent
révision
129a70631a
1 fichiers modifiés avec 25 ajouts et 22 suppressions
  1. 25
    22
      src/core/settings.c

+ 25
- 22
src/core/settings.c Voir le fichier

@@ -2014,15 +2014,18 @@ struct setting_type setting_type_busdevfn __setting_type = {
2014 2014
  * eventually free() it.
2015 2015
  */
2016 2016
 char * expand_settings ( const char *string ) {
2017
+	struct settings *settings;
2018
+	struct setting setting;
2017 2019
 	char *expstr;
2018 2020
 	char *start;
2019 2021
 	char *end;
2020 2022
 	char *head;
2021 2023
 	char *name;
2022 2024
 	char *tail;
2023
-	int setting_len;
2024
-	int new_len;
2025
+	char *value;
2025 2026
 	char *tmp;
2027
+	int new_len;
2028
+	int rc;
2026 2029
 
2027 2030
 	/* Obtain temporary modifiable copy of string */
2028 2031
 	expstr = strdup ( string );
@@ -2052,27 +2055,27 @@ char * expand_settings ( const char *string ) {
2052 2055
 		*end = '\0';
2053 2056
 		tail = ( end + 1 );
2054 2057
 
2055
-		/* Determine setting length */
2056
-		setting_len = fetchf_named_setting ( name, NULL, 0, NULL, 0 );
2057
-		if ( setting_len < 0 )
2058
-			setting_len = 0; /* Treat error as empty setting */
2059
-
2060
-		/* Read setting into temporary buffer */
2061
-		{
2062
-			char setting_buf[ setting_len + 1 ];
2063
-
2064
-			setting_buf[0] = '\0';
2065
-			fetchf_named_setting ( name, NULL, 0, setting_buf,
2066
-					       sizeof ( setting_buf ) );
2067
-
2068
-			/* Construct expanded string and discard old string */
2069
-			tmp = expstr;
2070
-			new_len = asprintf ( &expstr, "%s%s%s",
2071
-					     head, setting_buf, tail );
2072
-			free ( tmp );
2073
-			if ( new_len < 0 )
2074
-				return NULL;
2058
+		/* Expand setting */
2059
+		if ( ( rc = parse_setting_name ( name, find_child_settings,
2060
+						 &settings,
2061
+						 &setting ) ) != 0 ) {
2062
+			/* Treat invalid setting names as empty */
2063
+			value = NULL;
2064
+		} else {
2065
+			/* Fetch and format setting value.  Ignore
2066
+			 * errors; treat non-existent settings as empty.
2067
+			 */
2068
+			fetchf_setting_copy ( settings, &setting, &value );
2075 2069
 		}
2070
+
2071
+		/* Construct expanded string and discard old string */
2072
+		tmp = expstr;
2073
+		new_len = asprintf ( &expstr, "%s%s%s",
2074
+				     head, ( value ? value : "" ), tail );
2075
+		free ( value );
2076
+		free ( tmp );
2077
+		if ( new_len < 0 )
2078
+			return NULL;
2076 2079
 	}
2077 2080
 
2078 2081
 	return expstr;

Chargement…
Annuler
Enregistrer