Browse Source

Size limit for linked list

Kajetan Johannes Hammerle 2 months ago
parent
commit
22f3b5c1e6
2 changed files with 12 additions and 0 deletions
  1. 3 0
      include/core/data/LinkedList.hpp
  2. 9 0
      test/modules/LinkedListTests.cpp

+ 3 - 0
include/core/data/LinkedList.hpp

@@ -81,6 +81,9 @@ namespace Core {
 
         template<typename... Args>
         check_return Error put(Node*& n, Args&&... args) {
+            if(length >= CORE_INT_MAX) {
+                return Error::CAPACITY_REACHED;
+            }
             n = new Node(Core::forward<Args>(args)...);
             if(n == nullptr) {
                 return Error::OUT_OF_MEMORY;

+ 9 - 0
test/modules/LinkedListTests.cpp

@@ -293,6 +293,14 @@ static void testOutOfMemory() {
 #endif
 }
 
+static void testOverflow() {
+    IntList list;
+    for(int i = 0; i < CORE_INT_MAX; i++) {
+        CORE_TEST_ERROR(list.add(i));
+    }
+    CORE_TEST_EQUAL(Core::Error::CAPACITY_REACHED, list.add(1));
+}
+
 void Core::testLinkedList(bool light, bool outOfMemoryTest) {
     testWithoutCopyOrMove();
     testAdd();
@@ -311,4 +319,5 @@ void Core::testLinkedList(bool light, bool outOfMemoryTest) {
     if(outOfMemoryTest) {
         testOutOfMemory();
     }
+    testOverflow();
 }