david/ipxe
david
/
ipxe
Archived
1
0
Fork 0

[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>
This commit is contained in:
Michael Brown 2013-07-18 14:47:42 +01:00
parent a5be7c4f29
commit 129a70631a
1 changed files with 25 additions and 22 deletions

View File

@ -2014,15 +2014,18 @@ struct setting_type setting_type_busdevfn __setting_type = {
* eventually free() it. * eventually free() it.
*/ */
char * expand_settings ( const char *string ) { char * expand_settings ( const char *string ) {
struct settings *settings;
struct setting setting;
char *expstr; char *expstr;
char *start; char *start;
char *end; char *end;
char *head; char *head;
char *name; char *name;
char *tail; char *tail;
int setting_len; char *value;
int new_len;
char *tmp; char *tmp;
int new_len;
int rc;
/* Obtain temporary modifiable copy of string */ /* Obtain temporary modifiable copy of string */
expstr = strdup ( string ); expstr = strdup ( string );
@ -2052,27 +2055,27 @@ char * expand_settings ( const char *string ) {
*end = '\0'; *end = '\0';
tail = ( end + 1 ); tail = ( end + 1 );
/* Determine setting length */ /* Expand setting */
setting_len = fetchf_named_setting ( name, NULL, 0, NULL, 0 ); if ( ( rc = parse_setting_name ( name, find_child_settings,
if ( setting_len < 0 ) &settings,
setting_len = 0; /* Treat error as empty setting */ &setting ) ) != 0 ) {
/* Treat invalid setting names as empty */
/* Read setting into temporary buffer */ value = NULL;
{ } else {
char setting_buf[ setting_len + 1 ]; /* Fetch and format setting value. Ignore
* errors; treat non-existent settings as empty.
setting_buf[0] = '\0'; */
fetchf_named_setting ( name, NULL, 0, setting_buf, fetchf_setting_copy ( settings, &setting, &value );
sizeof ( setting_buf ) );
/* Construct expanded string and discard old string */
tmp = expstr;
new_len = asprintf ( &expstr, "%s%s%s",
head, setting_buf, tail );
free ( tmp );
if ( new_len < 0 )
return NULL;
} }
/* Construct expanded string and discard old string */
tmp = expstr;
new_len = asprintf ( &expstr, "%s%s%s",
head, ( value ? value : "" ), tail );
free ( value );
free ( tmp );
if ( new_len < 0 )
return NULL;
} }
return expstr; return expstr;