From: Andrej Shadura <andrew.shadura@collabora.co.uk>
Date: Thu, 30 Jan 2020 17:31:36 +0100
Subject: Check the hash algorithm before (possibly failing to) malloc

malloc only accepts unsigned sizes, so failing to catch a negative
size as an errorcode will result in a malloc call with an enormous
value, leading to an allocation failure and a nonsensical error
message.

Signed-off-by: Andrej Shadura <andrew.shadura@collabora.co.uk>
---
 code/cst/code/front_end/src/acst.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/code/cst/code/front_end/src/acst.c b/code/cst/code/front_end/src/acst.c
index e382673..3f780ab 100644
--- a/code/cst/code/front_end/src/acst.c
+++ b/code/cst/code/front_end/src/acst.c
@@ -788,16 +788,17 @@ void encrypt_images(ahab_data_t *ahab_data,
 
         uint8_t hash_type = ahab_container_image_get_hash(image);
         int32_t hash_size = ahab_get_hash_size_by_sha_type(hash_type);
+
+        if (hash_size < 0) {
+            error("Unsupported hash algorithm for image integrity");
+        }
+
         uint8_t *hash = malloc(hash_size);
 
         if (NULL ==  hash) {
             error("Cannot allocate memory for the hash value of the plaintext");
         }
 
-        if (hash_size < 0) {
-            error("Unsupported hash algorithm for image integrity");
-        }
-
         /* Retrieve image data */
 
         offsets_t offsets = {
