[libbdplus-devel] Check memory allocation

anonymous git at videolan.org
Thu Mar 16 13:24:52 CET 2017


libbdplus | branch: master | anonymous <anonymous at anonymous.org> | Thu Mar 16 14:21:27 2017 +0200| [ecf82be058fd0fec28b238d956db3220bfdbf418] | committer: anonymous

Check memory allocation

> http://git.videolan.org/gitweb.cgi/libbdplus.git/?a=commit;h=ecf82be058fd0fec28b238d956db3220bfdbf418
---

 src/libbdplus/bdsvm/trap.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)

diff --git a/src/libbdplus/bdsvm/trap.c b/src/libbdplus/bdsvm/trap.c
index 58d9177..5ae491b 100644
--- a/src/libbdplus/bdsvm/trap.c
+++ b/src/libbdplus/bdsvm/trap.c
@@ -358,9 +358,10 @@ uint32_t TRAP_PrivateKey(bdplus_config_t *config, uint32_t keyID, uint8_t *dst,
 {
     uint8_t *message = NULL;
     gcry_error_t gcry_err;
-    gcry_mpi_t mpi_hash;
-    gcry_sexp_t sexp_key, sexp_data, sexp_sig, sexp_r, sexp_s;
+    gcry_mpi_t mpi_hash = NULL;
+    gcry_sexp_t sexp_key = NULL, sexp_data = NULL, sexp_sig = NULL, sexp_r = NULL, sexp_s = NULL;
     char errstr[100];
+    uint32_t result = STATUS_INVALID_PARAMETER;
 
     if (!config || !config->ecdsa_keys) {
         BD_DEBUG(DBG_BDPLUS | DBG_CRIT, "[TRAP] TRAP_PrivateKey: ECDSA keys not loaded.\n");
@@ -541,6 +542,11 @@ uint32_t TRAP_PrivateKey(bdplus_config_t *config, uint32_t keyID, uint8_t *dst,
         );
     }
 
+    if (!strfmt_key) {
+      BD_DEBUG(DBG_BDPLUS | DBG_CRIT,"[TRAP] TRAP_PrivateKey: out of memory ?\n");
+      goto error;
+    }
+
     /* Now build the S-expression */
     gcry_err = gcry_sexp_build(&sexp_key, NULL, strfmt_key);
     if (gcry_err)
@@ -608,6 +614,9 @@ uint32_t TRAP_PrivateKey(bdplus_config_t *config, uint32_t keyID, uint8_t *dst,
     memcpy(dst, r, 20);
     memcpy(dst + 20, s, 20);
 
+    result = STATUS_OK;
+
+ error:
     /* Free allocated memory */
     gcry_mpi_release(mpi_hash);
     gcry_sexp_release(sexp_key);
@@ -620,7 +629,7 @@ uint32_t TRAP_PrivateKey(bdplus_config_t *config, uint32_t keyID, uint8_t *dst,
     X_FREE(message);
     X_FREE(strfmt_key);
 
-    return STATUS_OK;
+    return result;
 }
 
 // write <len> random bytes to <dst>



More information about the libbdplus-devel mailing list