diff --git a/src/string.c b/src/string.c index 8f7b26b..d6985d0 100644 --- a/src/string.c +++ b/src/string.c @@ -20,6 +20,7 @@ #include #include #include +#include #include #include "System/error.h" #include "System/object.h" @@ -106,9 +107,14 @@ String * string_new_length(String const * string, size_t length) #ifdef DEBUG fprintf(stderr, "DEBUG: %s(\"%s\", %zu)\n", __func__, string, length); #endif - if((ret = object_new(++length)) == NULL) + if(length == SIZE_T_MAX) + { + error_set_code(-ERANGE, "%s", strerror(ERANGE)); return NULL; - snprintf(ret, length, "%s", (string != NULL) ? string : ""); + } + if((ret = object_new(length + 1)) == NULL) + return NULL; + snprintf(ret, length + 1, "%s", (string != NULL) ? string : ""); return ret; }