The correct fix is to not assume a maximum length for paths, but use
dynamic allocation with the proper length.

One way to do this can be seen in the attached patch (calling snprintf
twics).

Another options is to calculate the needed length by adding up the
strlen() result for each of the strings and adding the constant length
needed for the constant bits of the format string.

This will work for systems that define PATH_MAX/MAXPATHLEN too, so
there is no need for ifdefs.

        Mattias

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++];

Attachment: signature.asc
Description: This is a digitally signed message part

Reply via email to