From b287b27a87ecfbbd3b1206b17269d19e76a1b467 Mon Sep 17 00:00:00 2001
From: pic4xiu <40382944+pic4xiu@users.noreply.github.com>
Date: Thu, 29 Jun 2023 19:50:47 +0800
Subject: [PATCH] Update opj_malloc.c

Conflict: NA
Reference: https://github.com/uclouvain/openjpeg/commit/b287b27a87ecfbbd3b1206b17269d19e76a1b467
---
 src/lib/openjp2/opj_malloc.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/src/lib/openjp2/opj_malloc.c b/src/lib/openjp2/opj_malloc.c
index dca91bfcb..6242614f9 100644
--- a/src/lib/openjp2/opj_malloc.c
+++ b/src/lib/openjp2/opj_malloc.c
@@ -197,6 +197,16 @@ void * opj_malloc(size_t size)
 }
 void * opj_calloc(size_t num, size_t size)
 {
+    static unsigned long long allocated_size = 0;
+    static unsigned long long max_allocated_size = 4ULL * 1024 * 1024 * 1024;
+    /*Restrict this function can only malloc 4GB of memory*/
+    
+    unsigned long long total_size = (unsigned long long)(num * size);
+    allocated_size += total_size;
+    if (allocated_size > max_allocated_size) {
+        /*Prevent excessive resource allocation*/
+        return NULL;
+    }
     if (num == 0 || size == 0) {
         /* prevent implementation defined behavior of realloc */
         return NULL;
