Use safer sizeof variant in malloc
[openssl.git] / apps / srp.c
index adc6a6f130113e709425c671fb804baf9de0a35b..c7a93cf7490a791d7fa597b2f78512d2d04e7a14 100644 (file)
@@ -138,11 +138,7 @@ static int update_index(CA_DB *db, char **row)
     char **irow;
     int i;
 
-    if ((irow = OPENSSL_malloc(sizeof(char *) * (DB_NUMBER + 1))) == NULL) {
-        BIO_printf(bio_err, "Memory allocation failure\n");
-        return 0;
-    }
-
+    irow = app_malloc(sizeof(*irow) * (DB_NUMBER + 1), "row pointers");
     for (i = 0; i < DB_NUMBER; i++) {
         irow[i] = row[i];
         row[i] = NULL;
@@ -336,7 +332,8 @@ int srp_main(int argc, char **argv)
                    "Exactly one of the options -add, -delete, -modify -list must be specified.\n");
         goto opthelp;
     }
-    if ((mode == OPT_DELETE || mode == OPT_MODIFY || OPT_ADD) && argc < 1) {
+    if ((mode == OPT_DELETE || mode == OPT_MODIFY || mode == OPT_ADD)
+        && argc < 1) {
         BIO_printf(bio_err,
                    "Need at least one user for options -add, -delete, -modify. \n");
         goto opthelp;
@@ -362,23 +359,12 @@ int srp_main(int argc, char **argv)
             configfile = getenv("SSLEAY_CONF");
         if (configfile == NULL) {
             const char *s = X509_get_default_cert_area();
-            size_t len;
+            size_t len = strlen(s) + 1 + sizeof(CONFIG_FILE);
 
+            tofree = app_malloc(len, "config filename space");
 # ifdef OPENSSL_SYS_VMS
-            len = strlen(s) + sizeof(CONFIG_FILE);
-            tofree = OPENSSL_malloc(len);
-            if (!tofree) {
-                BIO_printf(bio_err, "Out of memory\n");
-                goto end;
-            }
             strcpy(tofree, s);
 # else
-            len = strlen(s) + sizeof(CONFIG_FILE) + 1;
-            tofree = OPENSSL_malloc(len);
-            if (!tofree) {
-                BIO_printf(bio_err, "Out of memory\n");
-                goto end;
-            }
             BUF_strlcpy(tofree, s, len);
             BUF_strlcat(tofree, "/", len);
 # endif
@@ -398,10 +384,8 @@ int srp_main(int argc, char **argv)
                            errorline, configfile);
             goto end;
         }
-        if (tofree) {
-            OPENSSL_free(tofree);
-            tofree = NULL;
-        }
+        OPENSSL_free(tofree);
+        tofree = NULL;
 
         /* Lets get the config section we are using */
         if (section == NULL) {
@@ -533,26 +517,16 @@ int srp_main(int argc, char **argv)
                 row[DB_srpgN] = BUF_strdup(gNid);
 
                 if (!row[DB_srpid] || !row[DB_srpgN] || !row[DB_srptype]
-                    || !row[DB_srpverifier] || !row[DB_srpsalt] || (userinfo
-                                                                    &&
-                                                                    (!(row
-                                                                       [DB_srpinfo]
-                                                                       =
-                                                                       BUF_strdup
-                                                                       (userinfo))))
+                    || !row[DB_srpverifier] || !row[DB_srpsalt]
+                    || (userinfo &&
+                         (!(row [DB_srpinfo] = BUF_strdup (userinfo))))
                     || !update_index(db, row)) {
-                    if (row[DB_srpid])
-                        OPENSSL_free(row[DB_srpid]);
-                    if (row[DB_srpgN])
-                        OPENSSL_free(row[DB_srpgN]);
-                    if (row[DB_srpinfo])
-                        OPENSSL_free(row[DB_srpinfo]);
-                    if (row[DB_srptype])
-                        OPENSSL_free(row[DB_srptype]);
-                    if (row[DB_srpverifier])
-                        OPENSSL_free(row[DB_srpverifier]);
-                    if (row[DB_srpsalt])
-                        OPENSSL_free(row[DB_srpsalt]);
+                    OPENSSL_free(row[DB_srpid]);
+                    OPENSSL_free(row[DB_srpgN]);
+                    OPENSSL_free(row[DB_srpinfo]);
+                    OPENSSL_free(row[DB_srptype]);
+                    OPENSSL_free(row[DB_srpverifier]);
+                    OPENSSL_free(row[DB_srpsalt]);
                     goto end;
                 }
                 doupdatedb = 1;
@@ -690,17 +664,13 @@ int srp_main(int argc, char **argv)
 
     if (verbose)
         BIO_printf(bio_err, "SRP terminating with code %d.\n", ret);
-    if (tofree)
-        OPENSSL_free(tofree);
+    OPENSSL_free(tofree);
     if (ret)
         ERR_print_errors(bio_err);
     if (randfile)
         app_RAND_write_file(randfile);
-    if (conf)
-        NCONF_free(conf);
-    if (db)
-        free_index(db);
-
+    NCONF_free(conf);
+    free_index(db);
     OBJ_cleanup();
     return (ret);
 }