git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH v4] diff: make diff_free_filespec_data accept NULL

From
Junio C Hamano <gitster@pobox.com>
Date
Nov 10, 2020, 19:41 UTC
Message-ID
<xmqqpn4l6ltk.fsf@gitster.c.googlers.com>
In-Reply-To
<137f0fc1-fbd9-a62c-bd52-cffd26c364bf@theori.io>
Jinoh Kang <luke1337@theori.io> writes:
> Today, diff_free_filespec_data crashes when passed a NULL pointer.

No need to say "Today". We state how things are in the current codebase in the present tense, make observations on the way things can break (i.e. identify a bug), and outline an approach to correct it.

Show 12 quoted lines
> Commit 3aef54e8b8 ("diff: munmap() file contents before running external
> diff") introduced calls to diff_free_filespec_data in run_external_diff,
> which may pass NULL pointers.
>
> Git uses NULL filespecs to indicate unmerged files when merge conflict
> resolution is in progress.  Fortunately, other code paths bail out early
> even before NULL can reach diff_free_filespec_data(); however, difftool
> is expected to do a full-blown diff anyway regardless of conflict
> status.
>
> Fix this and prevent any similar bugs in the future by making
> `diff_free_filespec_data(NULL)` a no-op.
Nicely described.
> Also, add a test case that confirms that running difftool --cached with
> unmerged files does not SIGSEGV.
Show 7 quoted lines
> +test_expect_success 'difftool --cached with unmerged files' '
> +	test_when_finished git reset --hard &&
> +	echo base >file &&
> +	git add file &&
> +	git commit -m base &&
> +	git checkout -B conflict-a &&
> +	git checkout -B conflict-b &&

The above two are not wrong per-se, but would conceptually be cleaner to use "git branch -f", because the next thing you do immediately after preparing two branches is to start working on the 'A' side, below.

You could alternatively drop the above two lines and then instead turn this

> +	git checkout conflict-a &&

into "git checkout -B conflict-a master" (and similarly on the 'B' side below), which would reduce the test by two lines. That would be what I would recommend to do under normal circumstances, but since there is a separate topic that wages war on the 'master' branch, I wouldn't recommend it.

> +	echo conflict-a >>file &&
> +	git add file &&
> +	git commit -m conflict-a &&
> +	git checkout conflict-b &&
> +	echo conflict-b >>file &&
> +	git add file &&
> +	git commit -m conflict-b &&
> +	git checkout master &&
> +	git merge conflict-a &&
> +	test_must_fail git merge conflict-b &&
> +	: >expect &&
> +	git difftool --cached --no-prompt >actual &&
> +	test_cmp expect actual

Shouldn't we omit 'expect' and use test_must_be_empty helper instead?

	git difftool --cached --no-prompt >actual &&
	test_must_be_empty actual
Show 5 quoted lines
> +'
> +
>  test_expect_success 'outside worktree' '
>  	echo 1 >1 &&
>  	echo 2 >2 &&
Previous: Johannes SchindelinNext: Jinoh Kang
Message 11 of 15 in “diff: handle NULL filespecs in run_external_diff”
  1. diff: handle NULL filespecs in run_external_diffJinoh Kang, Nov 6, 2020
  2. diff: make diff_free_filespec_data accept NULLJinoh Kang, Nov 6, 2020
  3. Johannes SchindelinNov 10, 2020
  4. Jinoh KangNov 10, 2020
  5. Jinoh KangNov 10, 2020
  6. Junio C HamanoNov 10, 2020
  7. diff: make diff_free_filespec_data accept NULLJinoh Kang, Nov 10, 2020
  8. Johannes SchindelinNov 10, 2020
  9. Jinoh KangNov 11, 2020
  10. Johannes SchindelinNov 11, 2020
  11. Junio C HamanoNov 10, 2020
  12. diff: make diff_free_filespec_data accept NULLJinoh Kang, Nov 11, 2020
  13. Johannes SchindelinNov 11, 2020
  14. Junio C HamanoNov 11, 2020
  15. Junio C HamanoNov 6, 2020

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.