Git fixes pathspec overflow with exclude patterns
Negative pathspecs shorter than a shared positive prefix could trigger a heap buffer over-read, and excludes at the front blocked prefix optimization.
Git is fixing a heap buffer over-read in pathspec matching when exclude patterns are mixed with ordinary path limits, along with a related logic error that dropped a useful directory prefix.
Contributor Yannik Tausch reported that Git derives a common directory prefix only from non-exclude pathspecs, then still applied that prefix while matching excludes. If an exclude pattern was shorter than the prefix, matching walked past the end of the pattern string. Under AddressSanitizer the fault showed up as a heap-buffer-overflow; without it, results could depend on whatever followed the pattern in memory. The issue was also raised on the Git security list.
A second bug meant that an exclude listed first caused the common-prefix walk to bail out with length zero even when the remaining positive pathspecs clearly shared a directory, so directory filling lost an optimization it should have kept.
The fix stops applying the shared prefix when matching negative pathspecs, and computes the prefix from the first non-exclude item so both length and source string stay correct. Maintainer Junio C Hamano reviewed the series, requested a conventional output-parameter API instead of returning a small struct by value, and staged a separate const-correctness change for pathspec string fields so unit tests build cleanly. Regression coverage includes a short exclude against a long positive prefix and a unit case with an unrelated exclude ahead of two paths that share a directory.