Skip to content

os400qc3: open PEM files in binary mode - #1884

Closed
vszakats wants to merge 6 commits into
libssh2:masterfrom
vszakats:os400qc3-fopen-rb
Closed

vszakats wants to merge 6 commits into
libssh2:masterfrom
vszakats:os400qc3-fopen-rb

Conversation

@vszakats

@vszakats vszakats commented Apr 24, 2026 •

Copy link
Copy Markdown
Member

To make ftell() return a byte offset.

Also:

  • handle ftell() returning -1 when loading DER format.

Follow-up to 0ca33b6 #1883

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

Updates os400qc3 key loading to open PEM/key files in binary mode and simplifies the fopen mode handling.

Changes:

  • Remove static const fopen mode strings ("r", "rb") used for file opens.
  • Open RSA private key files using "rb" for both PEM parsing and DER fallback attempts.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/os400qc3.c Resolved

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.

Comments suppressed due to low confidence (1)

src/os400qc3.c:2145

  • In the DER fallback, filesize is taken directly from ftell(fp) without checking for errors. ftell() can return -1L; in that case filesize <= 32768 will be true and datalen becomes a huge size_t, leading to an unbounded alloca() and potential crash. Check the return values of fseek/ftell, require filesize >= 0, and only proceed when the size is valid.
        fseek(fp, 0L, SEEK_END);
        filesize = ftell(fp);

        if(filesize <= 32768) {        /* Limit to a reasonable size. */
            datalen = filesize;
            data = (unsigned char *) alloca(datalen);

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

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.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/os400qc3.c Resolved

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.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/os400qc3.c
Comment on lines 2141 to +2145
fseek(fp, 0L, SEEK_END);
filesize = ftell(fp);

if(filesize <= 32768) { /* Limit to a reasonable size. */
if(filesize > 0 &&
filesize <= 32768) { /* Limit to a reasonable size. */

Copilot AI Apr 25, 2026

Copy link

Choose a reason for hiding this comment

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

In the DER fallback, fseek(fp, 0L, SEEK_END) and ftell(fp) return values aren’t checked. If fseek() fails, the file position becomes unspecified and ftell() can yield a misleading non-negative value, causing an incorrect filesize to be used. Consider handling errors explicitly (check fseek(...) == 0 and filesize >= 0) similar to _libssh2_pem_parse() in src/pem.c:121-146.

Copilot uses AI. Check for mistakes.

@vszakats vszakats Apr 25, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

sure, but not walking into this rabbit hole at this time. Also: pre-existing issue.

@vszakats

Copy link
Copy Markdown
Member Author

@monnerat Your thoughts on this PR?

@vszakats vszakats closed this in 915ea44 Apr 30, 2026
@vszakats
vszakats deleted the os400qc3-fopen-rb branch April 30, 2026 07:53
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.

2 participants