From 0fbaef9e64fa10446aff805791befaa2b967e322 Mon Sep 17 00:00:00 2001 From: Bernd Edlinger Date: Sun, 19 Feb 2017 20:13:45 +0100 Subject: [PATCH] Fix some more memory leaks with TXT_DB_insert. Reviewed-by: Richard Levitte Reviewed-by: Rich Salz (Merged from https://github.com/openssl/openssl/pull/2684) --- apps/ca.c | 18 ++++++++++++------ apps/srp.c | 4 +--- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/apps/ca.c b/apps/ca.c index e53695453a..00fb6f5aba 100644 --- a/apps/ca.c +++ b/apps/ca.c @@ -2062,19 +2062,26 @@ static int do_revoke(X509 *x509, CA_DB *db, REVINFO_TYPE rev_type, row[DB_rev_date] = NULL; row[DB_file] = OPENSSL_strdup("unknown"); - irow = app_malloc(sizeof(*irow) * (DB_NUMBER + 1), "row ptr"); - for (i = 0; i < DB_NUMBER; i++) { - irow[i] = row[i]; - row[i] = NULL; + if (row[DB_type] == NULL || row[DB_file] == NULL) { + BIO_printf(bio_err, "Memory allocation failure\n"); + goto end; } + + irow = app_malloc(sizeof(*irow) * (DB_NUMBER + 1), "row ptr"); + for (i = 0; i < DB_NUMBER; i++) + irow[i] = row[i]; irow[DB_NUMBER] = NULL; if (!TXT_DB_insert(db->db, irow)) { BIO_printf(bio_err, "failed to update database\n"); BIO_printf(bio_err, "TXT_DB error number %ld\n", db->db->error); + OPENSSL_free(irow); goto end; } + for (i = 0; i < DB_NUMBER; i++) + row[i] = NULL; + /* Revoke Certificate */ if (rev_type == REV_VALID) ok = 1; @@ -2108,9 +2115,8 @@ static int do_revoke(X509 *x509, CA_DB *db, REVINFO_TYPE rev_type, } ok = 1; end: - for (i = 0; i < DB_NUMBER; i++) { + for (i = 0; i < DB_NUMBER; i++) OPENSSL_free(row[i]); - } return (ok); } diff --git a/apps/srp.c b/apps/srp.c index b213c6010d..be4a4b43d3 100644 --- a/apps/srp.c +++ b/apps/srp.c @@ -92,10 +92,8 @@ static int update_index(CA_DB *db, char **row) int i; irow = app_malloc(sizeof(*irow) * (DB_NUMBER + 1), "row pointers"); - for (i = 0; i < DB_NUMBER; i++) { + for (i = 0; i < DB_NUMBER; i++) irow[i] = row[i]; - row[i] = NULL; - } irow[DB_NUMBER] = NULL; if (!TXT_DB_insert(db->db, irow)) {