Skip to content

Commit 12427f4

Browse files
babelouestvszakats
authored andcommitted
tests: avoid using MAXPATHLEN, for portability
`MAXPATHLEN` is not present in some systems, e.g. GNU Hurd. Co-authored-by: Viktor Szakats Ref: 54bef4c libssh2#198 Fixes libssh2#1414 Closes libssh2#1415
1 parent a031069 commit 12427f4

File tree

2 files changed

+40
-28
lines changed

2 files changed

+40
-28
lines changed

tests/session_fixture.c

+39-27
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,9 @@
4646
#ifdef HAVE_UNISTD_H
4747
#include <unistd.h>
4848
#endif
49-
#ifdef HAVE_SYS_PARAM_H
50-
#include <sys/param.h>
51-
#endif
5249

5350
#include <stdio.h>
54-
#include <stdlib.h>
51+
#include <stdlib.h> /* for getenv() */
5552
#include <assert.h>
5653

5754
static LIBSSH2_SESSION *connected_session = NULL;
@@ -238,44 +235,59 @@ void stop_session_fixture(void)
238235
close_socket_to_openssh_server(connected_socket);
239236
connected_socket = LIBSSH2_INVALID_SOCKET;
240237

238+
srcdir_path(NULL); /* cleanup allocated filepath */
239+
241240
libssh2_exit();
242241

243242
stop_openssh_fixture();
244243
}
245244

246245

247246
/* 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.
250248
*/
251249
#define NUMPATHS 32
252-
const char *srcdir_path(const char *file)
250+
char *srcdir_path(const char *file)
253251
{
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];
259253
static int curpath;
260254
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++];
270282
}
271283
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;
276290
}
277-
278-
return filepath[curpath++];
279291
}
280292

281293
static const char *kbd_password;

tests/session_fixture.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
LIBSSH2_SESSION *start_session_fixture(int *skipped, int *err);
4848
void stop_session_fixture(void);
4949
void print_last_session_error(const char *function);
50-
const char *srcdir_path(const char *file);
50+
char *srcdir_path(const char *file);
5151

5252
#define TEST_AUTH_SHOULDFAIL 1
5353
#define TEST_AUTH_FROMMEM 2

0 commit comments

Comments
 (0)