From e223cca4f66bf2e201b21869304dc63befbbf9be Mon Sep 17 00:00:00 2001 From: Samuel Holland Date: Sun, 20 Mar 2016 11:13:21 -0700 Subject: basename: fix segfault on null input; add tests When passed an empty string, glibc's basename() returns a pointer to the string "." in read-only memory. If an empty suffix is given, it fits the condition of being shorter than the path, so we try to overwrite the null byte and crash. Fix this by just ignoring empty suffixes; they don't do anything anyway. --- toys/posix/basename.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'toys/posix/basename.c') diff --git a/toys/posix/basename.c b/toys/posix/basename.c index 1a27a23b..c123cc79 100644 --- a/toys/posix/basename.c +++ b/toys/posix/basename.c @@ -23,7 +23,7 @@ void basename_main(void) char *base = basename(*toys.optargs), *suffix = toys.optargs[1]; // chop off the suffix if provided - if (suffix) { + if (suffix && *suffix) { long bl = strlen(base), sl = strlen(suffix); char *s = base + bl - sl; -- cgit v1.2.3