Browse Source

[settings] Make fetch_string_setting_copy() easier to use

Most callers of functions in the fetch_setting() family treat any
errors as meaning "non-existent setting".  In the case of
fetch_string_setting_copy(), an existent setting can still result in
an error due to memory allocation failure.

Allow the caller to distinguish between a non-existent setting and an
error in allocating memory for the copy, by returning success (and a
NULL buffer pointer) for a non-existent setting.

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 13 years ago
parent
commit
48a4001152
1 changed files with 10 additions and 1 deletions
  1. 10
    1
      src/core/settings.c

+ 10
- 1
src/core/settings.c View File

@@ -625,6 +625,11 @@ int fetch_string_setting ( struct settings *settings, struct setting *setting,
625 625
  * The returned length will be the length of the underlying setting
626 626
  * data.  The caller is responsible for eventually freeing the
627 627
  * allocated buffer.
628
+ *
629
+ * To allow the caller to distinguish between a non-existent setting
630
+ * and an error in allocating memory for the copy, this function will
631
+ * return success (and a NULL buffer pointer) for a non-existent
632
+ * setting.
628 633
  */
629 634
 int fetch_string_setting_copy ( struct settings *settings,
630 635
 				struct setting *setting,
@@ -632,16 +637,20 @@ int fetch_string_setting_copy ( struct settings *settings,
632 637
 	int len;
633 638
 	int check_len = 0;
634 639
 
640
+	/* Avoid returning uninitialised data on error */
635 641
 	*data = NULL;
636 642
 
643
+	/* Fetch setting length, and return success if non-existent */
637 644
 	len = fetch_setting_len ( settings, setting );
638 645
 	if ( len < 0 )
639
-		return len;
646
+		return 0;
640 647
 
648
+	/* Allocate string buffer */
641 649
 	*data = malloc ( len + 1 );
642 650
 	if ( ! *data )
643 651
 		return -ENOMEM;
644 652
 
653
+	/* Fetch setting */
645 654
 	check_len = fetch_string_setting ( settings, setting, *data,
646 655
 					   ( len + 1 ) );
647 656
 	assert ( check_len == len );

Loading…
Cancel
Save