Skip to content

misc: add non-unsigned char variant for ssh2_get_string() - #2424

Closed
vszakats wants to merge 3 commits into
libssh2:masterfrom
vszakats:get-string-signed-char
Closed

vszakats wants to merge 3 commits into
libssh2:masterfrom
vszakats:get-string-signed-char

Conversation

@vszakats

@vszakats vszakats commented Jul 24, 2026 •

Copy link
Copy Markdown
Member

Also use it from ssh2_match_string() to reduce casts.

Apparently casting the return pointer of ssh2_get_string() buffer is
frowned upon, though existing practice and not completelely clear if UB
or not.

Current practice (with two exceptions, now reduced to one in WinCNG
code) is to declare the return buffer as unsigned char *, then cast
that result everywhere on use. It results in excessive casts. The
frowned-upon alternative to avoid this, is to declare as char * and
pass it to ssh2_get_string() as (unsigned char **)&data. This new
API avoid this, and allows to declare as char *data, retrieve it
without cast, and use it without casts too.

Ref: #2420
Ref: a7d05f9 #2422

Also use it from `ssh2_match_string()` to reduce casts.

Apparently casting the return pointer of `ssh2_get_string()` string is
frowned upon, though existing practice and not completelely clear if UB
or not.

Ref: libssh2#2420
Copilot AI review requested due to automatic review settings July 24, 2026 11:22

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 introduces a char **-returning variant of ssh2_get_string() to centralize the cast from the underlying unsigned char * buffer, and updates ssh2_match_string() to use the new helper to reduce call-site casting during SSH packet parsing.

Changes:

  • Add ssh2_get_schars() declaration to the internal misc header.
  • Implement ssh2_get_schars() in src/misc.c.
  • Switch ssh2_match_string() to use ssh2_get_schars() and remove the call-site cast.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
src/misc.h Declares the new ssh2_get_schars() helper.
src/misc.c Implements ssh2_get_schars() and uses it in ssh2_match_string().

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

Comment thread src/misc.c Resolved
Comment thread src/misc.c
Comment on lines +899 to 903
char *out;
size_t len = 0;
if(ssh2_get_string(buf, &out, &len) || len != strlen(match) ||
strncmp((const char *)out, match, strlen(match)))
if(ssh2_get_schars(buf, &out, &len) || len != strlen(match) ||
strncmp(out, match, strlen(match)))
return -1;

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.

Yeah, future PR. Though with 4 callers in non-hot path, this is picky of the picky, but why not.

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 #2425

Comment thread src/misc.h Resolved
@vszakats vszakats changed the title misc: add signed char variant for ssh2_get_string() misc: add non-unsigned char variant for ssh2_get_string() Jul 24, 2026
Copilot AI review requested due to automatic review settings July 24, 2026 11:43

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 2 out of 2 changed files in this pull request and generated no new comments.

@vszakats vszakats closed this in 907fe84 Jul 24, 2026
@vszakats
vszakats deleted the get-string-signed-char branch July 24, 2026 11:48
vszakats added a commit that referenced this pull request Jul 24, 2026
By using the recently added non-unsigned internal API variant.

Follow-up to 907fe84 #2424
Follow-up to 3e72343 #1315

Closes #2426
vszakats added a commit that referenced this pull request Jul 24, 2026
- change types to match common usage to reduce casts.
  (mostly `unsigned char *` to `char *`)
- use non-unsigned variant of `ssh2_get_string()`.
- use `void *` arg in `kex_value_hash()` to avoid casts.
- packet: replace magic number with `sizeof()` in `ssh2_packet_add()`,
  also make the constant static.
- drop redundant casts missed earlier.

Follow-up to 907fe84 #2424

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