File: maxpathlen.patch

package info (click to toggle)
libssh2 1.11.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,504 kB
  • sloc: ansic: 46,104; sh: 6,164; makefile: 348; cpp: 120; perl: 65; lisp: 33; awk: 23
file content (121 lines) | stat: -rw-r--r-- 3,717 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
From: Mattias Ellert <mattias.ellert@physics.uu.se>
From: Nicolas Mora
From: Viktor Szakats
From 2fc429b776bcd7d801c60ca2d0ef247bfeda86c9 Mon Sep 17 00:00:00 2001
Date: Wed, 26 Jun 2024 17:19:27 -0400
Subject: [PATCH 01/15] Update srcdir_path to avoid using MAXPATHLEN,
 which is not present in some systems. Backport from upstream PR #1415,
 based on Mattias Ellert's patch in #1068076
Forwarded: https://github.com/libssh2/libssh2/pull/1415

--- a/tests/session_fixture.c
+++ b/tests/session_fixture.c
@@ -44,12 +44,9 @@
 #ifdef HAVE_UNISTD_H
 #include <unistd.h>
 #endif
-#ifdef HAVE_SYS_PARAM_H
-#include <sys/param.h>
-#endif
 
 #include <stdio.h>
-#include <stdlib.h>
+#include <stdlib.h> /* for getenv() */
 #include <assert.h>
 
 static LIBSSH2_SESSION *connected_session = NULL;
@@ -212,6 +209,9 @@
     close_socket_to_openssh_server(connected_socket);
     connected_socket = LIBSSH2_INVALID_SOCKET;
 
+    /* cleanup allocated filepath */
+    srcdir_path(NULL);
+
     libssh2_exit();
 
     stop_openssh_fixture();
@@ -219,37 +219,50 @@
 
 
 /* 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)
+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) {
-        fprintf(stderr, "srcdir_path ran out of filepath slots.\n");
-    }
-    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);
+    if(file) {
+        int len;
+        if(curpath >= NUMPATHS) {
+            fprintf(stderr, "srcdir_path ran out of filepath slots.\n");
+        }
+        assert(curpath < NUMPATHS);
+        if(p) {
+            len = snprintf(NULL, 0, "%s/%s", p, file);
+            if(len > 2) {
+                filepath[curpath] = calloc(1, (size_t)len + 1);
+                snprintf(filepath[curpath], (size_t)len + 1, "%s/%s", p, file);
+            }
+            else {
+               return NULL;
+            }
+        }
+        else {
+            len = snprintf(NULL, 0, "%s", file);
+            if(len > 0) {
+                filepath[curpath] = calloc(1, (size_t)len + 1);
+                snprintf(filepath[curpath], (size_t)len + 1, "%s", file);
+            }
+            else {
+               return NULL;
+            }
+        }
+        return filepath[curpath++];
     }
     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 i;
+        for(i = 0; i < curpath; ++i) {
+            free(filepath[curpath]);
+        }
+        curpath = 0;
+        return NULL;
     }
-
-    return filepath[curpath++];
 }
 
 static const char *kbd_password;
--- a/tests/session_fixture.h
+++ b/tests/session_fixture.h
@@ -45,7 +45,7 @@
 LIBSSH2_SESSION *start_session_fixture(int *skipped, int *err);
 void stop_session_fixture(void);
 void print_last_session_error(const char *function);
-const char *srcdir_path(const char *file);
+char *srcdir_path(const char *file);
 
 #define TEST_AUTH_SHOULDFAIL  1
 #define TEST_AUTH_FROMMEM     2