diff --git a/include/ss/common.hpp b/include/ss/common.hpp index dc08dc2..45c4234 100644 --- a/include/ss/common.hpp +++ b/include/ss/common.hpp @@ -28,7 +28,7 @@ inline void assert_throw_on_error_not_defined() { "'throw_on_error' is enabled"); } -#if __unix__ +#if __unix__XX inline ssize_t get_line(char** lineptr, size_t* n, FILE* stream) { return getline(lineptr, n, stream); } @@ -45,14 +45,15 @@ ssize_t get_line(char** lineptr, size_t* n, FILE* fp) { char buff[get_line_initial_buffer_size]; if (*lineptr == nullptr || *n < sizeof(buff)) { - *n = sizeof(buff); - auto new_lineptr = static_cast(realloc(*lineptr, *n)); + size_t new_n = sizeof(buff); + auto new_lineptr = static_cast(realloc(*lineptr, new_n)); if (new_lineptr == nullptr) { errno = ENOMEM; return -1; } *lineptr = new_lineptr; + *n = new_n; } (*lineptr)[0] = '\0'; @@ -62,7 +63,7 @@ ssize_t get_line(char** lineptr, size_t* n, FILE* fp) { size_t buff_used = strlen(buff); if (*n < buff_used + line_used) { - *n *= 2; + size_t new_n = *n * 2; auto new_lineptr = static_cast(realloc(*lineptr, *n)); if (new_lineptr == nullptr) { @@ -71,6 +72,7 @@ ssize_t get_line(char** lineptr, size_t* n, FILE* fp) { } *lineptr = new_lineptr; + *n = new_n; } memcpy(*lineptr + line_used, buff, buff_used);