Skip to content

Make compilable in ISO C mode - #1674

Closed
tambry wants to merge 4 commits into
libssh2:masterfrom
tambry:c_ext
Closed

tambry wants to merge 4 commits into
libssh2:masterfrom
tambry:c_ext

Conversation

@tambry

@tambry tambry commented Sep 24, 2025

Copy link
Copy Markdown

If the compiler defaults to non-GNU mode _DEFAULT_SOURCE needs to be explicitly defined for Linux headers to expose definitions for various POSIX extensions.

@vszakats vszakats added the build label Sep 24, 2025
@vszakats

Copy link
Copy Markdown
Member

A skim across projects around libssh2 and curl, only LibreSSL sets it, when CMAKE_SYSTEM_NAME MATCHES "Linux".

Why do we want to to automate this in libssh2 specifically?

@tambry

tambry commented Sep 25, 2025 •

Copy link
Copy Markdown
Author

curl doesn't seem to use any extended POSIX functions that'd require this.
Don't know which others you're referring to so I can't comment on those.

But to explain better: libssh2 compilation currently assumes extended POSIX functions are always available. In practice this means the assumption that that the compiler compiles using -std=gnu11 vs -std=c11 (or whatever version). There's no guarantee for that and that's well supported by CMake. I'm using a vendored LLVM toolchain, which defaults to ISO C instead of GNU mode. As such __STRICT_ANSI__ gets defined, _DEFAULT_SOURCE doesn't get defined by /usr/include/features.h, relevant functions won't be declared and libssh2 fails to compile. Most software that does require these functions (e.g. CMake, LLVM) takes care to explicitly enable them.

There's three ways to approach fixing this:

  1. Define _DEFAULT_SOURCE (or an appropriate combination of _BSD_SOURCE, _XOPEN_SOURCE, _GNU_SOURCE, _POSIX_SOURCE, ...) globally.
  2. Define those per-file as necessary.
  3. Make compilation is done with extensions (i.e. set C_EXTENSIONS=ON).
    Has the downside that you might end up making your code accidentally non-portable unless CI tests without extensions.

I've gone with the first approach as that was the easiest and ensures that such non-portable occurrences are harder to re-introduce by the virtue of just globally enabling it everywhere. It might however be reasonable to just introduce a CI combination that uses -DCMAKE_C_EXTENSIONS=OFF to catch this.

@tambry

tambry commented Sep 25, 2025

Copy link
Copy Markdown
Author

only LibreSSL sets it, when CMAKE_SYSTEM_NAME MATCHES "Linux"

I could probably scope similarly here.
I just don't have any non-Linux systems to verify if it's necessary there too (LibreSSL's check would suggest not).
Though then again it probably just has no effect elsewhere.

@vszakats

vszakats commented Sep 25, 2025 •

Copy link
Copy Markdown
Member

I meant to refer to libssh2 and curl dependencies, sorry for being vague.

Can you say what errors do you see? I tried with CMAKE_C_EXTENSIONS=OFF on Linux, macOS and Windows 1, but all built cleanly. Then tried building with -std=c89 and it brought a bunch of missing types (e.g. u_char) in the chacha code (on Linux, macOS. did not test Windows). Is that what you're seeing?

Footnotes

  1. FTR only Linux had references to _DEFAULT_SOURCE in system headers. ↩

@tambry

tambry commented Sep 26, 2025 •

Copy link
Copy Markdown
Author

FTR only Linux had references to _DEFAULT_SOURCE in system headers.

Changed the checks to CMAKE_SYSTEM_NAME MATCHES "Linux".

Can you say what errors do you see? I tried with CMAKE_C_EXTENSIONS=OFF on Linux, macOS and Windows 1, but all built cleanly.

Right, forgot about this. CMake's standard flag selection logic used to be quite buggy. This included <LANG>_EXTENSIONS=OFF not actually turning off extensions if they were enabled by default by the given compiler, it only worked in conjunction with <LANG>_STANDARD_REQUIRED=ON.
I fixed most of these cases in CMP0128. libssh2 uses a pretty old policy range so bumping the range to at least 3.22 or using cmake_policy(SET CMP0128 NEW) would help this. You can also experiment on the CLI with -DCMAKE_POLICY_DEFAULT_CMP0128=NEW.

Then tried building with -std=c89 and it brought a bunch of missing types (e.g. u_char) in the chacha code (on Linux, macOS. did not test Windows). Is that what you're seeing?

Precisely. Sorry for being so succint in the original PR.

There's also cases in examples and tests:

/home/tambre/dev/libssh2/example/ssh2_exec.c:42:20: error: variable has incomplete type 'struct timeval'
   42 |     struct timeval timeout;
      |                    ^
/home/tambre/dev/libssh2/example/x11.c:93:20: error: use of undeclared identifier 'fileno'
   93 |     rc = tcgetattr(fileno(stdin), &tio);
      |                    ^~~~~~
/home/tambre/dev/libssh2/tests/openssh_fixture.c:119:12: error: use of undeclared identifier 'popen'; did you mean 'fopen'?
  119 |     pipe = popen(buf, "r");
      |            ^~~~~
      |            fopen
[and many more]

@vszakats

Copy link
Copy Markdown
Member

Thanks for the details! I wonder if it'd be a better approach to update the code to not use POSIX extensions? The chacha code uses types could be replaced with standard ones.

In case of popen(), perhaps defining _DEFAULT_SOURCE in-source would be better, with a comment saying why it's necessary. Same for any specific source in examples, that may need it.

@tambry

tambry commented Sep 26, 2025

Copy link
Copy Markdown
Author

I wonder if it'd be a better approach to update the code to not use POSIX extensions?
In case of popen(), perhaps defining _DEFAULT_SOURCE in-source would be better, with a comment saying why it's necessary. Same for any specific source in examples, that may need it.

Absolutely, done!
I was trying to keep the changes minimal, but then again actually cleaning up the code to avoid needing the extensions is fairly simple too.

@tambry
tambry force-pushed the c_ext branch 2 times, most recently from a17623e to 1c99926 Compare September 26, 2025 12:17
@tambry tambry changed the title cmake: define _DEFAULT_SOURCE for POSIX extensions Make compilable in ISO C mode Sep 26, 2025
@tambry

tambry commented Sep 26, 2025

Copy link
Copy Markdown
Author

I'll open a separate PR to try bump maximum CMake range and add to a CMAKE_C_EXTENSIONS=OFF combination to CI.

Comment thread example/direct_tcpip.c Resolved
Comment thread example/x11.c Resolved
Comment thread tests/openssh_fixture.c Resolved
Comment thread src/cipher-chachapoly.c Resolved
Comment thread src/cipher-chachapoly.c Resolved
@tambry
tambry force-pushed the c_ext branch 2 times, most recently from c887f6d to 54a4940 Compare September 26, 2025 12:52
@tambry

tambry commented Oct 21, 2025

Copy link
Copy Markdown
Author

@vszakats Minor ping. Let me know if I should bring the CI commit into this PR. 🙂

@willco007

Copy link
Copy Markdown
Member

If you pull up to master it should fix CI. I don't see much else to do with this MR, is it ready to go?

@tambry

tambry commented Feb 25, 2026

Copy link
Copy Markdown
Author

Rebased. As far as I am concerned this MR is ready.

tambry added 3 commits April 7, 2026 20:29
IV setup supports a 64-bit sequence number so might as well take them in.
These are BSD/SysV extensions not available in ISO C mode.
When compiling in ISO C mode these aren't defined without a macro enabling them.
In ISO C mode it doesn't get transitively included.
@vszakats

vszakats commented Apr 7, 2026

Copy link
Copy Markdown
Member

I have an alternate take on this in #1688
(it sprang out of this PR while trying to figure out the issues by first adding a
CI test).

Did not have the resources to reconcile that with this PR. My concern with this
solution is missing CI tests and some of the changes that seem non-portable.

@tambry

tambry commented Apr 7, 2026 •

Copy link
Copy Markdown
Author

CI changes are in #1683. I'm still chasing down one macOS issue related to the policy changes (CMP0128 must be NEW for CMAKE_C_EXTENSIONS to work properly).

@vszakats

vszakats commented Apr 7, 2026 •

Copy link
Copy Markdown
Member

CI changes are in #1683. I'm still chasing down one macOS issue related to the policy changes (CMP0128 must be NEW for CMAKE_C_EXTENSIONS to work properly).

#1683 is CMake-specific, raises minimum version, wants to changes policies,
and changes behavior for everyone, not just CI. I'm not a fan. I'd rather prefer
the way it's done in #1688, which makes this tested in CI, covering all C code,
but without touching the build systems.

edit: it also should be done in the same PR that's doing the necessary fixes to
verify they are complete.

Comment thread example/direct_tcpip.c

#ifdef HAVE_SYS_SELECT_H
#include <sys/select.h>
#endif

@vszakats vszakats Apr 7, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this necessary? What is the error it's fixing?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[13/108] Building C object example/CMakeFiles/direct_tcpip.dir/direct_tcpip.c.o
FAILED: [code=1] example/CMakeFiles/direct_tcpip.dir/direct_tcpip.c.o 
/usr/bin/cc -DHAVE_CONFIG_H -I/home/raul/dev/libssh2/build/src -I/home/raul/dev/libssh2/src -I/home/raul/dev/libssh2/include -Wextra -Wall -pedantic -Wbad-function-cast -Wconversion -Winline -Wmissing-declarations -Wmissing-prototypes -Wnested-externs -Wno-long-long -Wno-multichar -Wpointer-arith -Wshadow -Wsign-compare -Wundef -Wunused -Wwrite-strings -Waddress -Wattributes -Wcast-align -Wcast-qual -Wdeclaration-after-statement -Wdiv-by-zero -Wempty-body -Wendif-labels -Wfloat-equal -Wformat-security -Wignored-qualifiers -Wmissing-field-initializers -Wmissing-noreturn -Wno-format-nonliteral -Wno-system-headers -Wold-style-definition -Wredundant-decls -Wsign-conversion -Wno-error=sign-conversion -Wstrict-prototypes -Wtype-limits -Wunreachable-code -Wunused-macros -Wunused-parameter -Wvla -Wshift-sign-overflow -Wshorten-64-to-32 -Wformat=2 -Wlanguage-extension-token -Wdouble-promotion -Wenum-conversion -Wheader-guard -Wpragmas -Wsometimes-uninitialized -Wunused-const-variable -Wcomma -Wmissing-variable-declarations -Wassign-enum -Wextra-semi-stmt -Wimplicit-fallthrough -Wxor-used-as-pow -MD -MT example/CMakeFiles/direct_tcpip.dir/direct_tcpip.c.o -MF example/CMakeFiles/direct_tcpip.dir/direct_tcpip.c.o.d -o example/CMakeFiles/direct_tcpip.dir/direct_tcpip.c.o -c /home/raul/dev/libssh2/example/direct_tcpip.c
/home/raul/dev/libssh2/example/direct_tcpip.c:67:20: error: variable has incomplete type 'struct timeval'
   67 |     struct timeval tv;
      |                    ^
/home/raul/dev/libssh2/example/direct_tcpip.c:67:12: note: forward declaration of 'struct timeval'
   67 |     struct timeval tv;
      |            ^
/home/raul/dev/libssh2/example/direct_tcpip.c:249:9: error: use of undeclared identifier 'fd_set'
  249 |         fd_set fds;
      |         ^~~~~~
/home/raul/dev/libssh2/example/direct_tcpip.c:250:9: error: use of undeclared identifier 'FD_ZERO'
  250 |         FD_ZERO(&fds);
      |         ^~~~~~~
/home/raul/dev/libssh2/example/direct_tcpip.c:250:18: error: use of undeclared identifier 'fds'
  250 |         FD_ZERO(&fds);
      |                  ^~~
/home/raul/dev/libssh2/example/direct_tcpip.c:255:9: error: use of undeclared identifier 'FD_SET'
  255 |         FD_SET(forwardsock, &fds);
      |         ^~~~~~
/home/raul/dev/libssh2/example/direct_tcpip.c:255:30: error: use of undeclared identifier 'fds'
  255 |         FD_SET(forwardsock, &fds);
      |                              ^~~
/home/raul/dev/libssh2/example/direct_tcpip.c:261:14: error: use of undeclared identifier 'select'
  261 |         rc = select((int)(forwardsock + 1), &fds, NULL, NULL, &tv);
      |              ^~~~~~
/home/raul/dev/libssh2/example/direct_tcpip.c:261:46: error: use of undeclared identifier 'fds'
  261 |         rc = select((int)(forwardsock + 1), &fds, NULL, NULL, &tv);
      |                                              ^~~
/home/raul/dev/libssh2/example/direct_tcpip.c:270:18: error: use of undeclared identifier 'FD_ISSET'
  270 |         if(rc && FD_ISSET(forwardsock, &fds)) {
      |                  ^~~~~~~~
/home/raul/dev/libssh2/example/direct_tcpip.c:270:41: error: use of undeclared identifier 'fds'
  270 |         if(rc && FD_ISSET(forwardsock, &fds)) {
      |                                         ^~~
10 errors generated.

Per POSIX those functions are in <sys/select.h>.
On most systems in GNU mode the other headers happen to pull that also in.
Doesn't seem to be the case in ISO C mode.

@vszakats vszakats Apr 7, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Could reproduce in CI with !tests and !_DEFAULT_SOURCE:
https://github.com/libssh2/libssh2/actions/runs/24107290427/job/70333563236

I'm readding them because it is the canonical header for select(), and
removing them in a683133 was not accurate.

@tambry

tambry commented Apr 7, 2026

Copy link
Copy Markdown
Author

CI changes are in #1683. I'm still chasing down one macOS issue related to the policy changes (CMP0128 must be NEW for CMAKE_C_EXTENSIONS to work properly).

#1683 is CMake-specific, raises minimum version, wants to changes policies, and changes behavior for everyone, not just CI. I'm not a fan. I'd rather prefer the way it's done in #1688, which makes this tested in CI, covering all C code, but without touching the build systems.

edit: it also should be done in the same PR that's doing the necessary fixes to verify they are complete.

Touching CMake is mostly auxiliary cleanup except for the side effect of setting CMP0128 to NEW to make CMAKE_C_EXTENSIONS work.

Closing in favour of #1688 and will provide feedback on there to help get it into shape. 🙂

@tambry tambry closed this Apr 7, 2026
vszakats added a commit that referenced this pull request Apr 8, 2026
Note: On Linux with glibc, ISO mode also requires setting
the `_DEFAULT_SOURCE` macro to enable necessary POSIX features when
building tests (for `popen()`, `pclose()` and examples (for `fileno()`,
`tcsetattr()`). On other platforms and environments it may require
something else.

- replace `strdup()` with local implementation.
- replace `getline()` with C89 `fgets()`.
- replace `u_char`, `u_int` with C89 types.
- fix type mismatches in chacha-poly1305 code.
- silence `-Wno-format-nonliteral` locally. The global method was not
  effective (like because it was overridden by `-Wformat=2`) and caused
  these warnings when building in C89 mode.
- GHA: test ISO C89 in zlib-enabled Linux jobs with all crypto backends.
- fix formatting nits along the way.
- configure, cmake: silence silly `-Wno-comma` warnings with clang in
  C89 mode.
- examples: include `select.h` again, where `select()` is used. As its
  canonical header. Seen required on Linux glibc non-GNU/ISO builds.
  Follow-up to a683133 #999

Assisted-by: Raul Tambre
Ref: #1674

Closes #1688
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants