blame: fix -L bounds checking bug

Since inception, -LX,Y has correctly reported an out-of-range error when Y is beyond end of file, however, X was not checked, and an out-of-range X would cause a crash. 92f9e273 (blame: prevent a segv when -L given start > EOF; 2010-02-08) attempted to rectify this shortcoming but has its own off-by-one error which allows X to extend one line past end of file. For example, given a file with 5 lines: git blame -L5 foo # OK, blames line 5 git blame -L6 foo # accepted, no error, no output, huh? git blame -L7 foo # error "fatal: file foo has only 5 lines" Fix this bug. In order to avoid regressing "blame foo" when foo is an empty file, the fix is slightly more complicated than changing '<' to '<='. Signed-off-by: Eric Sunshine <sunshine@sunshineco.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Eric Sunshine committed Jul 31, 2013 at 04:15 UTC 164a9cf4303d4a8e5c969e057adf9d700468ddc5
2 files changed +6 -6
builtin/blame.c
+2 -2
@@ -2495,13 +2495,13 @@ parse_done:
2495 bottom = top = 0;
2496 if (bottomtop)
2497 prepare_blame_range(&sb, bottomtop, lno, &bottom, &top);
2498 + if (lno < top || ((lno || bottom) && lno < bottom))
2499 + die("file %s has only %lu lines", path, lno);
2500 if (bottom < 1)
2501 bottom = 1;
2502 if (top < 1)
2503 top = lno;
2504 bottom--;
2503 - if (lno < top || lno < bottom)
2504 - die("file %s has only %lu lines", path, lno);
2505
2506 ent = xcalloc(1, sizeof(*ent));
2507 ent->lno = bottom;
t/annotate-tests.sh
+4 -4
@@ -232,7 +232,7 @@ test_expect_success 'blame -L X (X == nlines)' '
232 check_count -L$n C 1
233 '
234
235 -test_expect_failure 'blame -L X (X == nlines + 1)' '
235 +test_expect_success 'blame -L X (X == nlines + 1)' '
236 n=$(expr $(wc -l <file) + 2) &&
237 test_must_fail $PROG -L$n file
238 '
@@ -321,7 +321,7 @@ test_expect_success 'blame -L 0 empty (undocumented)' '
321 check_count -h HEAD^^ -f incremental -L0
322 '
323
324 -test_expect_failure 'blame -L 1 empty' '
324 +test_expect_success 'blame -L 1 empty' '
325 test_must_fail $PROG -L1 incremental HEAD^^
326 '
327
@@ -341,7 +341,7 @@ test_expect_success 'blame -L 1 half' '
341 check_count -h HEAD^ -f incremental -L1 I 1
342 '
343
344 -test_expect_failure 'blame -L 2 half' '
344 +test_expect_success 'blame -L 2 half' '
345 test_must_fail $PROG -L2 incremental HEAD^
346 '
347
@@ -361,7 +361,7 @@ test_expect_success 'blame -L 1 full' '
361 check_count -f incremental -L1 I 1
362 '
363
364 -test_expect_failure 'blame -L 2 full' '
364 +test_expect_success 'blame -L 2 full' '
365 test_must_fail $PROG -L2 incremental
366 '
367