Skip to content

Chunk array constructors retain input arrays and can change existing patches #242

Open

Description

@parijat1222q

Describe 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, version 4.19-SNAPSHOT.
  • Full mvn -B -ntp package passes across both modules: 173 tests, zero failures/errors, 3 existing skips, zero Checkstyle violations, and Javadocs built.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions