added overflow-safe grow functions

AuthorKonata <konata@posteo.jp>
Date
Commit1245ac7bb4da68bc71fc79f50af1ae2efb3f3083
Parent1a0f31a
6 files changed, 30 insertions(+), 4 deletions(-)
Minclude/cutils/byte_array.h
@@ -46,6 +46,7 @@ static void bytearray_remove(Bytearray* bytearray, size_t index, void (*rmv)(voi
4646 }
4747 void bytearray_remove_range(Bytearray* bytearray, size_t index, size_t length, void (*rmv)(void*));
4848
49+bool bytearray_grow(Bytearray* bytearray, size_t add);
4950 bool bytearray_adjust_size(Bytearray* bytearray, size_t size);
5051 bool bytearray_shrink(Bytearray* bytearray);
5152 HEDLEY_NON_NULL(3)
Minclude/cutils/dyn_string.h
@@ -43,6 +43,7 @@ static void string_remove(String* string, size_t index)
4343 void string_remove_range(String* string, size_t index, size_t length);
4444 size_t string_strip(String* string, char character);
4545
46+bool string_grow(String* string, size_t add);
4647 bool string_adjust_size(String* string, size_t size);
4748 bool string_find_char(const String* haystack, const char needle, size_t* pos);
4849 HEDLEY_INLINE
Minclude/cutils/vector.h
@@ -42,6 +42,7 @@ static void vector_remove(Vector* vector, size_t index, void (*rmv)(void*))
4242 }
4343 void vector_remove_range(Vector* vector, size_t index, size_t length, void (*rmv)(void*));
4444
45+bool vector_grow(Vector* vector, size_t add);
4546 bool vector_adjust_size(Vector* vector, size_t size);
4647 bool vector_shrink(Vector* vector);
4748 HEDLEY_NON_NULL(3)
Msrc/byte_array.c
@@ -75,7 +75,7 @@ bool bytearray_insert(Bytearray* bytearray, size_t index, const void* item)
7575 size_t length = bytearray->length;
7676 size_t size = bytearray->element_size;
7777
78- if(index > length || length == SIZE_MAX || !bytearray_adjust_size(bytearray, length+1))
78+ if(index > length || !bytearray_grow(bytearray, 1))
7979 return false;
8080
8181 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)
8484 return true;
8585 }
8686
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+
8794 bool bytearray_adjust_size(Bytearray* bytearray, size_t size)
8895 {
8996 while(bytearray->capacity < size)
Msrc/dyn_string.c
@@ -55,7 +55,7 @@ size_t string_strip(String* string, char character)
5555
5656 bool string_insert(String* string, size_t index, char character)
5757 {
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))
5959 return false;
6060
6161 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)
7373
7474 bool string_concat(String* string, const String* other)
7575 {
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))
7779 return false;
7880 memcpy(string->chars+string->length,other->chars,other->length);
7981 string->length += other->length;
@@ -107,6 +109,13 @@ size_t string_count(const String* string, char character)
107109 return ret;
108110 }
109111
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+
110119 bool string_adjust_size(String* string, size_t size)
111120 {
112121 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
6161
6262 bool vector_insert(Vector* vector, size_t index, void* item)
6363 {
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))
6565 return false;
6666
6767 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)
7070 return true;
7171 }
7272
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+
7380 bool vector_adjust_size(Vector* vector, size_t size)
7481 {
7582 while(vector->capacity < size)