Skip to content

Commit

Permalink
Merge pull request #34 from red0124/improvement/getline_update
Browse files Browse the repository at this point in the history
Fix reallocation issues with non-POSIX get_line
  • Loading branch information
red0124 authored Feb 23, 2024
2 parents f8fdb97 + c0d3087 commit a27fd12
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
4 changes: 2 additions & 2 deletions include/ss/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions ssp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit a27fd12

Please sign in to comment.