Bladeren bron

[settings] Change "not-found" semantics of fetch_setting_copy()

fetch_settings_copy() currently returns success and a NULL data
pointer to indicate a non-existent setting.  This is intended to allow
the caller to differentiate between a non-existent setting and an
error in allocating memory for the copy of the setting.

The underlying settings blocks' fetch() methods provide no way to
perform an existence check separate from an attempt to fetch the
setting.  A "non-existent setting" therefore means simply a setting
for which an error was encountered when attempting to fetch from every
settings block within the subtree.

Since any underlying error within a settings block (e.g. a GuestRPC
failure when attempting to retrieve a VMware GuestInfo setting) will
produce the effect of a "non-existent setting", it seems somewhat
meaningless to give special treatment to memory allocation errors
within fetch_setting_copy().

Remove the special treatment and simplify the semantics of
fetch_setting_copy() by directly passing through any underlying error
(including non-existence) encountered while fetching the setting.

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 11 jaren geleden
bovenliggende
commit
72fb55e437
3 gewijzigde bestanden met toevoegingen van 8 en 47 verwijderingen
  1. 2
    14
      src/core/settings.c
  2. 4
    17
      src/crypto/clientcert.c
  3. 2
    16
      src/crypto/rootcert.c

+ 2
- 14
src/core/settings.c Bestand weergeven

@@ -722,11 +722,6 @@ int fetch_setting_len ( struct settings *settings, struct setting *setting ) {
722 722
  *
723 723
  * The caller is responsible for eventually freeing the allocated
724 724
  * buffer.
725
- *
726
- * To allow the caller to distinguish between a non-existent setting
727
- * and an error in allocating memory for the copy, this function will
728
- * return success (and a NULL buffer pointer) for a non-existent
729
- * setting.
730 725
  */
731 726
 int fetch_setting_copy ( struct settings *settings, struct setting *setting,
732 727
 			 void **data ) {
@@ -736,10 +731,10 @@ int fetch_setting_copy ( struct settings *settings, struct setting *setting,
736 731
 	/* Avoid returning uninitialised data on error */
737 732
 	*data = NULL;
738 733
 
739
-	/* Fetch setting length, and return success if non-existent */
734
+	/* Check existence, and fetch setting length */
740 735
 	len = fetch_setting_len ( settings, setting );
741 736
 	if ( len < 0 )
742
-		return 0;
737
+		return len;
743 738
 
744 739
 	/* Allocate buffer */
745 740
 	*data = malloc ( len );
@@ -1055,12 +1050,6 @@ int fetchf_setting ( struct settings *settings, struct setting *setting,
1055 1050
 		goto err_fetch_copy;
1056 1051
 	}
1057 1052
 
1058
-	/* Return error if setting does not exist */
1059
-	if ( ! raw ) {
1060
-		ret = -ENOENT;
1061
-		goto err_exists;
1062
-	}
1063
-
1064 1053
 	/* Sanity check */
1065 1054
 	assert ( setting->type != NULL );
1066 1055
 	assert ( setting->type->format != NULL );
@@ -1071,7 +1060,6 @@ int fetchf_setting ( struct settings *settings, struct setting *setting,
1071 1060
 
1072 1061
  err_format:
1073 1062
 	free ( raw );
1074
- err_exists:
1075 1063
  err_fetch_copy:
1076 1064
 	return ret;
1077 1065
 }

+ 4
- 17
src/crypto/clientcert.c Bestand weergeven

@@ -116,7 +116,6 @@ static int clientcert_apply_settings ( void ) {
116 116
 	static void *cert = NULL;
117 117
 	static void *key = NULL;
118 118
 	int len;
119
-	int rc;
120 119
 
121 120
 	/* Allow client certificate to be overridden only if
122 121
 	 * not explicitly specified at build time.
@@ -129,14 +128,8 @@ static int clientcert_apply_settings ( void ) {
129 128
 
130 129
 		/* Fetch new client certificate, if any */
131 130
 		free ( cert );
132
-		len = fetch_setting_copy ( NULL, &cert_setting, &cert );
133
-		if ( len < 0 ) {
134
-			rc = len;
135
-			DBGC ( &client_certificate, "CLIENTCERT cannot fetch "
136
-			       "client certificate: %s\n", strerror ( rc ) );
137
-			return rc;
138
-		}
139
-		if ( cert ) {
131
+		if ( ( len = fetch_setting_copy ( NULL, &cert_setting,
132
+						  &cert ) ) >= 0 ) {
140 133
 			client_certificate.data = cert;
141 134
 			client_certificate.len = len;
142 135
 		}
@@ -147,14 +140,8 @@ static int clientcert_apply_settings ( void ) {
147 140
 
148 141
 		/* Fetch new client private key, if any */
149 142
 		free ( key );
150
-		len = fetch_setting_copy ( NULL, &privkey_setting, &key );
151
-		if ( len < 0 ) {
152
-			rc = len;
153
-			DBGC ( &client_certificate, "CLIENTCERT cannot fetch "
154
-			       "client private key: %s\n", strerror ( rc ) );
155
-			return rc;
156
-		}
157
-		if ( key ) {
143
+		if ( ( len = fetch_setting_copy ( NULL, &privkey_setting,
144
+						  &key ) ) >= 0 ) {
158 145
 			client_private_key.data = key;
159 146
 			client_private_key.len = len;
160 147
 		}

+ 2
- 16
src/crypto/rootcert.c Bestand weergeven

@@ -91,7 +91,6 @@ struct x509_root root_certificates = {
91 91
 static void rootcert_init ( void ) {
92 92
 	void *external = NULL;
93 93
 	int len;
94
-	int rc;
95 94
 
96 95
 	/* Allow trusted root certificates to be overridden only if
97 96
 	 * not explicitly specified at build time.
@@ -101,21 +100,8 @@ static void rootcert_init ( void ) {
101 100
 		/* Fetch copy of "trust" setting, if it exists.  This
102 101
 		 * memory will never be freed.
103 102
 		 */
104
-		len = fetch_setting_copy ( NULL, &trust_setting, &external );
105
-		if ( len < 0 ) {
106
-			rc = len;
107
-			DBGC ( &root_certificates, "ROOTCERT cannot fetch "
108
-			       "trusted root certificate fingerprints: %s\n",
109
-			       strerror ( rc ) );
110
-			/* No way to prevent startup; fail safe by
111
-			 * trusting no certificates.
112
-			 */
113
-			root_certificates.count = 0;
114
-			return;
115
-		}
116
-
117
-		/* Use certificates from "trust" setting, if present */
118
-		if ( external ) {
103
+		if ( ( len = fetch_setting_copy ( NULL, &trust_setting,
104
+						  &external ) ) >= 0 ) {
119 105
 			root_certificates.fingerprints = external;
120 106
 			root_certificates.count = ( len / FINGERPRINT_LEN );
121 107
 		}

Laden…
Annuleren
Opslaan