|
46 | 46 | #ifdef HAVE_UNISTD_H
|
47 | 47 | #include <unistd.h>
|
48 | 48 | #endif
|
49 |
| -#ifdef HAVE_SYS_PARAM_H |
50 |
| -#include <sys/param.h> |
51 |
| -#endif |
52 | 49 |
|
53 | 50 | #include <stdio.h>
|
54 |
| -#include <stdlib.h> |
| 51 | +#include <stdlib.h> /* for getenv() */ |
55 | 52 | #include <assert.h>
|
56 | 53 |
|
57 | 54 | static LIBSSH2_SESSION *connected_session = NULL;
|
@@ -238,44 +235,59 @@ void stop_session_fixture(void)
|
238 | 235 | close_socket_to_openssh_server(connected_socket);
|
239 | 236 | connected_socket = LIBSSH2_INVALID_SOCKET;
|
240 | 237 |
|
| 238 | + srcdir_path(NULL); /* cleanup allocated filepath */ |
| 239 | + |
241 | 240 | libssh2_exit();
|
242 | 241 |
|
243 | 242 | stop_openssh_fixture();
|
244 | 243 | }
|
245 | 244 |
|
246 | 245 |
|
247 | 246 | /* Return a static string that contains a file path relative to the srcdir
|
248 |
| - * variable, if found. It does so in a way that avoids leaking memory by using |
249 |
| - * a fixed number of static buffers. |
| 247 | + * variable, if found. |
250 | 248 | */
|
251 | 249 | #define NUMPATHS 32
|
252 |
| -const char *srcdir_path(const char *file) |
| 250 | +char *srcdir_path(const char *file) |
253 | 251 | {
|
254 |
| -#ifdef _WIN32 |
255 |
| - static char filepath[NUMPATHS][_MAX_PATH]; |
256 |
| -#else |
257 |
| - static char filepath[NUMPATHS][MAXPATHLEN]; |
258 |
| -#endif |
| 252 | + static char *filepath[NUMPATHS]; |
259 | 253 | static int curpath;
|
260 | 254 | char *p = getenv("srcdir");
|
261 |
| - if(curpath >= NUMPATHS) { |
262 |
| - fprintf(stderr, "srcdir_path ran out of filepath slots.\n"); |
263 |
| - } |
264 |
| - assert(curpath < NUMPATHS); |
265 |
| - if(p) { |
266 |
| - /* Ensure the final string is nul-terminated on Windows */ |
267 |
| - filepath[curpath][sizeof(filepath[0]) - 1] = 0; |
268 |
| - snprintf(filepath[curpath], sizeof(filepath[0]) - 1, "%s/%s", |
269 |
| - p, file); |
| 255 | + if(file) { |
| 256 | + int len; |
| 257 | + if(curpath >= NUMPATHS) { |
| 258 | + fprintf(stderr, "srcdir_path ran out of filepath slots.\n"); |
| 259 | + } |
| 260 | + assert(curpath < NUMPATHS); |
| 261 | + if(p) { |
| 262 | + len = snprintf(NULL, 0, "%s/%s", p, file); |
| 263 | + if(len > 2) { |
| 264 | + filepath[curpath] = calloc(1, (size_t)len + 1); |
| 265 | + snprintf(filepath[curpath], (size_t)len + 1, "%s/%s", p, file); |
| 266 | + } |
| 267 | + else { |
| 268 | + return NULL; |
| 269 | + } |
| 270 | + } |
| 271 | + else { |
| 272 | + len = snprintf(NULL, 0, "%s", file); |
| 273 | + if(len > 0) { |
| 274 | + filepath[curpath] = calloc(1, (size_t)len + 1); |
| 275 | + snprintf(filepath[curpath], (size_t)len + 1, "%s", file); |
| 276 | + } |
| 277 | + else { |
| 278 | + return NULL; |
| 279 | + } |
| 280 | + } |
| 281 | + return filepath[curpath++]; |
270 | 282 | }
|
271 | 283 | else {
|
272 |
| - /* Ensure the final string is nul-terminated on Windows */ |
273 |
| - filepath[curpath][sizeof(filepath[0]) - 1] = 0; |
274 |
| - snprintf(filepath[curpath], sizeof(filepath[0]) - 1, "%s", |
275 |
| - file); |
| 284 | + int i; |
| 285 | + for(i = 0; i < curpath; ++i) { |
| 286 | + free(filepath[curpath]); |
| 287 | + } |
| 288 | + curpath = 0; |
| 289 | + return NULL; |
276 | 290 | }
|
277 |
| - |
278 |
| - return filepath[curpath++]; |
279 | 291 | }
|
280 | 292 |
|
281 | 293 | static const char *kbd_password;
|
|
0 commit comments