diff --git a/strace.c b/strace.c index 0ee9b5e3..f5547f92 100644 --- a/strace.c +++ b/strace.c @@ -673,8 +673,8 @@ newoutf(struct tcb *tcp) { tcp->outf = shared_log; /* if not -ff mode, the same file is for all */ if (followfork >= 2) { - char name[520 + sizeof(int) * 3]; - sprintf(name, "%.512s.%u", outfname, tcp->pid); + char name[PATH_MAX]; + sprintf(name, "%s.%u", outfname, tcp->pid); tcp->outf = strace_fopen(name); } } @@ -1831,6 +1831,9 @@ init(int argc, char *argv[]) shared_log = strace_popen(outfname + 1); } else if (followfork < 2) { shared_log = strace_fopen(outfname); + } else if (strlen(outfname) >= PATH_MAX - sizeof(int) * 3) { + errno = ENAMETOOLONG; + perror_msg_and_die("%s", outfname); } } else { /* -ff without -o FILE is the same as single -f */ diff --git a/tests/options-syntax.test b/tests/options-syntax.test index 9cfcf5cd..6d6706cf 100755 --- a/tests/options-syntax.test +++ b/tests/options-syntax.test @@ -67,6 +67,9 @@ check_e_using_grep 'regcomp: \[id: [[:alpha:]].+' -e trace='/[id' check_e_using_grep 'exec: File *name too long' "$(printf '%4096s' ' ')" +ff_name="$(printf '%4084s' ' ')" +check_e_using_grep "$ff_name: File *name too long" -ff -o "$ff_name" true + check_h 'must have PROG [ARGS] or -p PID' check_h 'PROG [ARGS] must be specified with -D' -D -p $$ check_h '-c and -C are mutually exclusive' -c -C true