Skip to content

build: more fixes and tidy-up (mostly for Windows) - #811

Closed
vszakats wants to merge 16 commits into
libssh2:masterfrom
vszakats:more-build-tidy-1
Closed

vszakats wants to merge 16 commits into
libssh2:masterfrom
vszakats:more-build-tidy-1

Conversation

@vszakats

@vszakats vszakats commented Mar 3, 2023 •

Copy link
Copy Markdown
Member
  • cmake: always link ws2_32 on Windows. Also add it to libssh2.pc.

    Fixes Add missing winsock2 link library #745

  • agent: fix gcc compiler warning:
    src/agent.c:296:35: warning: 'snprintf' output truncated before the last format character [-Wformat-truncation=]

  • autotools: fix EVP_aes_128_ctr detection with binutils ld

    The prerequisite for a successful detection is setting LIBS=-lbcrypt
    if the chosen openssl-compatible library requires it, e.g. libressl,
    or quictls/openssl built with -DUSE_BCRYPTGENRANDOM.

    With llvm lld, detection works out of the box. With binutils ld,
    it does not. The reason is lds world-famous pickiness with lib order.

    To fix it, we pass all custom libs before and after the TLS libs.
    This ugly hack makes ld happy and detection succeed.

  • agent: fix Windows-specific warning:
    src/agent.c:318:10: warning: implicit conversion loses integer precision: 'LRESULT' (aka 'long long') to 'int' [-Wshorten-64-to-32]

  • llvm/clang: fix warning:
    src/libssh2_priv.h:987:28: warning: variadic macros are a C99 feature [-Wvariadic-macros]

  • support inline with __GNUC__ (llvm/clang and gcc), fixing:

    src/libssh2_priv.h:990:8: warning: extension used [-Wlanguage-extension-token]
    static inline void
           ^
    
  • blowfish: support inline keyword with MSVC.

    Also switch to __inline__ (from __inline) for __GNUC__:
    https://gcc.gnu.org/onlinedocs/gcc/Inline.html
    https://clang.llvm.org/docs/UsersManual.html#differences-between-various-standard-modes

  • fix MSVC compiler warnings:

    • example\direct_tcpip.c(209): warning C4244: 'function': conversion from 'unsigned int' to 'u_short', possible loss of data
    • tests\session_fixture.c(96): warning C4013: 'getcwd' undefined; assuming extern returning int
    • tests\session_fixture.c(100): warning C4013: 'chdir' undefined; assuming extern returning int
  • delete unused macros:

    • HAVE_SOCKET
    • HAVE_INET_ADDR
    • NEED_LIB_NSL
    • NEED_LIB_SOCKET
    • HAVE_NTSTATUS_H
    • HAVE_NTDEF_H
  • delete stale zlib/openssl version numbers from path defaults.

  • cmake: convert tabs to spaces, add newline at EOFs.

Closes #811

vszakats added 9 commits March 3, 2023 21:47
from gcc:
```
~/libssh2/src/agent.c:296:35: warning: 'snprintf' output truncated before the last format character [-Wformat-truncation=]
  296 |              "PageantRequest%08x%c", (unsigned)GetCurrentThreadId(), '\0');
      |                                   ^
```

Remove this tricky code, that aimed to ensure a terminating zero by
adding an extra one, for Microsoft platforms where we map `snprintf()`
to `_snprintf()`. The latter is famous for not guaranteeing
a terminating zero.

However, this was the single instance in libssh2 with this trick, even
though this particular case doesn't seem to be affected, as the output
shall fit into the buffer always, one terminating zero included.

Refs:

- https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/snprintf-snprintf-snprintf-l-snwprintf-snwprintf-l

  "The _snprintf family of functions only appends a terminating null
  character if the formatted string length is strictly less than count
  characters."

- https://learn.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2012/2ts7cx93(v=vs.110)
@vszakats
vszakats force-pushed the more-build-tidy-1 branch from 1b0c51d to 42b05c7 Compare March 3, 2023 21:48
vszakats added 2 commits March 4, 2023 00:18
The prerequisite for a successful detection is setting `LIBS=-lbcrypt`
if the chosen openssl-compatible library requires it, e.g. libressl
or quictls/openssl built with `-DUSE_BCRYPTGENRANDOM`.

When using `llvm-ld`, detection works out of the box. When using
binutils `ld`, it does not. The reason is `ld`s world-famous pickiness
with lib order.

To fix this case, we pass all custom libs before and after the tls
libs. This ugly hack makes `ld` happy and detection succeeds.
vszakats added a commit to curl/curl-for-win that referenced this pull request Mar 4, 2023
We usually suppress ./configure output for dependencies. Make an
exception with libssh2, to have an obvious way to verify what it
detects. E.g. I found out that gcc builds failed to detect
`EVP_aes_128_ctr`. We switched to llvm/clang long ago, so this
didn't affect curl-for-win binaries.

Ref: libssh2/libssh2#811
@vszakats vszakats changed the title more build fixes and tidy-up (mostly for Windows) build: more fixes and tidy-up (mostly for Windows) Mar 5, 2023
and move `APPEND LIBRARIES ws2_32`s to top `CMakeLists.txt`

Ref: libssh2#745 (comment)
@vszakats
vszakats force-pushed the more-build-tidy-1 branch from f38512b to 972d622 Compare March 6, 2023 15:08
vszakats added 3 commits March 6, 2023 16:18
llvm/clang:
```
./src/libssh2_priv.h:987:28: warning: variadic macros are a C99 feature [-Wvariadic-macros]
                           ^
```

llvm/clang and gcc:
```
./src/libssh2_priv.h:990:8: warning: extension used [-Wlanguage-extension-token]
static inline void
       ^
```

Windows:
```
../../src/agent.c:318:10: warning: implicit conversion loses integer precision: 'LRESULT' (aka 'long long') to 'int' [-Wshorten-64-to-32]
    id = SendMessage(hwnd, WM_COPYDATA, (WPARAM) NULL, (LPARAM) &cds);
       ~ ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
```
@vszakats
vszakats force-pushed the more-build-tidy-1 branch from 521b554 to 385bdae Compare March 7, 2023 00:49
example\direct_tcpip.c(209): warning C4244: 'function': conversion from 'unsigned int' to 'u_short', possible loss of data
tests\session_fixture.c(96): warning C4013: 'getcwd' undefined; assuming extern returning int
tests\session_fixture.c(100): warning C4013: 'chdir' undefined; assuming extern returning int
@vszakats
vszakats force-pushed the more-build-tidy-1 branch from d1360ec to 33a1311 Compare March 7, 2023 12:11
@vszakats vszakats closed this in 31fb886 Mar 7, 2023
@vszakats
vszakats deleted the more-build-tidy-1 branch March 7, 2023 15:15
vszakats added a commit to vszakats/libssh2 that referenced this pull request Mar 19, 2023
libssh2 built with OpenSSL and without its HAVE_EVP_AES_128_CTR option
are working incorrectly. This option wasn't always auto-detected by
autotools up until recently (libssh2#811). non-cmake, non-autotools build
methods never enabled it automatically.

OpenSSL supports this options since at least v1.0.2, which is already
EOLed and considered obsolete. OpenSSL forks (LibreSSL, BoringSSL)
supported it all along.

In this patch we enable this option unconditionally, now requiring
OpenSSL v1.0.2 or newer or a fork.

Also modernize OpenSSL lib references to what 1.0.2 and newer versions
have been using.

Fixes libssh2#739
vszakats added a commit to vszakats/libssh2 that referenced this pull request Mar 19, 2023
libssh2 built with OpenSSL and without its `HAVE_EVP_AES_128_CTR` option
are working incorrectly. This option wasn't always auto-detected by
autotools up until recently (libssh2#811). non-cmake, non-autotools build
methods never enabled it automatically.

OpenSSL supports this options since at least v1.0.2, which is already
EOLed and considered obsolete. OpenSSL forks (LibreSSL, BoringSSL)
supported it all along.

In this patch we enable this option unconditionally, now requiring
OpenSSL v1.0.2 or newer or a fork.

Also modernize OpenSSL lib references to what 1.0.2 and newer versions
have been using.

Fixes libssh2#739
vszakats added a commit to vszakats/libssh2 that referenced this pull request Mar 19, 2023
libssh2 built with OpenSSL and without its `HAVE_EVP_AES_128_CTR` option
are working incorrectly. This option wasn't always auto-detected by
autotools up until recently (libssh2#811). Non-cmake, non-autotools build
methods never enabled it automatically.

OpenSSL supports this options since at least v1.0.2, which is already
EOLed and considered obsolete. OpenSSL forks (LibreSSL, BoringSSL)
supported it all along.

In this patch we enable this option unconditionally, now requiring
OpenSSL v1.0.2 or newer or a fork.

Also modernize OpenSSL lib references to what 1.0.2 and newer versions
have been using.

Fixes libssh2#739
vszakats added a commit to vszakats/libssh2 that referenced this pull request Mar 19, 2023
libssh2 built with OpenSSL and without its `EVP_aes_128_ctr()`, aka
`HAVE_EVP_AES_128_CTR` option are working incorrectly. This option
wasn't always auto-detected by autotools up until recently (libssh2#811).
Non-cmake, non-autotools build methods never enabled it automatically.

OpenSSL supports this options since at least v1.0.2, which is already
EOLed and considered obsolete. OpenSSL forks (LibreSSL, BoringSSL)
supported it all along.

In this patch we enable this option unconditionally, now requiring
OpenSSL supporting this function, or one of its forks.

Also modernize OpenSSL lib references to what 1.0.2 and newer versions
have been using.

Fixes libssh2#739
vszakats added a commit to vszakats/libssh2 that referenced this pull request Mar 19, 2023
libssh2 built with OpenSSL and without its `EVP_aes_128_ctr()`, aka
`HAVE_EVP_AES_128_CTR`, option are working incorrectly. This option
wasn't always auto-detected by autotools up until recently (libssh2#811).
Non-cmake, non-autotools build methods never enabled it automatically.

OpenSSL supports this options since at least v1.0.2, which is already
EOLed and considered obsolete. OpenSSL forks (LibreSSL, BoringSSL)
supported it all along.

In this patch we enable this option unconditionally, now requiring
OpenSSL supporting this function, or one of its forks.

Also modernize OpenSSL lib references to what 1.0.2 and newer versions
have been using.

Fixes libssh2#739
vszakats added a commit to vszakats/libssh2 that referenced this pull request Mar 19, 2023
libssh2 built with OpenSSL and without its `EVP_aes_128_ctr()`, aka
`HAVE_EVP_AES_128_CTR`, option are working incorrectly. This option
wasn't always auto-detected by autotools up until recently (libssh2#811).
Non-cmake, non-autotools build methods never enabled it automatically.
No build method auto-detected or enabled it for wolfSSL either.

OpenSSL supports this options since at least v1.0.2, which is already
EOLed and considered obsolete. OpenSSL forks (LibreSSL, BoringSSL)
supported it all along.

In this patch we enable this option unconditionally, now requiring
OpenSSL supporting this function, or one of its forks, or wolfSSL.

Also modernize OpenSSL lib references to what 1.0.2 and newer versions
have been using.

Fixes libssh2#739
vszakats added a commit that referenced this pull request Mar 20, 2023
libssh2 built with OpenSSL and without its `EVP_aes_128_ctr()`, aka
`HAVE_EVP_AES_128_CTR`, option are working incorrectly. This option
wasn't always auto-detected by autotools up until recently (#811).
Non-cmake, non-autotools build methods never enabled it automatically.

OpenSSL supports this options since at least v1.0.2, which is already
EOLed and considered obsolete. OpenSSL forks (LibreSSL, BoringSSL)
supported it all along.

In this patch we enable this option unconditionally, now requiring
OpenSSL supporting this function, or one of its forks.

Also modernize OpenSSL lib references to what 1.0.2 and newer versions
have been using.

Fixes #739
agreppin pushed a commit to agreppin/libssh2 that referenced this pull request Jul 14, 2024
- cmake: always link `ws2_32` on Windows. Also add it to `libssh2.pc`.

   Fixes libssh2#745

- agent: fix gcc compiler warning:
   `src/agent.c:296:35: warning: 'snprintf' output truncated before the last format character [-Wformat-truncation=]`

- autotools: fix `EVP_aes_128_ctr` detection with binutils `ld`

   The prerequisite for a successful detection is setting
   `LIBS=-lbcrypt` if the chosen openssl-compatible library requires
   it, e.g. libressl, or quictls/openssl built with
   `-DUSE_BCRYPTGENRANDOM`.

   With llvm `lld`, detection works out of the box. With binutils `ld`,
   it does not. The reason is `ld`s world-famous pickiness with lib
   order.

   To fix it, we pass all custom libs before and after the TLS libs.
   This ugly hack makes `ld` happy and detection succeed.

- agent: fix Windows-specific warning:
  `src/agent.c:318:10: warning: implicit conversion loses integer precision: 'LRESULT' (aka 'long long') to 'int' [-Wshorten-64-to-32]`

- src: fix llvm/clang compiler warning:
  `src/libssh2_priv.h:987:28: warning: variadic macros are a C99 feature [-Wvariadic-macros]`

- src: support `inline` with `__GNUC__` (llvm/clang and gcc), fixing:
  ```
  src/libssh2_priv.h:990:8: warning: extension used [-Wlanguage-extension-token]
  static inline void
         ^
  ```

- blowfish: support `inline` keyword with MSVC.

   Also switch to `__inline__` (from `__inline`) for `__GNUC__`:
     https://gcc.gnu.org/onlinedocs/gcc/Inline.html
     https://clang.llvm.org/docs/UsersManual.html#differences-between-various-standard-modes

- example/test: fix MSVC compiler warnings:

  - `example\direct_tcpip.c(209): warning C4244: 'function': conversion from 'unsigned int' to 'u_short', possible loss of data`
  - `tests\session_fixture.c(96): warning C4013: 'getcwd' undefined; assuming extern returning int`
  - `tests\session_fixture.c(100): warning C4013: 'chdir' undefined; assuming extern returning int`

- delete unused macros:
  - `HAVE_SOCKET`
  - `HAVE_INET_ADDR`
  - `NEED_LIB_NSL`
  - `NEED_LIB_SOCKET`
  - `HAVE_NTSTATUS_H`
  - `HAVE_NTDEF_H`

- build: delete stale zlib/openssl version numbers from path defaults.

- cmake: convert tabs to spaces, add newline at EOFs.

Closes libssh2#811
agreppin pushed a commit to agreppin/libssh2 that referenced this pull request Jul 14, 2024
libssh2 built with OpenSSL and without its `EVP_aes_128_ctr()`, aka
`HAVE_EVP_AES_128_CTR`, option are working incorrectly. This option
wasn't always auto-detected by autotools up until recently (libssh2#811).
Non-cmake, non-autotools build methods never enabled it automatically.

OpenSSL supports this options since at least v1.0.2, which is already
EOLed and considered obsolete. OpenSSL forks (LibreSSL, BoringSSL)
supported it all along.

In this patch we enable this option unconditionally, now requiring
OpenSSL supporting this function, or one of its forks.

Also modernize OpenSSL lib references to what 1.0.2 and newer versions
have been using.

Fixes libssh2#739
vszakats added a commit to vszakats/libssh2 that referenced this pull request Jul 13, 2025
The `libssh2.pc` generator logic automatically adds `-lws2_32` while
parsing `LIBSSH2_LIBS`, which contains this lib already. Then discard
the duplicate.

This change introduces a change in the position of `ws2_32` within
the lib list.

This order might in cases by significant, but:

- libssh2 no longer links against `libssl`, which was the library
  also referencing `ws2_32` and breaking picky binutils `ld` linker
  when not passed in strict dependency order.
  Ref: c84745e libssh2#1128

- since switching to INTERFACE targets, cmake messes up the lib order
  anyway, adding `OpenSSL:Crypto` last, instead of `ws2_32`. This did
  not seem to cause an issue so far.
  Ref: df0563a libssh2#1535

Ref: 33b6d5f libssh2#827
Ref: 31fb886 libssh2#811
vszakats added a commit that referenced this pull request Jul 13, 2025
The `libssh2.pc` generator logic automatically adds `-lws2_32` while
parsing `LIBSSH2_LIBS`, which contains this lib already. Then discard
the duplicate.

This patch introduces a change in the position of `ws2_32` within
the lib list advertised via `libssh2.pc` for static builds.

This order might in cases by significant, but:

- libssh2 no longer links against `libssl`, which was the library
  also referencing `ws2_32` and breaking picky binutils `ld` linker
  when not passed in strict dependency order.
  Ref: c84745e #1128

- since switching to INTERFACE targets, cmake messes up the lib order
  anyway, adding `OpenSSL:Crypto` last, instead of `ws2_32`. This did
  not seem to cause an issue so far.
  Ref: df0563a #1535

For these reasons it seems unlikely this position change could break
Windows OpenSSL static gcc/ld builds relying on `libssh2.pc` for their
lib list. It least no more than it was before this patch.

Turns out these theories don't stand in practice and the order is broken
possibly by introducing INTERFACE targets, with or without this patch.
`libcrypto` (tested with LibreSSL) is in fact depending on `ws2_32`, and
the `bcrypt` dependency is also causing breakage with picky binutils ld.
I may try addressing it in a separate PR.

Ref: 33b6d5f #827
Ref: 31fb886 #811

Closes #1619
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.

1 participant