Skip to content
Permalink
Browse files

Bug#4194: Add more checks for empty paths in pr_fs_dircat().

  • Loading branch information...
tjsaunders
tjsaunders committed Jul 25, 2015
1 parent 93577b9 commit f99ef850a05f46c56be8deae97e59efa50575e69
Showing with 38 additions and 1 deletion.
  1. +22 −1 src/fsio.c
  2. +16 −0 tests/api/fsio.c
@@ -2040,6 +2040,26 @@ int pr_fs_dircat(char *buf, int buflen, const char *dir1, const char *dir2) {
dir1len = strlen(dir1);
dir2len = strlen(dir2);

/* If both strings are empty, then the "concatenation" becomes trivial. */
if (dir1len == 0 &&
dir2len == 0) {
buf[0] = '/';
buf[1] = '\0';
return 0;
}

/* If dir2 is non-empty, but dir1 IS empty... */
if (dir1len == 0) {
sstrncpy(buf, dir2, buflen);
return 0;
}

/* Likewise, if dir1 is non-empty, but dir2 IS empty... */
if (dir2len == 0) {
sstrncpy(buf, dir1, buflen);
return 0;
}

if ((dir1len + dir2len + 1) >= PR_TUNABLE_PATH_MAX) {
errno = ENAMETOOLONG;
buf[0] = '\0';
@@ -2074,7 +2094,8 @@ int pr_fs_dircat(char *buf, int buflen, const char *dir1, const char *dir2) {
buflen -= dir1len;

if (buflen > 0 &&
*(_dir1 + (dir1len-1)) != '/') {
dir1len >= 1 &&
*(_dir1 + (dir1len-1)) != '/') {
sstrcat(ptr, "/", buflen);
ptr += 1;
buflen -= 1;
@@ -190,6 +190,22 @@ START_TEST (fs_dircat_test) {
fail_unless(res == 0, "Failed to concatenate two empty paths");
fail_unless(strcmp(buf, ok) == 0, "Expected concatenated dir '%s', got '%s'",
ok, buf);

a = "/foo";
b = "";
ok = "/foo";
res = pr_fs_dircat(buf, sizeof(buf)-1, a, b);
fail_unless(res == 0, "Failed to concatenate two empty paths");
fail_unless(strcmp(buf, ok) == 0, "Expected concatenated dir '%s', got '%s'",
ok, buf);

a = "";
b = "/bar";
ok = "/bar";
res = pr_fs_dircat(buf, sizeof(buf)-1, a, b);
fail_unless(res == 0, "Failed to concatenate two empty paths");
fail_unless(strcmp(buf, ok) == 0, "Expected concatenated dir '%s', got '%s'",
ok, buf);
}
END_TEST

0 comments on commit f99ef85

Please sign in to comment.
You can’t perform that action at this time.