mirror of
https://github.com/red0124/ssp.git
synced 2025-01-23 04:55:20 +01:00
Merge pull request #34 from red0124/improvement/getline_update
Fix reallocation issues with non-POSIX get_line
This commit is contained in:
commit
a27fd121a1
@ -62,10 +62,10 @@ ssize_t get_line(char** lineptr, size_t* n, FILE* fp) {
|
||||
size_t line_used = strlen(*lineptr);
|
||||
size_t buff_used = strlen(buff);
|
||||
|
||||
if (*n < buff_used + line_used) {
|
||||
if (*n <= buff_used + line_used) {
|
||||
size_t new_n = *n * 2;
|
||||
|
||||
auto new_lineptr = static_cast<char*>(realloc(*lineptr, *n));
|
||||
auto new_lineptr = static_cast<char*>(realloc(*lineptr, new_n));
|
||||
if (new_lineptr == nullptr) {
|
||||
errno = ENOMEM;
|
||||
return -1;
|
||||
|
4
ssp.hpp
4
ssp.hpp
@ -674,10 +674,10 @@ ssize_t get_line(char** lineptr, size_t* n, FILE* fp) {
|
||||
size_t line_used = strlen(*lineptr);
|
||||
size_t buff_used = strlen(buff);
|
||||
|
||||
if (*n < buff_used + line_used) {
|
||||
if (*n <= buff_used + line_used) {
|
||||
size_t new_n = *n * 2;
|
||||
|
||||
auto new_lineptr = static_cast<char*>(realloc(*lineptr, *n));
|
||||
auto new_lineptr = static_cast<char*>(realloc(*lineptr, new_n));
|
||||
if (new_lineptr == nullptr) {
|
||||
errno = ENOMEM;
|
||||
return -1;
|
||||
|
Loading…
Reference in New Issue
Block a user