Skip to content

tests: fix wrong index in srcdir_path() cleanup - #2089

Closed
vszakats wants to merge 10 commits into
libssh2:masterfrom
vszakats:curpath
Closed

vszakats wants to merge 10 commits into
libssh2:masterfrom
vszakats:curpath

Conversation

@vszakats

@vszakats vszakats commented Jun 20, 2026 •

Copy link
Copy Markdown
Member

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


https://github.com/libssh2/libssh2/pull/2089/files?w=1

Causing memleak, OOB read and freeing an invalid pointer.

Spotted by GitHub Code Quality

Follow-up to 12427f4 libssh2#1415
@vszakats vszakats added the tests label Jun 20, 2026
vszakats added 3 commits June 20, 2026 21:33
…() calls

```
/home/runner/work/libssh2/libssh2/tests/session_fixture.c:521:14: error: Use of memory after it is released [clang-analyzer-unix.Malloc,-warnings-as-errors]
  521 |         rc = libssh2_userauth_publickey_fromfile_ex(session, username,
      |              ^
  522 |                                                 (unsigned int)strlen(username),
  523 |                                                     srcdir_path(fn_pub),
      |                                                     ~~~~~~~~~~~~~~~~~~~
/home/runner/work/libssh2/libssh2/tests/session_fixture.c:474:8: note: Assuming the condition is false
  474 |     if(!openssh_fixture_have_docker() && !strcmp(username, "libssh2")) {
      |        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/runner/work/libssh2/libssh2/tests/session_fixture.c:474:39: note: Left side of '&&' is false
  474 |     if(!openssh_fixture_have_docker() && !strcmp(username, "libssh2")) {
      |                                       ^
/home/runner/work/libssh2/libssh2/tests/session_fixture.c:485:8: note: Assuming 'username' is non-null
  485 |     if(!username) {
      |        ^~~~~~~~~
/home/runner/work/libssh2/libssh2/tests/session_fixture.c:485:5: note: Taking false branch
  485 |     if(!username) {
      |     ^
/home/runner/work/libssh2/libssh2/tests/session_fixture.c:492:8: note: Assuming 'userauth_list' is non-null
  492 |     if(!userauth_list) {
      |        ^~~~~~~~~~~~~~
/home/runner/work/libssh2/libssh2/tests/session_fixture.c:492:5: note: Taking false branch
  492 |     if(!userauth_list) {
      |     ^
/home/runner/work/libssh2/libssh2/tests/session_fixture.c:497:8: note: Assuming the condition is false
  497 |     if(!strstr(userauth_list, "publickey")) {
      |        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/runner/work/libssh2/libssh2/tests/session_fixture.c:497:5: note: Taking false branch
  497 |     if(!strstr(userauth_list, "publickey")) {
      |     ^
/home/runner/work/libssh2/libssh2/tests/session_fixture.c:503:8: note: Assuming the condition is false
  503 |     if((flags & TEST_AUTH_FROMMEM) != 0) {
      |        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/runner/work/libssh2/libssh2/tests/session_fixture.c:503:5: note: Taking false branch
  503 |     if((flags & TEST_AUTH_FROMMEM) != 0) {
      |     ^
/home/runner/work/libssh2/libssh2/tests/session_fixture.c:523:53: note: Calling 'srcdir_path'
  523 |                                                     srcdir_path(fn_pub),
      |                                                     ^~~~~~~~~~~~~~~~~~~
/home/runner/work/libssh2/libssh2/tests/session_fixture.c:254:8: note: Assuming 'file' is non-null
  254 |     if(file) {
      |        ^~~~
/home/runner/work/libssh2/libssh2/tests/session_fixture.c:254:5: note: Taking true branch
  254 |     if(file) {
      |     ^
/home/runner/work/libssh2/libssh2/tests/session_fixture.c:256:12: note: 'curpath' is < NUMPATHS
  256 |         if(curpath >= NUMPATHS) {
      |            ^~~~~~~
/home/runner/work/libssh2/libssh2/tests/session_fixture.c:256:9: note: Taking false branch
  256 |         if(curpath >= NUMPATHS) {
      |         ^
/home/runner/work/libssh2/libssh2/tests/session_fixture.c:259:16: note: 'curpath' is < 32
  259 |         assert(curpath < NUMPATHS);
      |                ^
/home/runner/work/libssh2/libssh2/tests/session_fixture.c:259:9: note: Taking true branch
  259 |         assert(curpath < NUMPATHS);
      |         ^
/usr/include/assert.h:171:7: note: expanded from macro 'assert'
  171 |       if (expr)                                                         \
      |       ^
/home/runner/work/libssh2/libssh2/tests/session_fixture.c:260:12: note: 'p' is null
  260 |         if(p) {
      |            ^
/home/runner/work/libssh2/libssh2/tests/session_fixture.c:260:9: note: Taking false branch
  260 |         if(p) {
      |         ^
/home/runner/work/libssh2/libssh2/tests/session_fixture.c:272:16: note: Assuming 'len' is > 0
  272 |             if(len > 0) {
      |                ^~~~~~~
/home/runner/work/libssh2/libssh2/tests/session_fixture.c:272:13: note: Taking true branch
  272 |             if(len > 0) {
      |             ^
/home/runner/work/libssh2/libssh2/tests/session_fixture.c:273:37: note: Memory is allocated
  273 |                 filepath[curpath] = calloc(1, (size_t)len + 1);
      |                                     ^~~~~~~~~~~~~~~~~~~~~~~~~~
/home/runner/work/libssh2/libssh2/tests/session_fixture.c:523:53: note: Returned allocated memory
  523 |                                                     srcdir_path(fn_pub),
      |                                                     ^~~~~~~~~~~~~~~~~~~
/home/runner/work/libssh2/libssh2/tests/session_fixture.c:524:53: note: Calling 'srcdir_path'
  524 |                                                     srcdir_path(fn_priv),
      |                                                     ^~~~~~~~~~~~~~~~~~~~
/home/runner/work/libssh2/libssh2/tests/session_fixture.c:254:8: note: Assuming 'file' is null
  254 |     if(file) {
      |        ^~~~
/home/runner/work/libssh2/libssh2/tests/session_fixture.c:254:5: note: Taking false branch
  254 |     if(file) {
      |     ^
/home/runner/work/libssh2/libssh2/tests/session_fixture.c:284:9: note: Loop condition is true.  Entering loop body
  284 |         for(i = 0; i < curpath; ++i) {
      |         ^
/home/runner/work/libssh2/libssh2/tests/session_fixture.c:285:13: note: Memory is released
  285 |             free(filepath[i]);
      |             ^~~~~~~~~~~~~~~~~
/home/runner/work/libssh2/libssh2/tests/session_fixture.c:284:9: note: Loop condition is false. Execution continues on line 287
  284 |         for(i = 0; i < curpath; ++i) {
      |         ^
/home/runner/work/libssh2/libssh2/tests/session_fixture.c:524:53: note: Returning; memory was released
  524 |                                                     srcdir_path(fn_priv),
      |                                                     ^~~~~~~~~~~~~~~~~~~~
/home/runner/work/libssh2/libssh2/tests/session_fixture.c:521:14: note: Use of memory after it is released
  521 |         rc = libssh2_userauth_publickey_fromfile_ex(session, username,
      |              ^
  522 |                                                 (unsigned int)strlen(username),
  523 |                                                     srcdir_path(fn_pub),
      |                                                     ~~~~~~~~~~~~~~~~~~~
```
https://github.com/libssh2/libssh2/actions/runs/27881681542/job/82510240714?pr=2089

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

Updates the test session fixture helper srcdir_path() to improve cleanup behavior and safety, following earlier portability work around path handling in tests.

Changes:

  • Refactors srcdir_path() to use file-scope storage and introduces srcdir_path_free() for explicit cleanup.
  • Switches slot/index handling to size_t and uses SSH2_ARRAYSIZE() for bounds checks.
  • Avoids calling getenv("srcdir") when no input path is provided.

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

Comment thread tests/session_fixture.c Resolved
Comment thread tests/session_fixture.c Resolved

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

Comment thread tests/session_fixture.c Resolved
Comment thread tests/session_fixture.c Resolved

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

Comment thread tests/session_fixture.c Outdated
Comment on lines +65 to +67
if(curpath >= SSH2_ARRAYSIZE(filepath)) {
fprintf(stderr, "srcdir_path ran out of filepath slots.\n");
return NULL;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

There we go.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Will address in separate PR.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

→ PR #2102

@vszakats

vszakats commented Jun 20, 2026 •

Copy link
Copy Markdown
Member Author

Perhaps it'd be worth reverting to a reasonably-sized fixed length buffer to
avoid this bag of issues and error cases. With them fixed, and returning NULL,
tests will randomly fail, which also isn't helping in practice.

edit: done via #2106.

Planning to revert this in a follow-up PR.
@vszakats vszakats closed this in aae3776 Jun 20, 2026
@vszakats
vszakats deleted the curpath branch June 20, 2026 21:45
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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants