mirror of
https://github.com/red0124/ssp.git
synced 2025-01-23 04:55:20 +01:00
Fix get_line possible leak
This commit is contained in:
parent
230da6a3f2
commit
ea21b9ba04
@ -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<char*>(realloc(*lineptr, *n));
|
||||
size_t new_n = sizeof(buff);
|
||||
auto new_lineptr = static_cast<char*>(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<char*>(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);
|
||||
|
Loading…
Reference in New Issue
Block a user