Repository navigation
Conversation
There was a problem hiding this comment.
Pull request overview
Fixes readline_memory() offset handling so that when an input line is longer than the destination buffer, the next call resumes at the correct character (instead of skipping one).
Changes:
- Stop unconditionally incrementing
*filedata_offsetafter copying a line fragment. - Only advance the offset when a line terminator (
\n/\r) is actually present at the current offset.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
In `readline_memory()`.
Reported-by GitHub Code Quality
---
```c
int main(void)
{
const char *filedata = "abcdefghijklmnopqrstuvwxyz01234567890";
char line[20] = { 0 };
size_t pos = 0;
readline_memory(line, 20, filedata, strlen(filedata), &pos);
printf("1|%s|\n", line);
readline_memory(line, 20, filedata, strlen(filedata), &pos);
printf("2|%s|\n", line);
readline_memory(line, 20, filedata, strlen(filedata), &pos);
printf("3|%s|\n", line);
printf("z|%zu|\n", pos);
}
```
Original report:
The offset is unconditionally incremented even when no newline/carriage
return is found. If the loop exits because `len == line_size - 1`, this
skips a valid character. Consider only incrementing when a line
terminator is actually consumed from the input.
… buffer" This reverts commit 96531b0.
readline_memory() fixes
readline_memory() fixesreadline_memory() to return error, and to not skip characters
readline_memory() to return error, and to not skip charactersreadline_memory() to detect end of input, and to not skip chars
readline_memory() to detect end of input, and to not skip charsreadline_memory() to detect end of input, not skip chars
|
LGTM. Traced the Confirmed by testing both versions across the relevant cases:
The One nit (non-blocking): in Looks good to merge. |
fix to not skip character after lines longer than 128 bytes.
Reported by GitHub Code Quality
swallow newline characters at the end of the line.
fix infinite loop with certain inputs.
Make the function return error if the whole input has been consumed
(vs. always success before this patch).
Also drop a recently added
*linecheck in favor of the above.Reported-by: afldl on github
Fixes Infinite loop in _libssh2_pem_parse_memory() when END marker follows BEGIN immediately (CWE-835) #2013
Follow-up to 141b035 Added passphrase to _libssh2_pem_parse_memory #1746
Remaining differences noted in
readline()(not touched in this PR):linein cases.E.g. with input
"abcdefghijklmno\rpqrstuvwxyz01234567890"Original report:
The offset is unconditionally incremented even when no newline/carriage return is found. If the loop exits because
len == line_size - 1, this skips a valid character. Consider only incrementing when a line terminator is actually consumed from the input.