Fix a DTLS memory leak
[openssl.git] / apps / app_rand.c
index 960d2fe6c63739298d348fef92e025ea7fd4e83b..d0b2e834340635bb961767c43949eaa308e782f7 100644 (file)
@@ -9,10 +9,11 @@
 
 #include "apps.h"
 #include <openssl/bio.h>
+#include <openssl/err.h>
 #include <openssl/rand.h>
 #include <openssl/conf.h>
 
-static const char *save_rand_file;
+static char *save_rand_file;
 
 void app_RAND_load_conf(CONF *c, const char *section)
 {
@@ -25,15 +26,14 @@ void app_RAND_load_conf(CONF *c, const char *section)
     if (RAND_load_file(randfile, -1) < 0) {
         BIO_printf(bio_err, "Can't load %s into RNG\n", randfile);
         ERR_print_errors(bio_err);
-        return;
     }
     if (save_rand_file == NULL)
-        save_rand_file = randfile;
+        save_rand_file = OPENSSL_strdup(randfile);
 }
 
 static int loadfiles(char *name)
 {
-    char *p, *n;
+    char *p;
     int last, ret = 1;
 
     for ( ; ; ) {
@@ -48,7 +48,6 @@ static int loadfiles(char *name)
             ERR_print_errors(bio_err);
             ret = 0;
         }
-        n = name;
         if (last)
             break;
         name = p + 1;
@@ -66,6 +65,8 @@ void app_RAND_write(void)
         BIO_printf(bio_err, "Cannot write random bytes:\n");
         ERR_print_errors(bio_err);
     }
+    OPENSSL_free(save_rand_file);
+    save_rand_file =  NULL;
 }
 
 
@@ -84,7 +85,8 @@ int opt_rand(int opt)
         return loadfiles(opt_arg());
         break;
     case OPT_R_WRITERAND:
-        save_rand_file = opt_arg();
+        OPENSSL_free(save_rand_file);
+        save_rand_file = OPENSSL_strdup(opt_arg());
         break;
     }
     return 1;