Skip to content

pem: fix readline_memory() to detect end of input, not skip chars - #2032

Closed
vszakats wants to merge 7 commits into
libssh2:masterfrom
vszakats:pemline
Closed

vszakats wants to merge 7 commits into
libssh2:masterfrom
vszakats:pemline

Conversation

@vszakats

@vszakats vszakats commented Jun 10, 2026 •

Copy link
Copy Markdown
Member

Remaining differences noted in readline() (not touched in this PR):

  • it returns newline characters in line in cases.
    E.g. with input "abcdefghijklmno\rpqrstuvwxyz01234567890"
  • it does not empty the return buffer on EOF.

int main(void)
{
    const char *filedata = "abcdefghijklmnopqrstuvwxyz01234567890";
    char line[20] = { 0 };
    int r;
    size_t pos = 0;
    r = readline_memory(line, 20, filedata, strlen(filedata), &pos); printf("|%d|%s", r, line);
    r = readline_memory(line, 20, filedata, strlen(filedata), &pos); printf("|%d|%s", r, line);
    r = readline_memory(line, 20, filedata, strlen(filedata), &pos); printf("|%d|%s", r, line);
    r = readline_memory(line, 20, filedata, strlen(filedata), &pos); printf("|%d|%s", r, line);
}

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.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_offset after 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.

Comment thread src/pem.c Resolved
@vszakats
vszakats marked this pull request as draft June 11, 2026 02:18
vszakats added 4 commits June 11, 2026 04:38
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.
@vszakats vszakats changed the title pem: fix to not skip last character of lines longer than line buffer pem: readline_memory() fixes Jun 11, 2026
@vszakats vszakats changed the title pem: readline_memory() fixes pem: fix readline_memory() to return error, and to not skip characters Jun 11, 2026
@vszakats
vszakats requested a review from Copilot June 11, 2026 09:47
@vszakats vszakats changed the title pem: fix readline_memory() to return error, and to not skip characters pem: fix readline_memory() to detect end of input, and to not skip chars Jun 11, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 1 comment.

Comment thread src/pem.c Resolved
@vszakats
vszakats requested a review from Copilot June 11, 2026 10:05
@vszakats vszakats changed the title pem: fix readline_memory() to detect end of input, and to not skip chars pem: fix readline_memory() to detect end of input, not skip chars Jun 11, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 1 comment.

Comment thread src/pem.c Resolved
@vszakats
vszakats requested a review from Copilot June 11, 2026 10:17
@vszakats
vszakats marked this pull request as ready for review June 11, 2026 10:18

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated no new comments.

@afldl

afldl commented Jun 11, 2026

Copy link
Copy Markdown

LGTM. Traced the readline_memory change against master — the unconditional *filedata_offset += 1 was the real root cause behind #2013, and fixing it once at the source beats patching every caller.

Confirmed by testing both versions across the relevant cases:

The *filedata_offset > off ? 0 : -1 EOF check is sound — offset only advances when bytes are copied or a \r/\n is consumed, so there's no way to spin with ret==0 at EOF. Dropping the if(!*line) break; in the first loop is safe for the same reason.

One nit (non-blocking): in openssh_pem_parse_memory the new -1 path is just ret = -1; goto out;, missing the ssh2_err() string the other error branches have. Would be nice to match, but functionally correct either way.

Looks good to merge.

@vszakats vszakats closed this in 8e8e3fc Jun 11, 2026
@vszakats
vszakats deleted the pemline branch June 11, 2026 17:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Infinite loop in _libssh2_pem_parse_memory() when END marker follows BEGIN immediately (CWE-835)

3 participants