Browse Source

[settings] Remove temporary name buffer parameter from parse_setting_name()

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 11 years ago
parent
commit
78178608e9
1 changed files with 40 additions and 22 deletions
  1. 40
    22
      src/core/settings.c

+ 40
- 22
src/core/settings.c View File

@@ -1189,27 +1189,27 @@ static struct setting_type * find_setting_type ( const char *name ) {
1189 1189
  * @v settings		Settings block to fill in
1190 1190
  * @v setting		Setting to fill in
1191 1191
  * @v default_type	Default type to use, if none specified
1192
- * @v tmp_name		Buffer for copy of setting name
1193 1192
  * @ret rc		Return status code
1194 1193
  *
1195 1194
  * Interprets a name of the form
1196 1195
  * "[settings_name/]tag_name[:type_name]" and fills in the appropriate
1197 1196
  * fields.
1198 1197
  *
1199
- * The @c tmp_name buffer must be large enough to hold a copy of the
1200
- * setting name.
1198
+ * Note that on success, this function will have modified the original
1199
+ * setting @c name.
1201 1200
  */
1202 1201
 static int
1203
-parse_setting_name ( const char *name,
1204
-		     struct settings * ( * get_child ) ( struct settings *,
1205
-							 const char * ),
1202
+parse_setting_name ( char *name,
1203
+		     struct settings * ( * get_child )
1204
+			     ( struct settings *settings,
1205
+			       const char *name ),
1206 1206
 		     struct settings **settings, struct setting *setting,
1207
-		     struct setting_type *default_type,
1208
-		     char *tmp_name ) {
1207
+		     struct setting_type *default_type ) {
1209 1208
 	char *settings_name;
1210 1209
 	char *setting_name;
1211 1210
 	char *type_name;
1212 1211
 	struct setting *predefined;
1212
+	int rc;
1213 1213
 
1214 1214
 	/* Set defaults */
1215 1215
 	*settings = &settings_root;
@@ -1218,12 +1218,11 @@ parse_setting_name ( const char *name,
1218 1218
 	setting->type = default_type;
1219 1219
 
1220 1220
 	/* Split name into "[settings_name/]setting_name[:type_name]" */
1221
-	strcpy ( tmp_name, name );
1222
-	if ( ( setting_name = strchr ( tmp_name, '/' ) ) != NULL ) {
1221
+	if ( ( setting_name = strchr ( name, '/' ) ) != NULL ) {
1223 1222
 		*(setting_name++) = 0;
1224
-		settings_name = tmp_name;
1223
+		settings_name = name;
1225 1224
 	} else {
1226
-		setting_name = tmp_name;
1225
+		setting_name = name;
1227 1226
 		settings_name = NULL;
1228 1227
 	}
1229 1228
 	if ( ( type_name = strchr ( setting_name, ':' ) ) != NULL )
@@ -1235,7 +1234,8 @@ parse_setting_name ( const char *name,
1235 1234
 		if ( *settings == NULL ) {
1236 1235
 			DBG ( "Unrecognised settings block \"%s\" in \"%s\"\n",
1237 1236
 			      settings_name, name );
1238
-			return -ENODEV;
1237
+			rc = -ENODEV;
1238
+			goto err;
1239 1239
 		}
1240 1240
 	}
1241 1241
 
@@ -1257,11 +1257,20 @@ parse_setting_name ( const char *name,
1257 1257
 		if ( setting->type == NULL ) {
1258 1258
 			DBG ( "Invalid setting type \"%s\" in \"%s\"\n",
1259 1259
 			      type_name, name );
1260
-			return -ENOTSUP;
1260
+			rc = -ENOTSUP;
1261
+			goto err;
1261 1262
 		}
1262 1263
 	}
1263 1264
 
1264 1265
 	return 0;
1266
+
1267
+ err:
1268
+	/* Restore original name */
1269
+	if ( settings_name )
1270
+		*( setting_name - 1 ) = '/';
1271
+	if ( type_name )
1272
+		*( type_name - 1 ) = ':';
1273
+	return rc;
1265 1274
 }
1266 1275
 
1267 1276
 /**
@@ -1299,10 +1308,13 @@ int store_named_setting ( const char *name, struct setting_type *default_type,
1299 1308
 	char tmp_name[ strlen ( name ) + 1 ];
1300 1309
 	int rc;
1301 1310
 
1311
+	/* Create modifiable copy of setting name */
1312
+	strcpy ( tmp_name, name );
1313
+
1302 1314
 	/* Parse setting name */
1303
-	if ( ( rc = parse_setting_name ( name, autovivify_child_settings,
1304
-					 &settings, &setting, default_type,
1305
-					 tmp_name ) ) != 0 )
1315
+	if ( ( rc = parse_setting_name ( tmp_name, autovivify_child_settings,
1316
+					 &settings, &setting,
1317
+					 default_type ) ) != 0 )
1306 1318
 		return rc;
1307 1319
 
1308 1320
 	/* Store setting */
@@ -1327,10 +1339,13 @@ int storef_named_setting ( const char *name, struct setting_type *default_type,
1327 1339
 	char tmp_name[ strlen ( name ) + 1 ];
1328 1340
 	int rc;
1329 1341
 
1342
+	/* Create modifiable copy of setting name */
1343
+	strcpy ( tmp_name, name );
1344
+
1330 1345
 	/* Parse setting name */
1331
-	if ( ( rc = parse_setting_name ( name, autovivify_child_settings,
1332
-					 &settings, &setting, default_type,
1333
-					 tmp_name ) ) != 0 )
1346
+	if ( ( rc = parse_setting_name ( tmp_name, autovivify_child_settings,
1347
+					 &settings, &setting,
1348
+					 default_type ) ) != 0 )
1334 1349
 		return rc;
1335 1350
 
1336 1351
 	/* Store setting */
@@ -1360,9 +1375,12 @@ int fetchf_named_setting ( const char *name,
1360 1375
 	int len;
1361 1376
 	int rc;
1362 1377
 
1378
+	/* Create modifiable copy of setting name */
1379
+	strcpy ( tmp_name, name );
1380
+
1363 1381
 	/* Parse setting name */
1364
-	if ( ( rc = parse_setting_name ( name, find_child_settings, &settings,
1365
-					 &setting, NULL, tmp_name ) ) != 0 )
1382
+	if ( ( rc = parse_setting_name ( tmp_name, find_child_settings,
1383
+					 &settings, &setting, NULL ) ) != 0 )
1366 1384
 		return rc;
1367 1385
 
1368 1386
 	/* Fetch setting */

Loading…
Cancel
Save