From 40652d7dbf701eda83fa8323b42a6b5bf0ca6bdd Mon Sep 17 00:00:00 2001
From: dec05eba <dec05eba@protonmail.com>
Date: Sat, 24 Aug 2019 01:26:09 +0200
Subject: Add sanity checks

---
 src/program.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)

(limited to 'src')

diff --git a/src/program.c b/src/program.c
index 8a17ac9..55bfda4 100644
--- a/src/program.c
+++ b/src/program.c
@@ -8,6 +8,9 @@
 #include <errno.h>
 #include <assert.h>
 
+/* One gigabyte */
+#define PROGRAM_MAX_SIZE 1024*1024*1024
+
 /* TODO: If system is big-endian, then do endian conversion for all reads */
 
 /* This matches SsaNumberType */
@@ -136,6 +139,11 @@ static CHECK_RESULT int amal_program_set_exported_function_instruction_offset_ad
 }
 
 int amal_program_append_bytecode(amal_program *self, Bytecode *bytecode) {
+    /* Sanity check for indices used later. A program shouldn't be more than 1gb */
+    if(self->data.size + bytecode->data.size > PROGRAM_MAX_SIZE) {
+        amal_log_error("Program is too large. Max size is 1GB");
+        return -1;
+    }
     return buffer_append(&self->data, bytecode->data.data, bytecode->data.size);
 }
 
@@ -567,6 +575,10 @@ static CHECK_RESULT int amal_program_read_instructions(amal_program *self, amal_
 int amal_program_run(amal_program *self) {
     int result;
     amal_executor *executor;
+    if(self->data.size > PROGRAM_MAX_SIZE) {
+        amal_log_error("Program is too large. Max size is 1GB");
+        return AMAL_PROGRAM_ERR;
+    }
     result = AMAL_PROGRAM_ERR;
     return_if_error(amal_executor_init(&executor));
 
-- 
cgit v1.2.3-70-g09d2