pathspec: warn on empty strings as pathspec

An empty string as a pathspec element matches all paths. A buggy script, however, could accidentally assign an empty string to a variable that then gets passed to a Git command invocation, e.g.: path=... compute a path to be removed in $path ... git rm -r "$paht" which would unintentionally remove all paths in the current directory. The fix for this issue requires a two-step approach. As there may be existing scripts that knowingly use empty strings in this manner, the first step simply gives a warning that (1) tells that an empty string will become an invalid pathspec element and (2) asks the user to use "." if they mean to match all. For step two, a follow-up patch several release cycles later will remove the warning and throw an error instead. This patch is the first step. Signed-off-by: Emily Xie <emilyxxie@gmail.com> Reported-by: David Turner <novalis@novalis.org> Mentored-by: Michail Denchev <mdenchev@gmail.com> Thanks-to: Sarah Sharp <sarah@thesharps.us> and James Sharp <jamey@minilop.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Emily Xie committed Jun 22, 2016 at 19:00 UTC d426430e6ec2a05bf0a4ee88c319dd6072908504
3 files changed +19 -2
pathspec.c
+9 -2
@@ -364,7 +364,7 @@ void parse_pathspec(struct pathspec *pathspec,
364 {
365 struct pathspec_item *item;
366 const char *entry = argv ? *argv : NULL;
367 - int i, n, prefixlen, nr_exclude = 0;
367 + int i, n, prefixlen, warn_empty_string, nr_exclude = 0;
368
369 memset(pathspec, 0, sizeof(*pathspec));
370
@@ -402,8 +402,15 @@ void parse_pathspec(struct pathspec *pathspec,
402 }
403
404 n = 0;
405 - while (argv[n])
405 + warn_empty_string = 1;
406 + while (argv[n]) {
407 + if (*argv[n] == '\0' && warn_empty_string) {
408 + warning(_("empty strings as pathspecs will be made invalid in upcoming releases. "
409 + "please use . instead if you meant to match all paths"));
410 + warn_empty_string = 0;
411 + }
412 n++;
413 + }
414
415 pathspec->nr = n;
416 ALLOC_ARRAY(pathspec->items, n);
t/t3600-rm.sh
+5
@@ -881,4 +881,9 @@ test_expect_success 'rm files with two different errors' '
881 test_i18ncmp expect actual
882 '
883
884 +test_expect_success 'rm empty string should invoke warning' '
885 + git rm -rf "" 2>output &&
886 + test_i18ngrep "warning: empty strings" output
887 +'
888 +
889 test_done
t/t3700-add.sh
+5
@@ -332,4 +332,9 @@ test_expect_success 'git add --dry-run --ignore-missing of non-existing file out
332 test_i18ncmp expect.err actual.err
333 '
334
335 +test_expect_success 'git add empty string should invoke warning' '
336 + git add "" 2>output &&
337 + test_i18ngrep "warning: empty strings" output
338 +'
339 +
340 test_done