Skip to content

tests: avoid using MAXPATHLEN, for portability - #1415

Closed
babelouest wants to merge 15 commits into
libssh2:masterfrom
babelouest:fix-maxpathlen
Closed

babelouest wants to merge 15 commits into
libssh2:masterfrom
babelouest:fix-maxpathlen

Conversation

@babelouest

@babelouest babelouest commented Jun 26, 2024 •

Copy link
Copy Markdown
Contributor

MAXPATHLEN is not present in some systems, e.g. GNU Hurd.

Fixes #1414
Closes #1415

@vszakats

vszakats commented Jun 26, 2024 •

Copy link
Copy Markdown
Member

valgrind tests would be a nice addition, but I personally have not
much experience with it. (it never got supported in my dev env, macOS)

As for freeing pointers:
I think the idea was to keep a fixed list of pointers so callers
don't have to deal with freeing after use.

What do you think of an approach of freeing filepath items
on deinit in stop_session_fixture()?

If we move to freeing right after use, there are two more cases
to update in test_agent_forward_ok.c and test_read.c. And
in this case the static filepath list along with the curpath counter
become unnecessary.

@babelouest

Copy link
Copy Markdown
Contributor Author

Thanks for your feedback!

I've updated the PR to cleanup filepath during stop_session_fixture() instead.

Concerning valgrind, I use it by executing the test program via valgrind and checking the result, example:

$ valgrind --tool=memcheck --leak-check=full --show-leak-kinds=all ./test_auth_password_ok 2>valgrind-test_auth_password_ok.log

I don't know exactly where the test programs are executed in libssh2, I guess tests/test_sshd.test is one of the starting point.

Although, the docker tests should be updated too, but I don't know how yet.

@vszakats

Copy link
Copy Markdown
Member

Thanks for you updates!

checksrc has these complaints:

./tests/session_fixture.c:258:8: warning: we prefer !variable instead of "== NULL" comparisons (EQUALSNULL)
     if(NULL != file) {
        ^
./tests/session_fixture.c:259:7: warning: not indented 4 steps (uses 2) (INDENTATION)
       if(curpath >= NUMPATHS) {
       ^
./tests/session_fixture.c:280:9: warning: not indented 4 steps (uses 2) (INDENTATION)
         free(filepath[curpath]);
         ^

Tests are launched via the TESTS list in tests/Makefile.am for autotools and for CMake via tests/CMakeLists.txt / add_test(). Some launched via .test scripts, others directly. Plugging valgrind into .test scripts could be a sensible starting point. Probably with a low FIXTURE_XFER_COUNT to maintain CI turnaround times.

@vszakats vszakats changed the title Update srcdir_path to avoid using MAXPATHLEN, which is not present in … tests: avoid using MAXPATHLEN, for portability Jun 27, 2024
@vszakats

vszakats commented Jun 27, 2024 •

Copy link
Copy Markdown
Member

Looking good.

Based on https://learn.microsoft.com/en-us/cpp/c-runtime-library/path-field-limits https://www.man7.org/linux/man-pages/man3/realpath.3.html and 10a5cbf we might be able to drop <stdlib.h> and <sys/param.h>. It seems they were included for MAXPATHLEN and _MAX_PATH.

If CI remains green, we could drop sys/param.h detection:

CMakeLists.txt:    set(HAVE_SYS_PARAM_H 1)
CMakeLists.txt:  check_include_files("sys/param.h" HAVE_SYS_PARAM_H)  # tests
configure.ac:AC_CHECK_HEADERS([errno.h fcntl.h stdio.h unistd.h sys/param.h sys/uio.h])
src/libssh2_config_cmake.h.in:#cmakedefine HAVE_SYS_PARAM_H
src/libssh2_setup.h:# define HAVE_SYS_PARAM_H
tests/session_fixture.c:#ifdef HAVE_SYS_PARAM_H
tests/session_fixture.c:#include <sys/param.h>

Would you like to do these? Or let me know if I should do it.

@babelouest

Copy link
Copy Markdown
Contributor Author

Removing <stdlib.h> and <sys/param.h> doesn't seem to work. The errors seem to be about missing declarations from stdlib, I'll give another try without <sys/param.h> only.

If successful, I'd like you to remove sys/param.h detection from the build scripts, because I'm not familiar with those and I would be afraid to silently break something else, if you don't mind.

@vszakats

Copy link
Copy Markdown
Member

Agreed! stdlib.h still needed for getenv. I'll add such comment later.

@babelouest

Copy link
Copy Markdown
Contributor Author

It seems that removing <sys/param.h> does break AppVeyor CI tests as well, do you know if this could be fixed without re-including <sys/param.h>?

@vszakats

vszakats commented Jul 3, 2024

Copy link
Copy Markdown
Member

I think the AppVeyor job failed for unrelated reasons. It sometimes does that while trying to interact with a GHA server process and failing ("Waiting for SSH connection from GitHub Actions..[...] failed.", then timing out.

If you push (or force-push) the branch it will make it re-run.

@babelouest

Copy link
Copy Markdown
Contributor Author

If you push (or force-push) the branch it will make it re-run.

After a small change/push, still no luck with AppVeyor tests.

Comment thread tests/session_fixture.c Resolved
vszakats added 3 commits July 3, 2024 22:24
```
../../tests/session_fixture.c:266:49: warning: conversion to 'size_t' {aka 'long long unsigned int'} from 'int' may change the sign of the result [-Wsign-conversion]
  266 |                 snprintf(filepath[curpath], len + 1, "%s/%s", p, file);
      |                                             ~~~~^~~
../../tests/session_fixture.c:276:49: warning: conversion to 'size_t' {aka 'long long unsigned int'} from 'int' may change the sign of the result [-Wsign-conversion]
  276 |                 snprintf(filepath[curpath], len + 1, "%s", file);
      |                                             ~~~~^~~
../../tests/session_fixture.c:276:57: error: 'snprintf' output may be truncated before the last format character [-Werror=format-truncation=]
  276 |                 snprintf(filepath[curpath], len + 1, "%s", file);
      |                                                         ^
../../tests/session_fixture.c:276:17: note: 'snprintf' output 1 or more bytes (assuming 2) into a destination of size 1
  276 |                 snprintf(filepath[curpath], len + 1, "%s", file);
      |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../../tests/session_fixture.c:266:57: error: '/' directive output may be truncated writing 1 byte into a region of size between 0 and 1 [-Werror=format-truncation=]
  266 |                 snprintf(filepath[curpath], len + 1, "%s/%s", p, file);
      |                                                         ^
../../tests/session_fixture.c:266:54: note: assuming directive output of 1 byte
  266 |                 snprintf(filepath[curpath], len + 1, "%s/%s", p, file);
      |                                                      ^~~~~~~
../../tests/session_fixture.c:266:17: note: 'snprintf' output 2 or more bytes (assuming 4) into a destination of size 1
  266 |                 snprintf(filepath[curpath], len + 1, "%s/%s", p, file);
      |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
```
@vszakats vszakats closed this in 12427f4 Jul 3, 2024
@vszakats

vszakats commented Jul 3, 2024

Copy link
Copy Markdown
Member

Thank you Nicolas, merged!

@babelouest

Copy link
Copy Markdown
Contributor Author

Thanks for your help!

vszakats added a commit that referenced this pull request Jul 4, 2024
This header is no longer used.

Follow-up to 12427f4 #1415
Closes #1418
@babelouest
babelouest deleted the fix-maxpathlen branch July 4, 2024 11:54
agreppin pushed a commit to agreppin/libssh2 that referenced this pull request Jul 14, 2024
`MAXPATHLEN` is not present in some systems, e.g. GNU Hurd.

Co-authored-by: Viktor Szakats
Ref: 54bef4c libssh2#198
Fixes libssh2#1414
Closes libssh2#1415
agreppin pushed a commit to agreppin/libssh2 that referenced this pull request Jul 14, 2024
This header is no longer used.

Follow-up to 12427f4 libssh2#1415
Closes libssh2#1418
vszakats added a commit that referenced this pull request Jun 20, 2026
Causing memleak, OOB read and freeing an invalid pointer.

Also:
- fix potentially freeing the whole list when receiving `NULL` input
  path. (reported by clang-tidy, could not happen with existing tests.)
- make use of `SSH2_ARRAYSIZE()`, bump index type to `size_t`.
- avoid calling `getenv()` when unnecessary.
- clear freed slot pointers.
- avoid crash on OOM by checking `calloc()` results.
- return `NULL` if no more empty slots.
- drop single-instruction `{}` blocks.

Root issue spotted by GitHub Code Quality

Note: this is still problematic after this many changes since the many
failure mores all return `NULL` which then crash/break the actual test,
in obscure ways.

Follow-up to 12427f4 #1415

Closes #2089
vszakats added a commit that referenced this pull request Jun 21, 2026
- if `srcdir` is unset or empty return the received filename as-is.
- replace `assert()` with an always-enabled check, error messages and
  `abort()` on fail. To make it work in debug build and not return
  `NULL` to the caller.
- determine buffer length manuall, to not rely on `snprintf()`.
- limit input sizes.
- switch from `calloc()` to `malloc()` to avoid GCC false positive
  `-Wformat-truncation`.
  Ref: https://github.com/libssh2/libssh2/actions/runs/27912044754/job/82590731447#step:24:45
- restore lost `const` to return value.
- constify `getenv()` result pointer.
- rename `p` to `srcdir`.
- flatten.

If this still falls short in some ways, it's still an option to revert
to static buffers.

Follow-up to aae3776 #2089
Follow-up to 12427f4 #1415

Closes #2102
vszakats added a commit to vszakats/libssh2 that referenced this pull request Jun 21, 2026
This time without using `MAXPATHLEN`, which is missing from GNU Hurd.
Nor using other constants, and 256 bytes on all platforms.

Dynamic allocations almost looked good after good summer day of hair
pulling, until I ran gcc-16, gcc-15, gcc-14 locally, in unity mode,
where all 3 fell over with various bogus `-Wformat-truncate` false
positives. This isn't worth all this effort.

Ref: https://darnassus.sceen.net/~hurd-web/faq/foo_max/
Follow-up to 3f6481b1a870636d9bb19988ed61fff1f6877547 libssh2#2102
Follow-up to aae3776 libssh2#2089
Follow-up to 12427f4 libssh2#1415

Fixing:
gcc-14, gcc-15:
```
tests/session_fixture.c: In function 'srcdir_path':
tests/session_fixture.c:276:46: error: '%s' directive output may be truncated writing likely 1 or more bytes into a region of size between 0 and 1 [-Werror=format-truncation=]
  276 |     ssh2_snprintf(filepath[curpath], buflen, "%s/%s", srcdir, file);
      |                                              ^~~~~~~
tests/session_fixture.c:276:5: note: in expansion of macro 'ssh2_snprintf'
  276 |     ssh2_snprintf(filepath[curpath], buflen, "%s/%s", srcdir, file);
      |     ^~~~~~~~~~~~~
In file included from bld/tests/CMakeFiles/runner.dir/Unity/unity_0_c.c:7:
tests/session_fixture.c:276:50: note: format string is defined here
  276 |     ssh2_snprintf(filepath[curpath], buflen, "%s/%s", srcdir, file);
      |                                                  ^~
tests/session_fixture.c:276:46: note: assuming directive output of 1 byte
  276 |     ssh2_snprintf(filepath[curpath], buflen, "%s/%s", srcdir, file);
      |                                              ^~~~~~~
tests/session_fixture.c:276:5: note: in expansion of macro 'ssh2_snprintf'
  276 |     ssh2_snprintf(filepath[curpath], buflen, "%s/%s", srcdir, file);
      |     ^~~~~~~~~~~~~
src/libssh2_priv.h:151:23: note: '__builtin_snprintf' output 2 or more bytes (assuming 4) into a destination of size 2
  151 | #define ssh2_snprintf snprintf
      |                       ^~~~~~~~
tests/session_fixture.c:276:5: note: in expansion of macro 'ssh2_snprintf'
  276 |     ssh2_snprintf(filepath[curpath], buflen, "%s/%s", srcdir, file);
      |     ^~~~~~~~~~~~~
```

gcc-16:
```
tests/session_fixture.c: In function 'srcdir_path':
tests/session_fixture.c:276:46: error: '%s' directive output may be truncated writing up to 8192 bytes into a region of size 2 [-Werror=format-truncation=]
  276 |     ssh2_snprintf(filepath[curpath], buflen, "%s/%s", srcdir, file);
      |                                              ^~~~~~~
tests/session_fixture.c:276:5: note: in expansion of macro 'ssh2_snprintf'
  276 |     ssh2_snprintf(filepath[curpath], buflen, "%s/%s", srcdir, file);
      |     ^~~~~~~~~~~~~
In file included from bld/tests/CMakeFiles/runner.dir/Unity/unity_0_c.c:7:
tests/session_fixture.c:276:47: note: format string is defined here
  276 |     ssh2_snprintf(filepath[curpath], buflen, "%s/%s", srcdir, file);
      |                                               ^~
src/libssh2_priv.h:151:23: note: '__builtin_snprintf' output between 2 and 16386 bytes into a destination of size 2
  151 | #define ssh2_snprintf snprintf
      |                       ^~~~~~~~
tests/session_fixture.c:276:5: note: in expansion of macro 'ssh2_snprintf'
  276 |     ssh2_snprintf(filepath[curpath], buflen, "%s/%s", srcdir, file);
      |     ^~~~~~~~~~~~~
```
vszakats added a commit that referenced this pull request Jun 21, 2026
This time without using `MAXPATHLEN`, which is missing from GNU Hurd.
Nor using other constants, but fixed 256 bytes on all platforms.

Dynamic allocations almost looked good after good summer day of hair
pulling, until I ran gcc-16, gcc-15, gcc-14 locally, in unity mode,
where all 3 fell over with `-Wformat-truncate` false positives. This
isn't worth all this amount of hassle.

(It also fails in CI. What confused me is the last push only triggered
AppVeyor CI and it went green. What a mess.)

Ref: https://darnassus.sceen.net/~hurd-web/faq/foo_max/
Follow-up to 4cecccd #2102
Follow-up to aae3776 #2089
Follow-up to 12427f4 #1415

Fixing:
gcc-13 (in CI):
https://github.com/libssh2/libssh2/actions/runs/27913275531/job/82594004458
```
tests/session_fixture.c: In function ‘srcdir_path’:
tests/session_fixture.c:276:50: error: ‘%s’ directive output may be truncated writing likely 1 or more bytes into a region of size between 0 and 1 [-Werror=format-truncation=]
  276 |     ssh2_snprintf(filepath[curpath], buflen, "%s/%s", srcdir, file);
      |                                                  ^~
tests/session_fixture.c:276:46: note: assuming directive output of 1 byte
  276 |     ssh2_snprintf(filepath[curpath], buflen, "%s/%s", srcdir, file);
      |                                              ^~~~~~~
In file included from tests/session_fixture.h:45,
                 from tests/runner.h:44,
                 from tests/runner.c:40,
                 from bld/tests/CMakeFiles/runner.dir/Unity/unity_0_c.c:4:
src/libssh2_priv.h:151:23: note: ‘snprintf’ output 2 or more bytes (assuming 4) into a destination of size 2
  151 | #define ssh2_snprintf snprintf
tests/session_fixture.c:276:5: note: in expansion of macro ‘ssh2_snprintf’
  276 |     ssh2_snprintf(filepath[curpath], buflen, "%s/%s", srcdir, file);
      |     ^~~~~~~~~~~~~
```

gcc-14, gcc-15:
```
tests/session_fixture.c: In function 'srcdir_path':
tests/session_fixture.c:276:46: error: '%s' directive output may be truncated writing likely 1 or more bytes into a region of size between 0 and 1 [-Werror=format-truncation=]
  276 |     ssh2_snprintf(filepath[curpath], buflen, "%s/%s", srcdir, file);
      |                                              ^~~~~~~
tests/session_fixture.c:276:5: note: in expansion of macro 'ssh2_snprintf'
  276 |     ssh2_snprintf(filepath[curpath], buflen, "%s/%s", srcdir, file);
      |     ^~~~~~~~~~~~~
In file included from bld/tests/CMakeFiles/runner.dir/Unity/unity_0_c.c:7:
tests/session_fixture.c:276:50: note: format string is defined here
  276 |     ssh2_snprintf(filepath[curpath], buflen, "%s/%s", srcdir, file);
      |                                                  ^~
tests/session_fixture.c:276:46: note: assuming directive output of 1 byte
  276 |     ssh2_snprintf(filepath[curpath], buflen, "%s/%s", srcdir, file);
      |                                              ^~~~~~~
tests/session_fixture.c:276:5: note: in expansion of macro 'ssh2_snprintf'
  276 |     ssh2_snprintf(filepath[curpath], buflen, "%s/%s", srcdir, file);
      |     ^~~~~~~~~~~~~
src/libssh2_priv.h:151:23: note: '__builtin_snprintf' output 2 or more bytes (assuming 4) into a destination of size 2
  151 | #define ssh2_snprintf snprintf
      |                       ^~~~~~~~
tests/session_fixture.c:276:5: note: in expansion of macro 'ssh2_snprintf'
  276 |     ssh2_snprintf(filepath[curpath], buflen, "%s/%s", srcdir, file);
      |     ^~~~~~~~~~~~~
```

gcc-16:
```
tests/session_fixture.c: In function 'srcdir_path':
tests/session_fixture.c:276:46: error: '%s' directive output may be truncated writing up to 8192 bytes into a region of size 2 [-Werror=format-truncation=]
  276 |     ssh2_snprintf(filepath[curpath], buflen, "%s/%s", srcdir, file);
      |                                              ^~~~~~~
tests/session_fixture.c:276:5: note: in expansion of macro 'ssh2_snprintf'
  276 |     ssh2_snprintf(filepath[curpath], buflen, "%s/%s", srcdir, file);
      |     ^~~~~~~~~~~~~
In file included from bld/tests/CMakeFiles/runner.dir/Unity/unity_0_c.c:7:
tests/session_fixture.c:276:47: note: format string is defined here
  276 |     ssh2_snprintf(filepath[curpath], buflen, "%s/%s", srcdir, file);
      |                                               ^~
src/libssh2_priv.h:151:23: note: '__builtin_snprintf' output between 2 and 16386 bytes into a destination of size 2
  151 | #define ssh2_snprintf snprintf
      |                       ^~~~~~~~
tests/session_fixture.c:276:5: note: in expansion of macro 'ssh2_snprintf'
  276 |     ssh2_snprintf(filepath[curpath], buflen, "%s/%s", srcdir, file);
      |     ^~~~~~~~~~~~~
```

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MAXPATHLEN not defined on hurd systems

2 participants