Chunk array constructors retain input arrays and can change existing patches · Issue #242 · java-diff-utils/java-diff-utils
Repository navigation
Chunk array constructors retain input arrays and can change existing patches #242
New issue
New issue
Open
#243
Open
Chunk array constructors retain input arrays and can change existing patches #242 #243
Description
@parijat1222q's profile parijat1222q opened on Nov 10, 2026Describe the bug
The Chunk array constructors say they save a copy of the affected lines, but the three-argument constructor stores Arrays.asList(lines). That list is backed by the caller's array. The two-argument array constructor delegates to it, so both retain the input.
Changing the source array after constructing a patch makes the patch fail its source check. Changing only the revised array silently changes what the patch writes. The List constructors already make a defensive copy.
To reproduce
String[] revised = {"new"};
Patch<String> patch = new Patch<>();
patch.addDelta(new ChangeDelta<>(
new Chunk<>(0, new String[] {"old"}), new Chunk<>(0, revised)));
revised[0] = "changed revision";
System.out.println(patch.applyTo(Arrays.asList("old")));
Actual output: [changed revision].
If the source array is changed instead, applyTo throws PatchFailedException: CONTENT_DOES_NOT_MATCH_TARGET even when given the original source text.
Expected behavior
The patch should retain the line values supplied when the chunks were constructed. The example should return [new], consistently with the constructor Javadoc and the List overloads.
A one-line defensive copy fixes both array overloads. Three regression tests cover source-array mutation, the overload with change positions, and revised-array mutation. On unchanged source the tests produce two patch-application errors and one wrong-output assertion; all pass with the copy.
System
- Linux, Temurin 17.0.21.
- Current master
07a374624840a238e0c4e97dc94a30e0ac92ce28, version4.19-SNAPSHOT. - Full
mvn -B -ntp packagepasses across both modules: 173 tests, zero failures/errors, 3 existing skips, zero Checkstyle violations, and Javadocs built.