added overflow-safe grow functions
Minclude/cutils/byte_array.h
| @@ -46,6 +46,7 @@ static void bytearray_remove(Bytearray* bytearray, size_t index, void (*rmv)(voi | |||
|---|---|---|---|
| 46 | 46 | } | |
| 47 | 47 | void bytearray_remove_range(Bytearray* bytearray, size_t index, size_t length, void (*rmv)(void*)); | |
| 48 | 48 | ||
| 49 | + | bool bytearray_grow(Bytearray* bytearray, size_t add); | |
| 49 | 50 | bool bytearray_adjust_size(Bytearray* bytearray, size_t size); | |
| 50 | 51 | bool bytearray_shrink(Bytearray* bytearray); | |
| 51 | 52 | HEDLEY_NON_NULL(3) | |
Minclude/cutils/dyn_string.h
| @@ -43,6 +43,7 @@ static void string_remove(String* string, size_t index) | |||
|---|---|---|---|
| 43 | 43 | void string_remove_range(String* string, size_t index, size_t length); | |
| 44 | 44 | size_t string_strip(String* string, char character); | |
| 45 | 45 | ||
| 46 | + | bool string_grow(String* string, size_t add); | |
| 46 | 47 | bool string_adjust_size(String* string, size_t size); | |
| 47 | 48 | bool string_find_char(const String* haystack, const char needle, size_t* pos); | |
| 48 | 49 | HEDLEY_INLINE | |
Minclude/cutils/vector.h
| @@ -42,6 +42,7 @@ static void vector_remove(Vector* vector, size_t index, void (*rmv)(void*)) | |||
|---|---|---|---|
| 42 | 42 | } | |
| 43 | 43 | void vector_remove_range(Vector* vector, size_t index, size_t length, void (*rmv)(void*)); | |
| 44 | 44 | ||
| 45 | + | bool vector_grow(Vector* vector, size_t add); | |
| 45 | 46 | bool vector_adjust_size(Vector* vector, size_t size); | |
| 46 | 47 | bool vector_shrink(Vector* vector); | |
| 47 | 48 | HEDLEY_NON_NULL(3) | |
Msrc/byte_array.c
| @@ -75,7 +75,7 @@ bool bytearray_insert(Bytearray* bytearray, size_t index, const void* item) | |||
|---|---|---|---|
| 75 | 75 | size_t length = bytearray->length; | |
| 76 | 76 | size_t size = bytearray->element_size; | |
| 77 | 77 | ||
| 78 | - | if(index > length || length == SIZE_MAX || !bytearray_adjust_size(bytearray, length+1)) | |
| 78 | + | if(index > length || !bytearray_grow(bytearray, 1)) | |
| 79 | 79 | return false; | |
| 80 | 80 | ||
| 81 | 81 | memmove(bytearray->items+index*size+1*size, bytearray->items+index*size, (length*size-index*size)*sizeof(*bytearray->items)); | |
| @@ -84,6 +84,13 @@ bool bytearray_insert(Bytearray* bytearray, size_t index, const void* item) | |||
|---|---|---|---|
| 84 | 84 | return true; | |
| 85 | 85 | } | |
| 86 | 86 | ||
| 87 | + | bool bytearray_grow(Bytearray* bytearray, size_t add) | |
| 88 | + | { | |
| 89 | + | if(bytearray->length+add < bytearray->length) | |
| 90 | + | return false; | |
| 91 | + | return bytearray_adjust_size(bytearray, bytearray->length+add); | |
| 92 | + | } | |
| 93 | + | ||
| 87 | 94 | bool bytearray_adjust_size(Bytearray* bytearray, size_t size) | |
| 88 | 95 | { | |
| 89 | 96 | while(bytearray->capacity < size) | |
Msrc/dyn_string.c
| @@ -55,7 +55,7 @@ size_t string_strip(String* string, char character) | |||
|---|---|---|---|
| 55 | 55 | ||
| 56 | 56 | bool string_insert(String* string, size_t index, char character) | |
| 57 | 57 | { | |
| 58 | - | if(index > string->length || string->length == SIZE_MAX || !string_adjust_size(string, string->length+1)) | |
| 58 | + | if(index > string->length || !string_grow(string, 1)) | |
| 59 | 59 | return false; | |
| 60 | 60 | ||
| 61 | 61 | memmove(string->chars+index+1, string->chars+index, (string->length-index+(string->null_terminated?1:0))*sizeof(*string->chars)); | |
| @@ -73,7 +73,9 @@ char string_pop_at(String* string, size_t index) | |||
|---|---|---|---|
| 73 | 73 | ||
| 74 | 74 | bool string_concat(String* string, const String* other) | |
| 75 | 75 | { | |
| 76 | - | if(!string_adjust_size(string,string->length+other->length-1)) | |
| 76 | + | if(other->length == 0) | |
| 77 | + | return true; | |
| 78 | + | if(!string_grow(string, other->length-1)) | |
| 77 | 79 | return false; | |
| 78 | 80 | memcpy(string->chars+string->length,other->chars,other->length); | |
| 79 | 81 | string->length += other->length; | |
| @@ -107,6 +109,13 @@ size_t string_count(const String* string, char character) | |||
|---|---|---|---|
| 107 | 109 | return ret; | |
| 108 | 110 | } | |
| 109 | 111 | ||
| 112 | + | bool string_grow(String* string, size_t add) | |
| 113 | + | { | |
| 114 | + | if(string->length+add < string->length) | |
| 115 | + | return false; | |
| 116 | + | return string_adjust_size(string, string->length+add); | |
| 117 | + | } | |
| 118 | + | ||
| 110 | 119 | bool string_adjust_size(String* string, size_t size) | |
| 111 | 120 | { | |
| 112 | 121 | if(size == SIZE_MAX && string->null_terminated) | |
Msrc/vector.c
| @@ -61,7 +61,7 @@ size_t* vector_find(const Vector* haystack, const void* needle, int (*cmp)(const | |||
|---|---|---|---|
| 61 | 61 | ||
| 62 | 62 | bool vector_insert(Vector* vector, size_t index, void* item) | |
| 63 | 63 | { | |
| 64 | - | if(index > vector->length || vector->length == SIZE_MAX || !vector_adjust_size(vector, vector->length+1)) | |
| 64 | + | if(index > vector->length || !vector_grow(vector, 1)) | |
| 65 | 65 | return false; | |
| 66 | 66 | ||
| 67 | 67 | memmove(vector->items+index+1, vector->items+index, (vector->length-index)*sizeof(*vector->items)); | |
| @@ -70,6 +70,13 @@ bool vector_insert(Vector* vector, size_t index, void* item) | |||
|---|---|---|---|
| 70 | 70 | return true; | |
| 71 | 71 | } | |
| 72 | 72 | ||
| 73 | + | bool vector_grow(Vector* vector, size_t add) | |
| 74 | + | { | |
| 75 | + | if(vector->length+add < vector->length) | |
| 76 | + | return false; | |
| 77 | + | return vector_adjust_size(vector, vector->length+add); | |
| 78 | + | } | |
| 79 | + | ||
| 73 | 80 | bool vector_adjust_size(Vector* vector, size_t size) | |
| 74 | 81 | { | |
| 75 | 82 | while(vector->capacity < size) | |