Skip to content

MAXPATHLEN not defined on hurd systems #1414

Closed

Description

@babelouest

Describe the bug
When building libssh2 on hurd* systems, the tests fail to build because MAXPATHLEN is not defined on those systems:

session_fixture.c:231:36: error: ‘MAXPATHLEN’ undeclared (first use in
this function)
  231 |     static char filepath[NUMPATHS][MAXPATHLEN];
      |                                    ^~~~~~~~~~

See Debian build logs on hurd-i386

Version (please complete the following information):

  • OS and version: Debian hurd i386 and Debian hurd amd64
  • libssh2 version: 1.11.0
  • crypto backend and version: OpenSSL 3.2.2

Additional context
The build issue is in the test file tests/session_fixture.c

The bug open in debian is #1068076

Activity

  1. vszakats commented on Jun 26, 2024

    @vszakats
    Member

    MAXPATHLEN has been in use since 54bef4c (2018-03-21), released in 1.9.0. I'm curious why this didn't come up earlier. Could be a header that I deleted recently (though the report says that not offering this macro is intentional in hurd), or tests were not built earlier?

    Either way, the patch posted by Mattias Ellert to the report looks like a good solution:
    https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1068076#15

    diff --git a/tests/session_fixture.c b/tests/session_fixture.c
    index 3bb9da2..caeabfc 100644
    --- a/tests/session_fixture.c
    +++ b/tests/session_fixture.c
    @@ -225,11 +225,7 @@ void stop_session_fixture(void)
     #define NUMPATHS 32
     const char *srcdir_path(const char *file)
     {
    -#ifdef WIN32
    -    static char filepath[NUMPATHS][_MAX_PATH];
    -#else
    -    static char filepath[NUMPATHS][MAXPATHLEN];
    -#endif
    +    static char *filepath[NUMPATHS];
         static int curpath;
         char *p = getenv("srcdir");
         if(curpath >= NUMPATHS) {
    @@ -237,16 +233,14 @@ const char *srcdir_path(const char *file)
         }
         assert(curpath < NUMPATHS);
         if(p) {
    -        /* Ensure the final string is nul-terminated on Windows */
    -        filepath[curpath][sizeof(filepath[0]) - 1] = 0;
    -        snprintf(filepath[curpath], sizeof(filepath[0]) - 1, "%s/%s",
    -                 p, file);
    +        int len = snprintf(NULL, 0, "%s/%s", p, file);
    +        filepath[curpath] = malloc(len + 1);
    +        snprintf(filepath[curpath], len + 1, "%s/%s", p, file);
         }
         else {
    -        /* Ensure the final string is nul-terminated on Windows */
    -        filepath[curpath][sizeof(filepath[0]) - 1] = 0;
    -        snprintf(filepath[curpath], sizeof(filepath[0]) - 1, "%s",
    -                 file);
    +        int len = snprintf(NULL, 0, "%s", file);
    +        filepath[curpath] = malloc(len + 1);
    +        snprintf(filepath[curpath], len + 1, "%s", file);
         }
     
         return filepath[curpath++];

    Also update the comment:

    diff --git a/tests/session_fixture.c b/tests/session_fixture.c
    index b7cac1a7..3060f4e4 100644
    --- a/tests/session_fixture.c
    +++ b/tests/session_fixture.c
    @@ -245,8 +245,7 @@ void stop_session_fixture(void)
     
     
     /* Return a static string that contains a file path relative to the srcdir
    - * variable, if found. It does so in a way that avoids leaking memory by using
    - * a fixed number of static buffers.
    + * variable, if found.
      */
     #define NUMPATHS 32
     const char *srcdir_path(const char *file)

    Would you like to open a PR for that?

  2. babelouest commented on Jun 26, 2024

    @babelouest
    ContributorAuthor

    If you're ok with the patch then I can make a PR with pleasure.

    Although the malloc in filepath[curpath] = malloc(len + 1); should be free afterwards.

  3. vszakats commented on Jun 26, 2024

    @vszakats
    Member

    Thanks, I think the patch is good. I agree to free the pointers when appropriate.

  4. added a commit that references this issue on Jun 26, 2024
    2fc429b
  5. babelouest commented on Jun 26, 2024

    @babelouest
    ContributorAuthor

    I've updated the patch to free allocated chars and made the PR #1415

    Although, I wasn't able to test the patch with valgrind to make sure all the allocated data is cleaned. Do you have a method to test memory leaks?

  6. added a commit that references this issue on Jul 3, 2024
    12427f4
  7. added a commit that references this issue on Jul 14, 2024
    1cfedc7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions