Skip to content

tidy-up: drop stray comparisons with literal zero - #2014

Closed
vszakats wants to merge 4 commits into
libssh2:masterfrom
vszakats:eqzero
Closed

vszakats wants to merge 4 commits into
libssh2:masterfrom
vszakats:eqzero

Conversation

@vszakats

Copy link
Copy Markdown
Member

Drop from:

  • strcmp, strncmp, strlen, memcmp, fseek, fstat

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 performs a repository-wide tidy-up to remove explicit comparisons against literal zero for common C library functions (e.g., strcmp(...) == 0 → !strcmp(...), memcmp(...) != 0 → memcmp(...)), including a few fseek/fstat checks.

Changes:

  • Replace == 0 / != 0 comparisons for strcmp, strncmp, memcmp, strlen, fseek, and fstat with idiomatic truthy/falsey checks.
  • Apply the same style cleanup in tests and example programs to keep consistency.
  • (Issue found) One updated strncmp check in the Windows CNG backend is inverted and would reject valid keys.

Reviewed changes

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

Show a summary per file
File Description
vms/man2help.c Uses !strncmp(...) for section header matching.
tests/test_simple.c Simplifies strcmp equality check in base64 decode test.
tests/test_hostkey.c Simplifies memcmp result check for hostkey equality.
tests/test_hostkey_hash.c Simplifies multiple digest strcmp checks.
tests/test_auth_keyboard_info_request.c Simplifies strcmp check for expected error message.
tests/session_fixture.c Simplifies several strcmp checks in fixture setup/auth.
tests/openssh_fixture.c Simplifies strcmp check for Docker “0.0.0.0” handling.
src/wincng.c Simplifies string/memory comparisons; contains an inverted strncmp check.
src/userauth.c Simplifies strncmp/memcmp equality checks in auth/key-sign algorithm selection.
src/sftp.c Simplifies strncmp check for SFTP extension name.
src/publickey.c Simplifies strncmp checks when parsing response codes/attributes.
src/pem.c Simplifies fseek/strcmp/memcmp/strncmp/strcmp checks in PEM parsing paths.
src/packet.c Simplifies multiple memcmp comparisons used for packet/type matching.
src/os400qc3.c Simplifies memcmp equality check for key method name.
src/openssl.c Simplifies many algorithm strcmp/memcmp equality checks.
src/misc.c Simplifies strncmp mismatch check in ssh2_match_string.
src/mbedtls.c Simplifies strcmp/fseek/memcmp checks in the mbedTLS backend.
src/libgcrypt.c Simplifies memcmp equality check for key method name.
src/kex.c Simplifies strcmp/strncmp equality checks in KEX method matching.
src/hostkey.c Simplifies strncmp equality/mismatch checks for hostkey parsing.
example/x11.c Simplifies strncmp check for DISPLAY parsing.
example/ssh2_exec.c Simplifies strlen(password) non-empty check.
example/ssh2_echo.c Simplifies strlen(password) non-empty check.
example/scp_write.c Simplifies fstat error check.
example/scp_write_nonblock.c Simplifies fstat error check.

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

Comment thread src/wincng.c Resolved

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

Comment thread src/pem.c
Comment on lines 452 to 456

if(strncmp((const char *)decoded.dataptr, AUTH_MAGIC,
strlen(AUTH_MAGIC)) != 0) {
strlen(AUTH_MAGIC))) {
ret = ssh2_err(session, LIBSSH2_ERROR_PROTO,
"key auth magic mismatch");

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.

→ Fix attempt #2022

Comment thread src/wincng.c
Comment on lines 2780 to 2784
/* Read OPENSSL_PRIVATEKEY_AUTH_MAGIC */
if(strncmp(data, OPENSSL_PRIVATEKEY_AUTH_MAGIC, data_len) != 0) {
if(strncmp(data, OPENSSL_PRIVATEKEY_AUTH_MAGIC, data_len)) {
result = -1;
goto cleanup;
}

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.

→ Fix attempt #2022

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