Skip to content

publickey: flatten bounds check if blocks (tidy-up) - #2203

Closed
vszakats wants to merge 1 commit into
libssh2:masterfrom
vszakats:pubkey-simplify-ifs
Closed

vszakats wants to merge 1 commit into
libssh2:masterfrom
vszakats:pubkey-simplify-ifs

Conversation

@vszakats

@vszakats vszakats commented Jul 4, 2026 •

Copy link
Copy Markdown
Member

Reverse conditions and branch on fail, move success paths onto
the main block (from the else blocks prior to this patch).

To shorten, improve readability and use the same pattern everywhere.


https://github.com/libssh2/libssh2/pull/2203/files?w=1

@vszakats
vszakats marked this pull request as draft July 4, 2026 10:36
@vszakats
vszakats force-pushed the pubkey-simplify-ifs branch from 6019c9b to ed474b6 Compare July 4, 2026 10:39
@vszakats
vszakats requested a review from Copilot July 4, 2026 10:40
@vszakats
vszakats marked this pull request as ready for review July 4, 2026 10:40

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 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 else blocks in publickey_init() and libssh2_publickey_list_fetch().

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

Comment thread src/publickey.c Outdated
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;
}

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.

Ref: c02d9b7

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 #2204 e3fba52

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).

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 4 comments.

Comment thread src/publickey.c
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 thread src/publickey.c
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 thread src/publickey.c
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;

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

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 thread src/publickey.c
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;
@vszakats vszakats changed the title publickey: simplify sanity checks (tidy-up) publickey: flatten bounds check if blocks (tidy-up) Jul 4, 2026
@vszakats vszakats closed this in 3b8d6dd Jul 4, 2026
@vszakats
vszakats deleted the pubkey-simplify-ifs branch July 4, 2026 12:04
vszakats added a commit that referenced this pull request Jul 4, 2026
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