Skip to content

Commit

Permalink
Fix null dereferences on unset state variable
Browse files Browse the repository at this point in the history
Tig knows three kinds of state variables that encode different
information:
1. the state of the view (ARGV_ENV_INFO), like %(commit)
2. the state of the worktree (REPO_INFO), like %(repo:head)
3. the arguments given on the commandline, like %(fileargs)

The values exposed by the first two kinds are never null,
but most of the third kind default to null.

Prior to this commit when trying to format a null value,
argv_format() reported success but left the output string
as null. Fix this by writing the empty string in format_append_argv(),
because current callers (echo) don't really care about the difference
between empty and null.

Reproduce the null dereferences with

	:!%(fileargs)
	:echo %(fileargs)

Surprisingly to me, this did not break this example:

	bind generic aaa !sh -c 'printf "%s\n" "$@" | wc -l' -- line1 %(fileargs) line2
	# still prints 2

because of the early return in argv_appendn().

In future we should also fix format_append_arg(), which currently
fails on

	:echo "%(fileargs)"

because format_expand_arg() does not receive variables like
%(fileargs).
  • Loading branch information
krobelus committed Sep 25, 2021
1 parent 529182c commit 9b262f6
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/argv.c
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ format_append_argv(struct format_context *format, const char ***dst_argv, const
int argc;

if (!src_argv)
return true;
return argv_append(dst_argv, "");

for (argc = 0; src_argv[argc]; argc++)
if (!format_append_arg(format, dst_argv, src_argv[argc]))
Expand Down
43 changes: 43 additions & 0 deletions test/regressions/github-1136-test
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/bin/sh

. libtest.sh
. libgit.sh

LINES=10

in_work_dir create_repo_from_tgz "$base_dir/files/scala-js-benchmarks.tgz"

# This runs an empty command, hence the empty pager.
test_case bang-cmdlineargs-doesnt-crash \
--args='status' \
--script='
:!%(cmdlineargs)
' <<EOF
[pager] 0%
EOF

test_case echo-cmdlineargs-doesnt-crash \
--args='status' \
--script='
:echo %(cmdlineargs)
' <<EOF
On branch master
Changes to be committed:
(no files)
Changes not staged for commit:
(no files)
Untracked files:
(no files)
[status] Nothing to update 100%
EOF

run_test_cases

0 comments on commit 9b262f6

Please sign in to comment.