Repository navigation
Conversation
vszakats
marked this pull request as draft
July 4, 2026 10:36
vszakats
force-pushed
the
pubkey-simplify-ifs
branch
from
July 4, 2026 10:39
6019c9b to
ed474b6
Compare
vszakats
marked this pull request as ready for review
July 4, 2026 10:40
There was a problem hiding this comment.
Pull request overview
This PR refactors parsing “sanity checks” in the publickey subsystem by inverting conditions and using early error branches, aiming to reduce nesting and improve readability while preserving behavior.
Changes:
- Invert several length/bounds checks to
if (invalid) goto err_exit;patterns. - Move success-path parsing statements out of
elseblocks inpublickey_init()andlibssh2_publickey_list_fetch().
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
431
to
435
| if(session->pkeyInit_data_len < 8) { | ||
| ssh2_err(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL, | ||
| "Public key init data too small"); | ||
| goto err_exit; | ||
| } |
vszakats
added a commit
that referenced
this pull request
Jul 4, 2026
Fix by applying the pattern used in subsequent checks. "The status-response parsing checks `session->pkeyInit_data_len < 8`, but `s` has already been advanced by `publickey_response_id()`. On a malformed packet where the total length is >= 8 but the remaining bytes from `s` are < 8, this will read past the end when decoding `status`/`descr_len` (and the later checks occur after those reads). Please validate the remaining bytes relative to `s` before calling `ssh2_ntohu32()`." Reported by Copilot Bug: #2203 (comment) Follow-up to c02d9b7
Reverse conditions and branch on failure, move success paths onto the main block (from the `else` blocks prior to this patch).
vszakats
force-pushed
the
pubkey-simplify-ifs
branch
from
July 4, 2026 11:52
ed474b6 to
91ffbd8
Compare
Comment on lines
+431
to
435
| if(s + 8 > | ||
| session->pkeyInit_data + session->pkeyInit_data_len) { | ||
| status = ssh2_ntohu32(s); | ||
| s += 4; | ||
| descr_len = ssh2_ntohu32(s); | ||
| s += 4; | ||
| } | ||
| else { | ||
| ssh2_err(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL, | ||
| "Public key init data too small"); | ||
| goto err_exit; |
Comment on lines
+453
to
457
| if(s + lang_len > | ||
| session->pkeyInit_data + session->pkeyInit_data_len) { | ||
| /* lang starts here */ | ||
| s += lang_len; | ||
| } | ||
| else { | ||
| ssh2_err(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL, | ||
| "Public key init data too small"); | ||
| goto err_exit; |
Comment on lines
+839
to
843
| if(pkey->listFetch_s + 8 > | ||
| pkey->listFetch_data + pkey->listFetch_data_len) { | ||
| status = ssh2_ntohu32(pkey->listFetch_s); | ||
| pkey->listFetch_s += 4; | ||
| descr_len = ssh2_ntohu32(pkey->listFetch_s); | ||
| pkey->listFetch_s += 4; | ||
| } | ||
| else { | ||
| ssh2_err(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL, | ||
| "ListFetch data too short"); | ||
| goto err_exit; |
Member
Author
There was a problem hiding this comment.
First recommending this pattern in the previous check:
#2203 (comment)
then, with that applied, freaking out about it in the next check.
Saying "annoying" doesn't really express the feeling of this
neverending grind of a chain of fixes.
See also: #2204 (comment)
It's all pre-existing.
Comment on lines
+861
to
865
| if(pkey->listFetch_s + lang_len > | ||
| pkey->listFetch_data + pkey->listFetch_data_len) { | ||
| /* lang starts at pkey->listFetch_s */ | ||
| pkey->listFetch_s += lang_len; | ||
| } | ||
| else { | ||
| ssh2_err(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL, | ||
| "ListFetch data too short"); | ||
| goto err_exit; |
if blocks (tidy-up)
vszakats
added a commit
that referenced
this pull request
Jul 4, 2026
Bug: #2204 (comment) Bug: #2203 (comment) (and the rest below) Closes #2205
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Reverse conditions and branch on fail, move success paths onto
the main block (from the
elseblocks prior to this patch).To shorten, improve readability and use the same pattern everywhere.
https://github.com/libssh2/libssh2/pull/2203/files?w=1