Repository navigation
Conversation
7bf6241 to
cc2031c
Compare
| [ -d "$HOME"/usr ] && options+=" -DCMAKE_PREFIX_PATH=$HOME/usr" | ||
| [ "${MATRIX_ARCH}" = 'i386' ] && options+=" -DCMAKE_SYSTEM_NAME=Linux -DCMAKE_SYSTEM_VERSION=1 -DCMAKE_SYSTEM_PROCESSOR=${MATRIX_ARCH} -DCMAKE_C_FLAGS=-m32" | ||
| [ "${MATRIX_COMPILER}" = 'clang-tidy' ] && options+=' -DLIBSSH2_CLANG_TIDY=ON' | ||
| [[ "${MATRIX_CRYPTO}" =~ ^(OpenSSL|Libgcrypt|mbedTLS|wolfSSL)$ && "${MATRIX_COMPILER}" != 'clang-tidy' && "${MATRIX_ZLIB}" = 'ON' ]] && export CFLAGS='-std=c99 -D_DEFAULT_SOURCE' |
There was a problem hiding this comment.
Passing -D_DEFAULT_SOURCE and outside the build systems kinda ignores the other half of the issues.
Building this branch fails for me with:
/usr/bin/cc -DHAVE_CONFIG_H -DLIBSSH2_OPENSSL -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 tests/CMakeFiles/runner.dir/openssh_fixture.c.o -MF tests/CMakeFiles/runner.dir/openssh_fixture.c.o.d -o tests/CMakeFiles/runner.dir/openssh_fixture.c.o -c /home/raul/dev/libssh2/tests/openssh_fixture.c
/home/raul/dev/libssh2/tests/openssh_fixture.c:119:12: error: use of undeclared identifier 'popen'; did you mean 'fopen'?
119 | pipe = popen(buf, "r");
| ^~~~~
| fopen
/usr/include/stdio.h:267:14: note: 'fopen' declared here
267 | extern FILE *fopen (const char *__restrict __filename,
| ^
/home/raul/dev/libssh2/tests/openssh_fixture.c:131:11: error: use of undeclared identifier 'pclose'
131 | ret = pclose(pipe);
| ^~~~~~
There were a lot of changes related to this in #1674 because of this.
I would expect a ISO C defaulting compiler to be able to build libssh2 out of the box instead of requiring the user to specify extra flags. Because then they might as well pass -std=gnu99.
It does mean you still require the system to have those GNU extensions but that's understandable.
Not to say that this isn't worth merging as-is.
Everything but the tests now compiles in ISO C mode and that's a good step forward.
There was a problem hiding this comment.
I'm confused. How do I reproduce this? Tests and examples are building
cleanly in CI, e.g.:
https://github.com/libssh2/libssh2/actions/runs/24100935125/job/70311767941?pr=1688
Fact is that some libssh2 parts do not compile in ISO mode. That
is something we cannot change easily.
If you need a libssh2 library built purely with -std=c89, why not just
disable building tests and examples?
If the goal is to build libssh2's different parts with different C standards,
it seems to be a different problem. I find this goal odd, because mixing
components built with different C standards is not necessarily safe AFAIK.
I'm not sure automating this and hiding it under the hood helps most users
(or maintainers).
There was a problem hiding this comment.
How do I reproduce this?
Building with cmake -DCMAKE_C_FLAGS=-std=c99 is sufficient with both GCC 16 and Clang 23.
Fact is that some libssh2 parts do not compile in ISO mode. That is something we cannot change easily.
There's two things you shouldn't conflate: POSIX extensions and GNU language extensions.
In GNU language extensions mode /usr/include/features.h defines _DEFAULT_SOURCE to enable essentially all POSIX extensions (both BSD, GNU, X/Open, etc.).
libssh2 isn't reliant on any GNU language extensions – #1674 compiles for me.
But it does rely on some POSIX extensions – popen(), pclose(), fileno(), tcsetattr().
That's OK. I simply would expect libssh2 to define _DEFAULT_SOURCE (or one of the more specific macros) at the build system level or at the file level (as #1674 did) to enable them instead of relying on GNU language extension mode to enable them for you.
I would consider this similar to how GNU mode happens to pull in <sys/select.h> transitively in many places. Better to do it yourself explicitly instead of relying on the environment if it's what you intended.
If you need a libssh2 library built purely with -std=c89, why not just disable building tests and examples?
I think that's a fine option.
Whether libssh2 should also support pure ISO C mode is up to you.
In #1674 the relevant defines total 16 lines. Would probably be just a few lines if defined globally at the build system level (though I prefer explicitly enabling extensions where intended).
If the goal is to build libssh2's different parts with different C standards
Not my goal. Though AFAIK should be safe for GNU vs non-GNU mode. There's plenty of software that enables GNU mode just for a few files where required to avoid accidentally introducing extensions in new code.
There was a problem hiding this comment.
Do you know the difference between _DEFAULT_SOURCE and _POSIX_SOURCE? Also the equivalent of such macro in other platforms/environments? Seeing more clearly now, I agree that
setting it/them for tests and examples would be helpful. It can be done without conditions if limited to these two components.
FWIW I find the name "_DEFAULT_SOURCE" very much unhelpful to understand what it is for. Also glibc-specific.
There was a problem hiding this comment.
man 7 feature_test_macros is the authoritative documentation.
_DEFAULT_SOURCE is kinda the catch-all "give me the default set of extensions", which more or less means everything.
_POSIX_SOURCE is considered obsolete and you should use _POSIX_C_SOURCE, which has documented values.
Taking for example popen()/pclose():
- Documented by glibc as
_POSIX_C_SOURCE >= 2 || /* glibc <= 2.19: */ _BSD_SOURCE || _SVID_SOURCE. _POSIX_C_SOURCE=2corresponds to POSIX.2-1992.- So keep it minimal you'd go with
#define _POSIX_C_SOURCE 2.
But all of the above is glibc-specific. There's no unified documentation that I know of how various C standard library implementations guard these.
It seems on IBM z/OS for tcsetattr() you'd need to define _POSIX_SOURCE despite glibc considering it deprecated. On glibc you'll need to define the general _DEFAULT_SOURCE (but on my installation it's actually unguarded!).
In general I must admit this is a bit of a rabbit hole if you want to support more obscure non-Linux systems (IBM, Solaris, SPARC, QNX, AIX, Cray PE). AFAICT on Linux other implementations beside glibc generally provide these unconditionally or support the same macros. So just stick a #if defined(__linux__) and define what the glibc's manpage says, I'd say. If someone wants to make this work for Solaris I'd expect them to provide a CI runner and patches.
There was a problem hiding this comment.
This continues to be awkward for examples. To avoid setting an unused macro
(and the warning with it), this would need to be guarded for glibc-only (e.g. not
for MUSL), making __linux__ unfit for the purpose.
(edit: could be mitigated by moving shared stuff to an examples.h, but that
makes examples non-self-contained, and I tried avoiding this so far, also in curl,
because it makes examples less convenient to consume.)
I'm back to the initial conclusion that libssh2 does require GNU extensions, and it's
not energy well-spent to try making it build without them out of the box. There may
be a middle ground possibly, but even that's boosting complexity, and that middle
ground is possibly hit just requiring to set the necessary macros manually for those
who wish to compile in ISO mode.
I wonder what are the real-life use-cases when any more complexity makes this
effort a net positive?
There was a problem hiding this comment.
this would need to be guarded for glibc-only (e.g. not for MUSL), making linux unfit for this purpose.
Probably #include <features.h> + #ifdef __GLIBC__ would be the way to go if -Werror=unused-macros is the constraint.
I'm back to the initial conclusion that libssh2 does require GNU extensions, and it's not energy well-spent to try making it build without them out of the box.
I think that's an understandable standpoint to have.
CMake for example put in a lot of effort (and it's a ugly). But then again they support SPARC, Solaris, Cray PE, IBM systems, ancient glibc (pre-2011) and more, which is quite varied and which very few projects bother with these days.
I wonder what are the real-life use-cases when any more complexity makes this
effort a net positive?
How I even ended up here initially was that as part of building a large Rust project libssh2 was being built but failed due to not compiling in ISO C mode, which is what my system compiler defaults to. In that case I didn't have a good way to fix the build besides changing my global default or setting a different CC. I think this solves that even if the examples and tests are left as-is.
There was a problem hiding this comment.
Ack, and thanks for the details that lead here, also the CMake links.
Going the <features.h> way looks like a deep rabbit hole. I'm already
exhaused.
The global option came before `-Wformat=2` and presumable this
was the reason that it had no effect when building in C89 mode.
In file included from _cm-openssl-c89/tests/CMakeFiles/runner.dir/Unity/unity_0_c.c:10:
tests/openssh_fixture.c:100:55: warning: format string is not a string literal [-Wformat-nonliteral]
100 | ret = vsnprintf(command_buf, sizeof(command_buf), command, args);
| ^~~~~~~
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/secure/_stdio.h:124:69: note: expanded from macro 'vsnprintf'
124 | #define vsnprintf(str, len, ...) __vsnprintf_chk_func (str, len, 0, __VA_ARGS__)
| ^~~~~~~~~~~
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/secure/_stdio.h:81:65: note: expanded from macro '__vsnprintf_chk_func'
81 | __builtin___vsnprintf_chk (str, len, flag, __darwin_obsz(str), format, ap)
| ^~~~~~
1 warning generated.
[6/114] Building C object src/CMakeFiles/libssh2_static.dir/Unity/unity_0_c.c.o
In file included from _cm-openssl-c89/src/CMakeFiles/libssh2_static.dir/Unity/unity_0_c.c:49:
src/misc.c:613:50: warning: format string is not a string literal [-Wformat-nonliteral]
613 | len = vsnprintf(buffer + msglen, buflen, format, vargs);
| ^~~~~~
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/secure/_stdio.h:124:69: note: expanded from macro 'vsnprintf'
124 | #define vsnprintf(str, len, ...) __vsnprintf_chk_func (str, len, 0, __VA_ARGS__)
| ^~~~~~~~~~~
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/secure/_stdio.h:81:65: note: expanded from macro '__vsnprintf_chk_func'
81 | __builtin___vsnprintf_chk (str, len, flag, __darwin_obsz(str), format, ap)
| ^~~~~~
1 warning generated.
[7/114] Building C object src/CMakeFiles/libssh2_shared.dir/Unity/unity_0_c.c.o
In file included from _cm-openssl-c89/src/CMakeFiles/libssh2_shared.dir/Unity/unity_0_c.c:49:
src/misc.c:613:50: warning: format string is not a string literal [-Wformat-nonliteral]
613 | len = vsnprintf(buffer + msglen, buflen, format, vargs);
| ^~~~~~
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/secure/_stdio.h:124:69: note: expanded from macro 'vsnprintf'
124 | #define vsnprintf(str, len, ...) __vsnprintf_chk_func (str, len, 0, __VA_ARGS__)
| ^~~~~~~~~~~
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/secure/_stdio.h:81:65: note: expanded from macro '__vsnprintf_chk_func'
81 | __builtin___vsnprintf_chk (str, len, flag, __darwin_obsz(str), format, ap)
| ^~~~~~
There was a problem hiding this comment.
Pull request overview
This PR aims to make the project build cleanly in ISO C89/C90 mode (including CI coverage), by removing/working around non-C89 APIs and types, tightening type correctness in chacha20-poly1305, and adjusting warning handling for strict builds.
Changes:
- Replace non-C89 calls/types (e.g.,
strdup,getline,u_char/u_int) with C89-compatible alternatives in touched areas. - Fix/align chacha20-poly1305 and poly1305 type signatures to remove mismatches and improve portability.
- Update build tooling (CMake/autotools/GitHub Actions) to test C89 mode in CI and locally suppress
-Wformat-nonliteral, plus silence clang-Wcommain C89.
Reviewed changes
Copilot reviewed 27 out of 27 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/session_fixture.c | Switches keyboard-interactive response allocation from strdup() to test-local dup helper. |
| tests/openssh_fixture.h | Exposes a new test-local string duplication helper declaration. |
| tests/openssh_fixture.c | Adds test-local strdup implementation, replaces getline() with fgets(), and locally suppresses -Wformat-nonliteral. |
| src/poly1305.h | Replaces u_char usage with standard unsigned char in the public prototype. |
| src/poly1305.c | Matches the updated poly1305_auth signature and adjusts formatting. |
| src/misc.c | Locally suppresses -Wformat-nonliteral around vsnprintf in debug logging. |
| src/libssh2_setup.h | Removes MSVC <1900 strdup mapping and updates comments accordingly. |
| src/crypt.c | Removes u_int casts and aligns calls with updated chachapoly signatures; minor formatting cleanup. |
| src/cipher-chachapoly.h | Updates internal API types (u_char/u_int → unsigned char/size_t/libssh2_uint64_t). |
| src/cipher-chachapoly.c | Implements the updated chachapoly API types and minor style tweaks. |
| src/chacha.h | Introduces C89-friendly typedefs and updates prototypes to avoid non-C89 types. |
| src/chacha.c | Aligns implementation with header (struct-pointer types, size_t lengths). |
| example/x11.c | Re-adds canonical sys/select.h include guard where select() is used. |
| example/tcpip-forward.c | Re-adds canonical sys/select.h include guard where select() is used. |
| example/ssh2_exec.c | Re-adds canonical sys/select.h include guard where select() is used. |
| example/ssh2_echo.c | Re-adds canonical sys/select.h include guard where select() is used. |
| example/ssh2_agent_forwarding.c | Re-adds canonical sys/select.h include guard where select() is used. |
| example/sftp_write_sliding.c | Re-adds canonical sys/select.h include guard where select() is used. |
| example/sftp_write_nonblock.c | Re-adds canonical sys/select.h include guard where select() is used. |
| example/sftp_RW_nonblock.c | Re-adds canonical sys/select.h include guard where select() is used. |
| example/sftp_nonblock.c | Re-adds canonical sys/select.h include guard where select() is used. |
| example/scp_write_nonblock.c | Re-adds canonical sys/select.h include guard where select() is used. |
| example/scp_nonblock.c | Re-adds canonical sys/select.h include guard where select() is used. |
| example/direct_tcpip.c | Re-adds canonical sys/select.h include guard where select() is used. |
| cmake/PickyWarnings.cmake | Removes global -Wno-format-nonliteral; adds clang C89 -Wno-comma suppression. |
| acinclude.m4 | Removes global -Wno-format-nonliteral; adds clang C89 -Wno-comma suppression logic. |
| .github/workflows/ci.yml | Adds a Linux C89 test path (CMake C90 + _DEFAULT_SOURCE) for certain zlib-enabled jobs; refactors i386 flag handling. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| void close_socket_to_openssh_server(libssh2_socket_t sock); | ||
| int openssh_fixture_have_docker(void); | ||
|
|
||
| char *libssh2_strdup(const char *str); |
There was a problem hiding this comment.
You've missed a few more usages in:
example/sftp.cexample/sftpdir.cexample/ssh2.c
There was a problem hiding this comment.
Examples cannot use code/headers from src or tests.
There was a problem hiding this comment.
Correction: in libssh2, examples do use one base header from src: libssh2_setup.h. I forgot about it. It won't help with libssh2_strdup(), but may help with _DEFAULT_SOURCE.
| [ -d "$HOME"/usr ] && options+=" -DCMAKE_PREFIX_PATH=$HOME/usr" | ||
| [ "${MATRIX_ARCH}" = 'i386' ] && options+=" -DCMAKE_SYSTEM_NAME=Linux -DCMAKE_SYSTEM_VERSION=1 -DCMAKE_SYSTEM_PROCESSOR=${MATRIX_ARCH} -DCMAKE_C_FLAGS=-m32" | ||
| [ "${MATRIX_COMPILER}" = 'clang-tidy' ] && options+=' -DLIBSSH2_CLANG_TIDY=ON' | ||
| [[ "${MATRIX_CRYPTO}" =~ ^(OpenSSL|Libgcrypt|mbedTLS|wolfSSL)$ && "${MATRIX_COMPILER}" != 'clang-tidy' && "${MATRIX_ZLIB}" = 'ON' ]] && export CFLAGS='-std=c99 -D_DEFAULT_SOURCE' |
There was a problem hiding this comment.
How do I reproduce this?
Building with cmake -DCMAKE_C_FLAGS=-std=c99 is sufficient with both GCC 16 and Clang 23.
Fact is that some libssh2 parts do not compile in ISO mode. That is something we cannot change easily.
There's two things you shouldn't conflate: POSIX extensions and GNU language extensions.
In GNU language extensions mode /usr/include/features.h defines _DEFAULT_SOURCE to enable essentially all POSIX extensions (both BSD, GNU, X/Open, etc.).
libssh2 isn't reliant on any GNU language extensions – #1674 compiles for me.
But it does rely on some POSIX extensions – popen(), pclose(), fileno(), tcsetattr().
That's OK. I simply would expect libssh2 to define _DEFAULT_SOURCE (or one of the more specific macros) at the build system level or at the file level (as #1674 did) to enable them instead of relying on GNU language extension mode to enable them for you.
I would consider this similar to how GNU mode happens to pull in <sys/select.h> transitively in many places. Better to do it yourself explicitly instead of relying on the environment if it's what you intended.
If you need a libssh2 library built purely with -std=c89, why not just disable building tests and examples?
I think that's a fine option.
Whether libssh2 should also support pure ISO C mode is up to you.
In #1674 the relevant defines total 16 lines. Would probably be just a few lines if defined globally at the build system level (though I prefer explicitly enabling extensions where intended).
If the goal is to build libssh2's different parts with different C standards
Not my goal. Though AFAIK should be safe for GNU vs non-GNU mode. There's plenty of software that enables GNU mode just for a few files where required to avoid accidentally introducing extensions in new code.
|
Builds cleanly for me now. 👍 |
This reverts commit 98cb19d. Seems sloppy to define this. Could be filtered for triplets, which adds a lot of complexity to detect Linux (which is still kind of sloppy, because we'd need to detect glibc.) The there are the non-Linux/non-glibc targets with their own macros.
Maybe without picky warnings, but it failed in CI :( I'm giving up on this, for reasons written in comments above. Ultimately the issue is that setting this macro is specific to target. Ergo, if someone must build in ISO mode, and must also build examples |
|
off topic, sadly, after recent updates, or perhaps changes in CI env, Linux tests became flaky: I haven't seen this in the previous years. |
|
Fair enough on giving up. |
|
Thanks @tambry for your help and patience! I merged this. |
Just in case. In practice this is supposed to be as short string. Ref: #1688 (comment) Follow-up to e8e6bc2 #1688 Cherry-picked from #1947
It wasn't needed anymore for `write()` in contratry to the comment, because mapping it earlier to `_write()` in examples. But, it was needed again for `strdup()` due to a regression. Fix it by mapping it again to `_strdup()` in examples. Also: document `vsnprintf()` used in tests. Follow-up to ca43b30 #2042 Follow-up to e8e6bc2 #1688 Closes #2332
Note: On Linux with glibc, ISO mode also requires setting
the
_DEFAULT_SOURCEmacro to enable necessary POSIX features whenbuilding tests (for
popen(),pclose()and examples (forfileno(),tcsetattr()). On other platforms and environments it may requiresomething else.
strdup()with local implementation.getline()with C89fgets().u_char,u_intwith C89 types.-Wno-format-nonliterallocally. The global method was noteffective (like because it was overridden by
-Wformat=2) and causedthese warnings when building in C89 mode.
-Wno-commawarnings with clang inC89 mode.
select.hagain, whereselect()is used. As itscanonical header. Seen required on Linux glibc non-GNU/ISO builds.
Follow-up to a683133 tidy-up: C header use #999
Assisted-by: Raul Tambre
Ref: #1674
Caveats:
select()is used in examples unconditionally. It breaks building examplesif
HAVE_SELECTis false. Also, examples wouldn't work without it anyway.Does it make sense to keep a feature check?