APPS: replace awkward and error-prone pattern by calls to new app_conf_try_number()
authorDr. David von Oheimb <David.von.Oheimb@siemens.com>
Tue, 16 May 2023 08:24:35 +0000 (10:24 +0200)
committerDr. David von Oheimb <dev@ddvo.net>
Thu, 25 May 2023 07:04:35 +0000 (09:04 +0200)
Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Paul Dale <pauli@openssl.org>
Reviewed-by: David von Oheimb <david.von.oheimb@siemens.com>
(Merged from https://github.com/openssl/openssl/pull/20971)

apps/ca.c
apps/include/apps.h
apps/lib/apps.c
apps/req.c

index 8bdbc9a1f0e6e50c0e3bd2ec0c9fac143997cb6d..751287eda8de43f7feed4ef6185c85026330c7a0 100644 (file)
--- a/apps/ca.c
+++ b/apps/ca.c
@@ -886,10 +886,8 @@ end_of_options:
         }
 
         if (days == 0) {
-            if (!NCONF_get_number(conf, section, ENV_DEFAULT_DAYS, &days)) {
-                ERR_clear_error();
+            if (!app_conf_try_number(conf, section, ENV_DEFAULT_DAYS, &days))
                 days = 0;
-            }
         }
         if (enddate == NULL && days == 0) {
             BIO_printf(bio_err, "cannot lookup how many days to certify for\n");
@@ -1149,16 +1147,12 @@ end_of_options:
         }
 
         if (!crldays && !crlhours && !crlsec) {
-            if (!NCONF_get_number(conf, section,
-                                  ENV_DEFAULT_CRL_DAYS, &crldays)) {
-                ERR_clear_error();
+            if (!app_conf_try_number(conf, section,
+                                  ENV_DEFAULT_CRL_DAYS, &crldays))
                 crldays = 0;
-            }
-            if (!NCONF_get_number(conf, section,
-                                  ENV_DEFAULT_CRL_HOURS, &crlhours)) {
-                ERR_clear_error();
+            if (!app_conf_try_number(conf, section,
+                                  ENV_DEFAULT_CRL_HOURS, &crlhours))
                 crlhours = 0;
-            }
         }
         if ((crl_nextupdate == NULL) &&
                 (crldays == 0) && (crlhours == 0) && (crlsec == 0)) {
index c9e0d440e8c9f4cb46cea736909914022b020e14..62b4d19ae3e7fabc87905c34466f1607fe6eaab9 100644 (file)
@@ -66,6 +66,8 @@ BIO *bio_open_owner(const char *filename, int format, int private);
 BIO *bio_open_default(const char *filename, char mode, int format);
 BIO *bio_open_default_quiet(const char *filename, char mode, int format);
 char *app_conf_try_string(const CONF *cnf, const char *group, const char *name);
+int app_conf_try_number(const CONF *conf, const char *group, const char *name,
+                        long *result);
 CONF *app_load_config_bio(BIO *in, const char *filename);
 # define app_load_config(filename) app_load_config_internal(filename, 0)
 # define app_load_config_quiet(filename) app_load_config_internal(filename, 1)
index bfa983a35199a45aa99bfb565a9c4325f840a5b0..79980257bdeef3d7636a8236b6ccf984bd38357f 100644 (file)
@@ -349,6 +349,19 @@ char *app_conf_try_string(const CONF *conf, const char *group, const char *name)
     return res;
 }
 
+int app_conf_try_number(const CONF *conf, const char *group, const char *name,
+                        long *result)
+{
+    int ok;
+
+    ERR_set_mark();
+    ok = NCONF_get_number(conf, group, name, result);
+    if (!ok)
+        ERR_pop_to_mark();
+    else
+        ERR_clear_last_mark();
+    return ok;
+}
 
 CONF *app_load_config_bio(BIO *in, const char *filename)
 {
index 11ecf6cad7533b0f5fa828563d9020c6af2213ba..59ed6ebaa2e52d8d65d7d89df87ce21a26e705cb 100644 (file)
@@ -608,7 +608,7 @@ int req_main(int argc, char **argv)
     if (newreq && pkey == NULL) {
         app_RAND_load_conf(req_conf, section);
 
-        if (!NCONF_get_number(req_conf, section, BITS, &newkey_len))
+        if (!app_conf_try_number(req_conf, section, BITS, &newkey_len))
             newkey_len = DEFAULT_KEY_LENGTH;
 
         genctx = set_keygen_ctx(keyalg, &keyalgstr, &newkey_len, gen_eng);
@@ -1167,17 +1167,13 @@ static int prompt_info(X509_REQ *req,
 
             if (!join(buf, sizeof(buf), v->name, "_min", "Name"))
                 return 0;
-            if (!NCONF_get_number(req_conf, dn_sect, buf, &n_min)) {
-                ERR_clear_error();
+            if (!app_conf_try_number(req_conf, dn_sect, buf, &n_min))
                 n_min = -1;
-            }
 
             if (!join(buf, sizeof(buf), v->name, "_max", "Name"))
                 return 0;
-            if (!NCONF_get_number(req_conf, dn_sect, buf, &n_max)) {
-                ERR_clear_error();
+            if (!app_conf_try_number(req_conf, dn_sect, buf, &n_max))
                 n_max = -1;
-            }
 
             if (!add_DN_object(subj, v->value, def, value, nid,
                                n_min, n_max, chtype, mval))
@@ -1221,17 +1217,13 @@ static int prompt_info(X509_REQ *req,
 
                 if (!join(buf, sizeof(buf), type, "_min", "Name"))
                     return 0;
-                if (!NCONF_get_number(req_conf, attr_sect, buf, &n_min)) {
-                    ERR_clear_error();
+                if (!app_conf_try_number(req_conf, attr_sect, buf, &n_min))
                     n_min = -1;
-                }
 
                 if (!join(buf, sizeof(buf), type, "_max", "Name"))
                     return 0;
-                if (!NCONF_get_number(req_conf, attr_sect, buf, &n_max)) {
-                    ERR_clear_error();
+                if (!app_conf_try_number(req_conf, attr_sect, buf, &n_max))
                     n_max = -1;
-                }
 
                 if (!add_attribute_object(req,
                                           v->value, def, value, nid, n_min,