Skip to content

crypto: constify ssh2_dh_validate() args - #2436

Closed
vszakats wants to merge 2 commits into
libssh2:masterfrom
vszakats:constify
Closed

vszakats wants to merge 2 commits into
libssh2:masterfrom
vszakats:constify

Conversation

@vszakats

Copy link
Copy Markdown
Member

Ref: #2402
Follow-up to fcf8307 #2239

Copilot AI review requested due to automatic review settings July 27, 2026 11:56

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 tightens the internal DH validation helper API by making ssh2_dh_validate() treat its inputs as read-only across the OpenSSL, mbedTLS, and libgcrypt backends, aligning intent with usage and reducing accidental mutation risk.

Changes:

  • Update ssh2_dh_validate() definitions in OpenSSL, mbedTLS, and libgcrypt backends to take const ssh2_bn * arguments.
  • Update the shared prototype in src/crypto.h accordingly (unit-test helper).

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
src/openssl.c Const-qualify ssh2_dh_validate() parameters in the OpenSSL/WolfSSL backend implementation.
src/mbedtls.c Const-qualify ssh2_dh_validate() parameters in the mbedTLS backend implementation.
src/libgcrypt.c Const-qualify ssh2_dh_validate() parameters in the libgcrypt backend implementation.
src/crypto.h Const-qualify the ssh2_dh_validate() prototype used by unit tests.

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

Copilot AI review requested due to automatic review settings July 27, 2026 12:01
To fix:
```
/home/runner/work/libssh2/libssh2/src/libgcrypt.c:704:24: error: passing 'const struct gcry_mpi *' to parameter of type 'gcry_mpi_t' (aka 'struct gcry_mpi *') discards qualifiers [-Werror,-Wincompatible-pointer-types-discards-qualifiers]
  704 |     if(gcry_mpi_cmp_ui(f, 1) <= 0)
      |                        ^
/usr/include/gcrypt.h:614:39: note: passing argument to parameter 'u' here
  614 | int gcry_mpi_cmp_ui (const gcry_mpi_t u, unsigned long v);
      |                                       ^
In file included from /home/runner/work/libssh2/libssh2/bld/src/CMakeFiles/libssh2_static.dir/Unity/unity_0_c.c:40:
/home/runner/work/libssh2/libssh2/src/libgcrypt.c:710:26: error: passing 'const struct gcry_mpi *' to parameter of type 'gcry_mpi_t' (aka 'struct gcry_mpi *') discards qualifiers [-Werror,-Wincompatible-pointer-types-discards-qualifiers]
  710 |     gcry_mpi_sub_ui(tmp, p, 1);
      |                          ^
/usr/include/gcrypt.h:662:48: note: passing argument to parameter 'u' here
  662 | void gcry_mpi_sub_ui (gcry_mpi_t w, gcry_mpi_t u, unsigned long v );
      |                                                ^
In file included from /home/runner/work/libssh2/libssh2/bld/src/CMakeFiles/libssh2_static.dir/Unity/unity_0_c.c:40:
/home/runner/work/libssh2/libssh2/src/libgcrypt.c:711:21: error: passing 'const struct gcry_mpi *' to parameter of type 'gcry_mpi_t' (aka 'struct gcry_mpi *') discards qualifiers [-Werror,-Wincompatible-pointer-types-discards-qualifiers]
  711 |     if(gcry_mpi_cmp(f, tmp) >= 0) {
      |                     ^
/usr/include/gcrypt.h:609:36: note: passing argument to parameter 'u' here
  609 | int gcry_mpi_cmp (const gcry_mpi_t u, const gcry_mpi_t v);
      |                                    ^
In file included from /home/runner/work/libssh2/libssh2/bld/src/CMakeFiles/libssh2_static.dir/Unity/unity_0_c.c:40:
/home/runner/work/libssh2/libssh2/src/libgcrypt.c:717:39: error: passing 'const struct gcry_mpi *' to parameter of type 'gcry_mpi_t' (aka 'struct gcry_mpi *') discards qualifiers [-Werror,-Wincompatible-pointer-types-discards-qualifiers]
  717 |     for(i = 0, n = gcry_mpi_get_nbits(f), bits_set = 0; i < n; ++i)
      |                                       ^
/usr/include/gcrypt.h:775:45: note: passing argument to parameter 'a' here
  775 | unsigned int gcry_mpi_get_nbits (gcry_mpi_t a);
      |                                             ^
In file included from /home/runner/work/libssh2/libssh2/bld/src/CMakeFiles/libssh2_static.dir/Unity/unity_0_c.c:40:
/home/runner/work/libssh2/libssh2/src/libgcrypt.c:718:30: error: passing 'const struct gcry_mpi *' to parameter of type 'gcry_mpi_t' (aka 'struct gcry_mpi *') discards qualifiers [-Werror,-Wincompatible-pointer-types-discards-qualifiers]
  718 |         if(gcry_mpi_test_bit(f, i))
      |                              ^
/usr/include/gcrypt.h:778:40: note: passing argument to parameter 'a' here
  778 | int      gcry_mpi_test_bit (gcry_mpi_t a, unsigned int n);
      |                                        ^
5 errors generated.
```
Ref: https://github.com/libssh2/libssh2/actions/runs/30263895105/job/89969933303?pr=2436

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 4 out of 4 changed files in this pull request and generated 1 comment.

Comments suppressed due to low confidence (1)

src/libgcrypt.c:718

  • gcry_mpi_get_nbits() and gcry_mpi_test_bit() take a non-const gcry_mpi_t; after changing ssh2_dh_validate() to accept const ssh2_bn*, the argument needs the same un-const cast used elsewhere to avoid const-discard warnings (often fatal under -Werror).
    for(i = 0, n = gcry_mpi_get_nbits(SSH2_UNCONST(f)), bits_set = 0; i < n;
        ++i)

Comment thread src/libgcrypt.c Resolved
Copilot AI review requested due to automatic review settings July 27, 2026 12:04

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

@vszakats vszakats closed this in 199a459 Jul 27, 2026
@vszakats
vszakats deleted the constify branch July 27, 2026 12:17
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