blueloveTH 1 سال پیش
والد
کامیت
79cdd2b252
2فایلهای تغییر یافته به همراه6 افزوده شده و 1 حذف شده
  1. 2 1
      src/common/vector.c
  2. 4 0
      tests/98_lz4.py

+ 2 - 1
src/common/vector.c

@@ -30,7 +30,8 @@ c11_vector c11_vector__copy(const c11_vector* self){
 void c11_vector__reserve(c11_vector* self, int capacity){
     if(capacity < 4) capacity = 4;
     if(capacity <= self->capacity) return;
-    self->data = realloc(self->data, self->elem_size * capacity);
+    // self->elem_size * capacity may overflow
+    self->data = realloc(self->data, self->elem_size * (size_t)capacity);
     self->capacity = capacity;
 }
 

+ 4 - 0
tests/98_lz4.py

@@ -25,3 +25,7 @@ def gen_data():
 for i in range(100):
     ratio = test(gen_data())
     # print(f'compression ratio: {ratio:.2f}')
+
+# test 100MB of random data
+rnd = [random.randint(0, 255) for _ in range(1024*1024*100)]
+test(bytes(rnd))