diff: ignore unmerged paths outside prefix with --relative --cached

A diff using --relative ignores entries outside the current directory.
This results in a segfault when we try to process an unmerged entry
that's outside of our prefix, since we end up with a NULL diff_filepair
and use it without checking that it's valid.

I think this bug goes back to 76399c0195 (diff.c: return filepair from
diff_unmerge(), 2011-04-22). Prior to that, diff_unmerge() knew to skip
entries outside of our prefix, due to cd676a5136 (diff --relative:
output paths as relative to the current subdirectory, 2008-02-12). Back
then the caller didn't care that we hadn't added anything to the queue.
In 76399c0195 that changed; we now returned the pair (or NULL), and the
caller in do_oneway_diff() was then called fill_filespec() itself. And
it does so without checking for NULL, causing a segfault.

The obvious fix is to skip the fill_filespec() call (after which we just
return), which this patch does.

There's another call to diff_unmerge() in run_diff_files(). That case
was already fixed by 8174627b3d (diff-lib: ignore paths that are outside
$cwd if --relative asked, 2021-08-22), but of course it didn't help us
for --cached.

That commit also claims that checking the result of diff_unmerge() is
not enough, as we'd want other code paths to skip the entry, too (even
if they wouldn't segfault). But as far as I can tell, that is not true
for --cached. We eventually end up in diff_queue_addremove() or in
diff_queue_change(), both of which know to return early when we're
outside of the prefix.

Arguably we could be checking at the top of oneway_diff() whether the
path is interesting at all. That would not only avoid this code path
entirely, but would also possibly save a small amount of work. But since
everything else appears to work OK, I went for the smallest fix here to
avoid any regression.

Specifically, a comment in oneway_diff() claims we're supposed to
advance o->pos, which we might fail to do if we return early. Though
that "advance" seems to have gone away in da165f470e (unpack-trees.c:
prepare for looking ahead in the index, 2010-01-07), so it is possible
the comment is simply out of date.  We can explore that separately;
checking for a NULL return from diff_unmerge() seems like a sensible
thing to do regardless.

We can piggy-back on the tests added by 8174627b3d; we're just checking
the --cached variant.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2 files changed
tree: c70bff3a60cc5c96a7756d893bb8efcfd65041a8
  1. .github/
  2. bin-wrappers/
  3. block-sha1/
  4. builtin/
  5. ci/
  6. compat/
  7. compiler-tricks/
  8. contrib/
  9. Documentation/
  10. ewah/
  11. git-gui/
  12. gitk-git/
  13. gitweb/
  14. mergetools/
  15. negotiator/
  16. odb/
  17. oss-fuzz/
  18. perl/
  19. po/
  20. refs/
  21. reftable/
  22. sha1/
  23. sha1dc/
  24. sha256/
  25. src/
  26. subprojects/
  27. t/
  28. templates/
  29. tools/
  30. trace2/
  31. xdiff/
  32. .cirrus.yml
  33. .clang-format
  34. .editorconfig
  35. .gitattributes
  36. .gitignore
  37. .gitlab-ci.yml
  38. .gitmodules
  39. .mailmap
  40. .tsan-suppressions
  41. abspath.c
  42. abspath.h
  43. aclocal.m4
  44. add-interactive.c
  45. add-interactive.h
  46. add-patch.c
  47. add-patch.h
  48. advice.c
  49. advice.h
  50. alias.c
  51. alias.h
  52. alloc.c
  53. alloc.h
  54. apply.c
  55. apply.h
  56. archive-tar.c
  57. archive-zip.c
  58. archive.c
  59. archive.h
  60. attr.c
  61. attr.h
  62. banned.h
  63. base85.c
  64. base85.h
  65. bisect.c
  66. bisect.h
  67. blame.c
  68. blame.h
  69. blob.c
  70. blob.h
  71. bloom.c
  72. bloom.h
  73. branch.c
  74. branch.h
  75. build.rs
  76. builtin.h
  77. bundle-uri.c
  78. bundle-uri.h
  79. bundle.c
  80. bundle.h
  81. cache-tree.c
  82. cache-tree.h
  83. Cargo.toml
  84. cbtree.c
  85. cbtree.h
  86. chdir-notify.c
  87. chdir-notify.h
  88. checkout.c
  89. checkout.h
  90. chunk-format.c
  91. chunk-format.h
  92. CODE_OF_CONDUCT.md
  93. color.c
  94. color.h
  95. column.c
  96. column.h
  97. combine-diff.c
  98. command-list.txt
  99. commit-graph.c
  100. commit-graph.h
  101. commit-reach.c
  102. commit-reach.h
  103. commit-slab-decl.h
  104. commit-slab-impl.h
  105. commit-slab.h
  106. commit.c
  107. commit.h
  108. common-exit.c
  109. common-init.c
  110. common-init.h
  111. common-main.c
  112. config.c
  113. config.h
  114. config.mak.dev
  115. config.mak.in
  116. config.mak.uname
  117. configure.ac
  118. connect.c
  119. connect.h
  120. connected.c
  121. connected.h
  122. convert.c
  123. convert.h
  124. copy.c
  125. copy.h
  126. COPYING
  127. credential.c
  128. credential.h
  129. csum-file.c
  130. csum-file.h
  131. ctype.c
  132. daemon.c
  133. date.c
  134. date.h
  135. decorate.c
  136. decorate.h
  137. delta-islands.c
  138. delta-islands.h
  139. delta.h
  140. diagnose.c
  141. diagnose.h
  142. diff-delta.c
  143. diff-lib.c
  144. diff-merges.c
  145. diff-merges.h
  146. diff-no-index.c
  147. diff.c
  148. diff.h
  149. diffcore-break.c
  150. diffcore-delta.c
  151. diffcore-order.c
  152. diffcore-pickaxe.c
  153. diffcore-rename.c
  154. diffcore-rotate.c
  155. diffcore.h
  156. dir-iterator.c
  157. dir-iterator.h
  158. dir.c
  159. dir.h
  160. editor.c
  161. editor.h
  162. entry.c
  163. entry.h
  164. environment.c
  165. environment.h
  166. exec-cmd.c
  167. exec-cmd.h
  168. fetch-negotiator.c
  169. fetch-negotiator.h
  170. fetch-pack.c
  171. fetch-pack.h
  172. fmt-merge-msg.c
  173. fmt-merge-msg.h
  174. for-each-ref.h
  175. fsck.c
  176. fsck.h
  177. fsmonitor--daemon.h
  178. fsmonitor-ipc.c
  179. fsmonitor-ipc.h
  180. fsmonitor-ll.h
  181. fsmonitor-path-utils.h
  182. fsmonitor-settings.c
  183. fsmonitor-settings.h
  184. fsmonitor.c
  185. fsmonitor.h
  186. gettext.c
  187. gettext.h
  188. git-archimport.perl
  189. GIT-BUILD-OPTIONS.in
  190. git-compat-util.h
  191. git-curl-compat.h
  192. git-cvsexportcommit.perl
  193. git-cvsimport.perl
  194. git-cvsserver.perl
  195. git-difftool--helper.sh
  196. git-filter-branch.sh
  197. git-instaweb.sh
  198. git-merge-octopus.sh
  199. git-merge-one-file.sh
  200. git-merge-resolve.sh
  201. git-mergetool--lib.sh
  202. git-mergetool.sh
  203. git-p4.py
  204. git-quiltimport.sh
  205. git-request-pull.sh
  206. git-send-email.perl
  207. git-sh-i18n.sh
  208. git-sh-setup.sh
  209. git-submodule.sh
  210. git-svn.perl
  211. GIT-VERSION-FILE.in
  212. GIT-VERSION-GEN
  213. git-web--browse.sh
  214. git-zlib.c
  215. git-zlib.h
  216. git.c
  217. git.rc.in
  218. gpg-interface.c
  219. gpg-interface.h
  220. graph.c
  221. graph.h
  222. grep.c
  223. grep.h
  224. hash-lookup.c
  225. hash-lookup.h
  226. hash.c
  227. hash.h
  228. hashmap.c
  229. hashmap.h
  230. help.c
  231. help.h
  232. hex-ll.c
  233. hex-ll.h
  234. hex.c
  235. hex.h
  236. hook.c
  237. hook.h
  238. http-backend.c
  239. http-fetch.c
  240. http-push.c
  241. http-walker.c
  242. http.c
  243. http.h
  244. ident.c
  245. ident.h
  246. imap-send.c
  247. INSTALL
  248. iterator.h
  249. json-writer.c
  250. json-writer.h
  251. khash.h
  252. kwset.c
  253. kwset.h
  254. levenshtein.c
  255. levenshtein.h
  256. LGPL-2.1
  257. line-log.c
  258. line-log.h
  259. line-range.c
  260. line-range.h
  261. linear-assignment.c
  262. linear-assignment.h
  263. list-objects-filter-options.c
  264. list-objects-filter-options.h
  265. list-objects-filter.c
  266. list-objects-filter.h
  267. list-objects.c
  268. list-objects.h
  269. list.h
  270. lockfile.c
  271. lockfile.h
  272. log-tree.c
  273. log-tree.h
  274. loose.c
  275. loose.h
  276. ls-refs.c
  277. ls-refs.h
  278. mailinfo.c
  279. mailinfo.h
  280. mailmap.c
  281. mailmap.h
  282. Makefile
  283. match-trees.c
  284. match-trees.h
  285. mem-pool.c
  286. mem-pool.h
  287. merge-blobs.c
  288. merge-blobs.h
  289. merge-ll.c
  290. merge-ll.h
  291. merge-ort-wrappers.c
  292. merge-ort-wrappers.h
  293. merge-ort.c
  294. merge-ort.h
  295. merge.c
  296. merge.h
  297. mergesort.h
  298. meson.build
  299. meson_options.txt
  300. midx-write.c
  301. midx.c
  302. midx.h
  303. name-hash.c
  304. name-hash.h
  305. notes-cache.c
  306. notes-cache.h
  307. notes-merge.c
  308. notes-merge.h
  309. notes-utils.c
  310. notes-utils.h
  311. notes.c
  312. notes.h
  313. object-file-convert.c
  314. object-file-convert.h
  315. object-file.c
  316. object-file.h
  317. object-name.c
  318. object-name.h
  319. object.c
  320. object.h
  321. odb.c
  322. odb.h
  323. oid-array.c
  324. oid-array.h
  325. oidmap.c
  326. oidmap.h
  327. oidset.c
  328. oidset.h
  329. oidtree.c
  330. oidtree.h
  331. pack-bitmap-write.c
  332. pack-bitmap.c
  333. pack-bitmap.h
  334. pack-check.c
  335. pack-mtimes.c
  336. pack-mtimes.h
  337. pack-objects.c
  338. pack-objects.h
  339. pack-refs.c
  340. pack-refs.h
  341. pack-revindex.c
  342. pack-revindex.h
  343. pack-write.c
  344. pack.h
  345. packfile.c
  346. packfile.h
  347. pager.c
  348. pager.h
  349. parallel-checkout.c
  350. parallel-checkout.h
  351. parse-options-cb.c
  352. parse-options.c
  353. parse-options.h
  354. parse.c
  355. parse.h
  356. patch-delta.c
  357. patch-ids.c
  358. patch-ids.h
  359. path-walk.c
  360. path-walk.h
  361. path.c
  362. path.h
  363. pathspec.c
  364. pathspec.h
  365. pkt-line.c
  366. pkt-line.h
  367. preload-index.c
  368. preload-index.h
  369. pretty.c
  370. pretty.h
  371. prio-queue.c
  372. prio-queue.h
  373. progress.c
  374. progress.h
  375. promisor-remote.c
  376. promisor-remote.h
  377. prompt.c
  378. prompt.h
  379. protocol-caps.c
  380. protocol-caps.h
  381. protocol.c
  382. protocol.h
  383. prune-packed.c
  384. prune-packed.h
  385. pseudo-merge.c
  386. pseudo-merge.h
  387. quote.c
  388. quote.h
  389. range-diff.c
  390. range-diff.h
  391. reachable.c
  392. reachable.h
  393. read-cache-ll.h
  394. read-cache.c
  395. read-cache.h
  396. README.md
  397. rebase-interactive.c
  398. rebase-interactive.h
  399. rebase.c
  400. rebase.h
  401. ref-filter.c
  402. ref-filter.h
  403. reflog-walk.c
  404. reflog-walk.h
  405. reflog.c
  406. reflog.h
  407. refs.c
  408. refs.h
  409. refspec.c
  410. refspec.h
  411. remote-curl.c
  412. remote.c
  413. remote.h
  414. repack-cruft.c
  415. repack-filtered.c
  416. repack-geometry.c
  417. repack-midx.c
  418. repack-promisor.c
  419. repack.c
  420. repack.h
  421. replace-object.c
  422. replace-object.h
  423. replay.c
  424. replay.h
  425. repo-settings.c
  426. repo-settings.h
  427. repository.c
  428. repository.h
  429. rerere.c
  430. rerere.h
  431. reset.c
  432. reset.h
  433. resolve-undo.c
  434. resolve-undo.h
  435. revision.c
  436. revision.h
  437. run-command.c
  438. run-command.h
  439. sane-ctype.h
  440. scalar.c
  441. SECURITY.md
  442. send-pack.c
  443. send-pack.h
  444. sequencer.c
  445. sequencer.h
  446. serve.c
  447. serve.h
  448. server-info.c
  449. server-info.h
  450. setup.c
  451. setup.h
  452. sh-i18n--envsubst.c
  453. sha1dc_git.c
  454. sha1dc_git.h
  455. shallow.c
  456. shallow.h
  457. shared.mak
  458. shell.c
  459. shortlog.h
  460. sideband.c
  461. sideband.h
  462. sigchain.c
  463. sigchain.h
  464. simple-ipc.h
  465. sparse-index.c
  466. sparse-index.h
  467. split-index.c
  468. split-index.h
  469. stable-qsort.c
  470. statinfo.c
  471. statinfo.h
  472. strbuf.c
  473. strbuf.h
  474. string-list.c
  475. string-list.h
  476. strmap.c
  477. strmap.h
  478. strvec.c
  479. strvec.h
  480. sub-process.c
  481. sub-process.h
  482. submodule-config.c
  483. submodule-config.h
  484. submodule.c
  485. submodule.h
  486. symlinks.c
  487. symlinks.h
  488. tag.c
  489. tag.h
  490. tar.h
  491. tempfile.c
  492. tempfile.h
  493. thread-utils.c
  494. thread-utils.h
  495. tmp-objdir.c
  496. tmp-objdir.h
  497. trace.c
  498. trace.h
  499. trace2.c
  500. trace2.h
  501. trailer.c
  502. trailer.h
  503. transport-helper.c
  504. transport-internal.h
  505. transport.c
  506. transport.h
  507. tree-diff.c
  508. tree-walk.c
  509. tree-walk.h
  510. tree.c
  511. tree.h
  512. unicode-width.h
  513. unimplemented.sh
  514. unix-socket.c
  515. unix-socket.h
  516. unix-stream-server.c
  517. unix-stream-server.h
  518. unpack-trees.c
  519. unpack-trees.h
  520. upload-pack.c
  521. upload-pack.h
  522. url.c
  523. url.h
  524. urlmatch.c
  525. urlmatch.h
  526. usage.c
  527. userdiff.c
  528. userdiff.h
  529. utf8.c
  530. utf8.h
  531. varint.c
  532. varint.h
  533. version-def.h.in
  534. version.c
  535. version.h
  536. versioncmp.c
  537. versioncmp.h
  538. walker.c
  539. walker.h
  540. wildmatch.c
  541. wildmatch.h
  542. worktree.c
  543. worktree.h
  544. wrapper.c
  545. wrapper.h
  546. write-or-die.c
  547. write-or-die.h
  548. ws.c
  549. ws.h
  550. wt-status.c
  551. wt-status.h
  552. xdiff-interface.c
  553. xdiff-interface.h
README.md

Build status

Git - fast, scalable, distributed revision control system

Git is a fast, scalable, distributed revision control system with an unusually rich command set that provides both high-level operations and full access to internals.

Git is an Open Source project covered by the GNU General Public License version 2 (some parts of it are under different licenses, compatible with the GPLv2). It was originally written by Linus Torvalds with help of a group of hackers around the net.

Please read the file INSTALL for installation instructions.

Many Git online resources are accessible from https://git-scm.com/ including full documentation and Git related tools.

See Documentation/gittutorial.adoc to get started, then see Documentation/giteveryday.adoc for a useful minimum set of commands, and Documentation/git-<commandname>.adoc for documentation of each command. If git has been correctly installed, then the tutorial can also be read with man gittutorial or git help tutorial, and the documentation of each command with man git-<commandname> or git help <commandname>.

CVS users may also want to read Documentation/gitcvs-migration.adoc (man gitcvs-migration or git help cvs-migration if git is installed).

The user discussion and development of Git take place on the Git mailing list -- everyone is welcome to post bug reports, feature requests, comments and patches to git@vger.kernel.org (read Documentation/SubmittingPatches for instructions on patch submission and Documentation/CodingGuidelines).

Those wishing to help with error message, usage and informational message string translations (localization l10) should see po/README.md (a po file is a Portable Object file that holds the translations).

To subscribe to the list, send an email to git+subscribe@vger.kernel.org (see https://subspace.kernel.org/subscribing.html for details). The mailing list archives are available at https://lore.kernel.org/git/, https://marc.info/?l=git and other archival sites.

Issues which are security relevant should be disclosed privately to the Git Security mailing list git-security@googlegroups.com.

The maintainer frequently sends the “What's cooking” reports that list the current status of various development topics to the mailing list. The discussion following them give a good reference for project status, development direction and remaining tasks.

The name “git” was given by Linus Torvalds when he wrote the very first version. He described the tool as “the stupid content tracker” and the name as (depending on your mood):

  • random three-letter combination that is pronounceable, and not actually used by any common UNIX command. The fact that it is a mispronunciation of “get” may or may not be relevant.
  • stupid. contemptible and despicable. simple. Take your pick from the dictionary of slang.
  • “global information tracker”: you're in a good mood, and it actually works for you. Angels sing, and a light suddenly fills the room.
  • “goddamn idiotic truckload of sh*t”: when it breaks