]> git.ipfire.org Git - thirdparty/ipxe.git/commitdiff
[settings] Make fetch_string_setting_copy() easier to use
authorMichael Brown <mcb30@ipxe.org>
Thu, 3 Mar 2011 19:59:31 +0000 (19:59 +0000)
committerMichael Brown <mcb30@ipxe.org>
Thu, 3 Mar 2011 20:03:08 +0000 (20:03 +0000)
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>
src/core/settings.c

index 1f22ea45f9eb3c436178e72772c81fa8b08d8e1d..139addd52ff8636a74765fcfb52be780611e9370 100644 (file)
@@ -625,6 +625,11 @@ int fetch_string_setting ( struct settings *settings, struct setting *setting,
  * The returned length will be the length of the underlying setting
  * data.  The caller is responsible for eventually freeing the
  * allocated buffer.
+ *
+ * To allow the caller to distinguish between a non-existent setting
+ * and an error in allocating memory for the copy, this function will
+ * return success (and a NULL buffer pointer) for a non-existent
+ * setting.
  */
 int fetch_string_setting_copy ( struct settings *settings,
                                struct setting *setting,
@@ -632,16 +637,20 @@ int fetch_string_setting_copy ( struct settings *settings,
        int len;
        int check_len = 0;
 
+       /* Avoid returning uninitialised data on error */
        *data = NULL;
 
+       /* Fetch setting length, and return success if non-existent */
        len = fetch_setting_len ( settings, setting );
        if ( len < 0 )
-               return len;
+               return 0;
 
+       /* Allocate string buffer */
        *data = malloc ( len + 1 );
        if ( ! *data )
                return -ENOMEM;
 
+       /* Fetch setting */
        check_len = fetch_string_setting ( settings, setting, *data,
                                           ( len + 1 ) );
        assert ( check_len == len );