Fix some issues found found by valgrind.

Buffer underflow and though strcpy has always been safe there, change to
memmove. Had the added benefit of helping me create more test cases for
better coverage.
This commit is contained in:
Bill Currie 2012-01-24 19:54:21 +09:00
parent 1ef24e0404
commit d8d21b00f4
2 changed files with 11 additions and 5 deletions

View file

@ -776,12 +776,13 @@ QFS_CompressPath (const char *pth)
p += 2 + (p[2] == '/');
continue;
}
strcpy (d, p + 2 + (p[2] == '/'));
p = p + 2 + (p[2] == '/');
memmove (d, p, strlen (p) + 1);
p = d;
continue;
}
} else if (p[1] == '/') {
strcpy (p, p + 2);
memmove (p, p + 2, strlen (p + 2) + 1);
continue;
} else if (p[1] == 0) {
p[0] = 0;
@ -795,11 +796,11 @@ QFS_CompressPath (const char *pth)
for (d = p; *d == '/'; d++)
;
if (d != p)
strcpy (p, d);
memmove (p, d, strlen (d) + 1);
}
}
// strip any trailing /, but not if it's the root /
if (--p != path && *p == '/')
if (--p > path && *p == '/')
*p = 0;
return path;

View file

@ -16,6 +16,8 @@ struct {
const char *expect;
} path_tests [] = {
{"", ""},
{"/x", "/x"},
{"x/", "x"},
{"/", "/"},
{"\\", "/"},
{".", ""},
@ -25,7 +27,9 @@ struct {
{"/..", "/"},
{"foo/..", ""},
{"foo/bar/..", "foo"},
{"foo/bar/.", "foo/bar"},
{"foo//bar", "foo/bar"},
{"foo/./bar", "foo/bar"},
{"../foo/..", ".."},
{"\\blah\\../foo/..\\baz/.\\x", "/baz/x"},
};
@ -70,12 +74,13 @@ main (int argc, const char **argv)
int res = 0;
for (i = 0; i < num_path_tests; i++) {
const char *cpath = QFS_CompressPath (path_tests[i].path);
char *cpath = QFS_CompressPath (path_tests[i].path);
if (strcmp (cpath, path_tests[i].expect)) {
fprintf (stderr, "FAIL: (%zd) \"%s\" -> \"%s\", got \"%s\"\n", i,
path_tests[i].path, path_tests[i].expect, cpath);
res = 1;
}
free (cpath);
}
for (i = 0; i < num_ext_tests; i++) {