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
  * The returned length will be the length of the underlying setting
625
  * The returned length will be the length of the underlying setting
626
  * data.  The caller is responsible for eventually freeing the
626
  * data.  The caller is responsible for eventually freeing the
627
  * allocated buffer.
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
 int fetch_string_setting_copy ( struct settings *settings,
634
 int fetch_string_setting_copy ( struct settings *settings,
630
 				struct setting *setting,
635
 				struct setting *setting,
632
 	int len;
637
 	int len;
633
 	int check_len = 0;
638
 	int check_len = 0;
634
 
639
 
640
+	/* Avoid returning uninitialised data on error */
635
 	*data = NULL;
641
 	*data = NULL;
636
 
642
 
643
+	/* Fetch setting length, and return success if non-existent */
637
 	len = fetch_setting_len ( settings, setting );
644
 	len = fetch_setting_len ( settings, setting );
638
 	if ( len < 0 )
645
 	if ( len < 0 )
639
-		return len;
646
+		return 0;
640
 
647
 
648
+	/* Allocate string buffer */
641
 	*data = malloc ( len + 1 );
649
 	*data = malloc ( len + 1 );
642
 	if ( ! *data )
650
 	if ( ! *data )
643
 		return -ENOMEM;
651
 		return -ENOMEM;
644
 
652
 
653
+	/* Fetch setting */
645
 	check_len = fetch_string_setting ( settings, setting, *data,
654
 	check_len = fetch_string_setting ( settings, setting, *data,
646
 					   ( len + 1 ) );
655
 					   ( len + 1 ) );
647
 	assert ( check_len == len );
656
 	assert ( check_len == len );

Loading…
Cancel
Save