Skip to content

pem, test_hostkey: replace strlen() on literals with sizeof() - #2438

Closed
vszakats wants to merge 2 commits into
libssh2:masterfrom
vszakats:sizeof
Closed

vszakats wants to merge 2 commits into
libssh2:masterfrom
vszakats:sizeof

Conversation

@vszakats

@vszakats vszakats commented Jul 28, 2026 •

Copy link
Copy Markdown
Member

Follow-up to 2eeae1e #2432

@vszakats vszakats changed the title tidy-up: strlen() → sizeof() pem, test_hostkey: replace strlen() on literals with sizeof() Jul 28, 2026
@vszakats
vszakats requested a review from Copilot July 28, 2026 01:13

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

This PR is a follow-up to #2432, continuing the refactor of replacing strlen() calls on string literals with sizeof(...) - 1 to compute lengths at compile time and avoid runtime overhead.

Changes:

  • Update PEM parsing to use an array-backed literal for crypt_annotation and switch to sizeof(...) - 1 for its length.
  • Update test_hostkey expected hostkey literals to array form and use sizeof(...) - 1 when decoding.

Reviewed changes

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

File Description
tests/test_hostkey.c Converts expected hostkey literals to char[] so sizeof(...) - 1 can be used for base64 decode input lengths.
src/pem.c Converts crypt_annotation to char[] and uses sizeof(...) - 1 for the compare length in PEM parsing.

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

Comment thread src/pem.c
Comment on lines 194 to +195
if(passphrase &&
!memcmp(line, crypt_annotation, strlen(crypt_annotation))) {
!memcmp(line, crypt_annotation, sizeof(crypt_annotation) - 1)) {

@vszakats vszakats Jul 28, 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.

There we go again. Separate issue.

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.

The worst case here is matching Proc-Type: 4,ENCRYPTEDSOMETHING in a legacy-encrypted PEM file.

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.

→ PR #2440

@vszakats vszakats closed this in 7bbf17a Jul 28, 2026
@vszakats
vszakats deleted the sizeof branch July 28, 2026 01:20
vszakats added a commit to vszakats/libssh2 that referenced this pull request Jul 28, 2026
Reported by Copilot
libssh2#2438 (comment)
Follow-up to 386e012
vszakats added a commit that referenced this pull request Jul 28, 2026
Before this patch it matched if the header had extra characters appended
to it.

Reported by Copilot
#2438 (comment)
Follow-up to 386e012

Closes #2440
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