Skip to content

packet, sftp: replace strlen() on literals with sizeof() - #2432

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

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

Conversation

@vszakats

@vszakats vszakats commented Jul 25, 2026 •

Copy link
Copy Markdown
Member

This reverts an earlier change doing the opposite. sizeof() determines
length at build-time, while strlen() does the same at runtime. In case
of literals the results are identical, but the build-time option does it
with zero runtime overhead.

Also:

Follow-up to 88c5fd9 #2400
Follow-up to af1b501 #2224
Follow-up to 88a960a #1179

Copilot AI review requested due to automatic review settings July 25, 2026 00:09

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 replaces strlen() calls on string literals with compile-time sizeof("literal") - 1 in packet and SFTP code paths, keeping literal-length calculations constant-folded and avoiding runtime work.

Changes:

  • Update SFTP subsystem startup to pass a compile-time length for "sftp".
  • Replace multiple literal strlen() usages in packet parsing and channel setup with sizeof(...)-1.
  • Update request-type comparisons (exit-status, exit-signal) to use compile-time literal lengths.

Reviewed changes

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

File Description
src/sftp.c Uses sizeof("sftp") - 1 for the subsystem name length when requesting SFTP.
src/packet.c Converts literal-length computations in packet/channel parsing to sizeof(...)-1 and updates request string comparisons accordingly.

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

Comment thread src/packet.c Resolved
Copilot AI review requested due to automatic review settings July 25, 2026 00:26

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 2eeae1e Jul 25, 2026
@vszakats
vszakats deleted the sizeof branch July 25, 2026 00:31
@vszakats

vszakats commented Jul 25, 2026 •

Copy link
Copy Markdown
Member Author

Another "second look" comment: strlen("literal") is optimized
at build-time by most popular compilers. But not all, e.g. MSVC
does it only with /O2.

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