Skip to content

Commit 3d5343c

Browse files
committed
fixes #107 refinement
1 parent d384ab6 commit 3d5343c

File tree

5 files changed

+3866
-5
lines changed

5 files changed

+3866
-5
lines changed

java-diff-utils/src/main/java/com/github/difflib/unifieddiff/UnifiedDiffReader.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ private UnifiedDiff parse() throws IOException, UnifiedDiffParserException {
106106
if (line != null) {
107107
processLine(line, CHUNK);
108108
while ((line = READER.readLine()) != null) {
109+
line = checkForNoNewLineAtTheEndOfTheFile(line);
110+
109111
if (!processLine(line, LINE_NORMAL, LINE_ADD, LINE_DEL)) {
110112
throw new UnifiedDiffParserException("expected data line not found");
111113
}
@@ -118,10 +120,7 @@ private UnifiedDiff parse() throws IOException, UnifiedDiffParserException {
118120
}
119121
line = READER.readLine();
120122

121-
if ("\\ No newline at end of file".equals(line)) {
122-
actualFile.setNoNewLineAtTheEndOfTheFile(true);
123-
line = READER.readLine();
124-
}
123+
line = checkForNoNewLineAtTheEndOfTheFile(line);
125124
}
126125
if (line == null || (line.startsWith("--") && !line.startsWith("---"))) {
127126
break;
@@ -142,6 +141,14 @@ private UnifiedDiff parse() throws IOException, UnifiedDiffParserException {
142141
return data;
143142
}
144143

144+
private String checkForNoNewLineAtTheEndOfTheFile(String line) throws IOException {
145+
if ("\\ No newline at end of file".equals(line)) {
146+
actualFile.setNoNewLineAtTheEndOfTheFile(true);
147+
return READER.readLine();
148+
}
149+
return line;
150+
}
151+
145152
static String[] parseFileNames(String line) {
146153
String[] split = line.split(" ");
147154
return new String[]{

java-diff-utils/src/test/java/com/github/difflib/unifieddiff/UnifiedDiffReaderTest.java

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,12 +277,44 @@ public void testParseIssue107_2() throws IOException {
277277

278278
assertThat(diff.getFiles().size()).isEqualTo(2);
279279

280-
final UnifiedDiffFile file = diff.getFiles().get(0);
281280
UnifiedDiffFile file1 = diff.getFiles().get(0);
282281
assertThat(file1.getFromFile()).isEqualTo("Main.java");
283282
assertThat(file1.getPatch().getDeltas().size()).isEqualTo(1);
284283

285284
}
285+
286+
@Test
287+
public void testParseIssue107_3() throws IOException {
288+
UnifiedDiff diff = UnifiedDiffReader.parseUnifiedDiff(
289+
UnifiedDiffReaderTest.class.getResourceAsStream("problem_diff_issue107_3.diff"));
290+
291+
assertThat(diff.getFiles().size()).isEqualTo(1);
292+
293+
UnifiedDiffFile file1 = diff.getFiles().get(0);
294+
assertThat(file1.getFromFile()).isEqualTo("Billion laughs attack.md");
295+
assertThat(file1.getPatch().getDeltas().size()).isEqualTo(1);
296+
297+
}
298+
299+
@Test
300+
public void testParseIssue107_4() throws IOException {
301+
UnifiedDiff diff = UnifiedDiffReader.parseUnifiedDiff(
302+
UnifiedDiffReaderTest.class.getResourceAsStream("problem_diff_issue107_4.diff"));
303+
304+
assertThat(diff.getFiles().size()).isEqualTo(27);
305+
306+
assertThat(diff.getFiles()).extracting(f -> f.getFromFile()).contains("README.md");
307+
}
308+
309+
@Test
310+
public void testParseIssue107_5() throws IOException {
311+
UnifiedDiff diff = UnifiedDiffReader.parseUnifiedDiff(
312+
UnifiedDiffReaderTest.class.getResourceAsStream("problem_diff_issue107_5.diff"));
313+
314+
assertThat(diff.getFiles().size()).isEqualTo(22);
315+
316+
assertThat(diff.getFiles()).extracting(f -> f.getFromFile()).contains("rt/management/src/test/java/org/apache/cxf/management/jmx/MBServerConnectorFactoryTest.java");
317+
}
286318

287319
@Test
288320
public void testParseIssue110() throws IOException {
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
diff -r 4d1de0a006b7 -r ace1482360cb Billion laughs attack.md
2+
--- a/Billion laughs attack.md Mon Feb 24 13:37:17 2020 +0000
3+
+++ b/Billion laughs attack.md Mon Feb 24 14:22:50 2020 +0000
4+
@@ -11,14 +11,12 @@
5+
6+
*The problem is*: when you stay with the mouth open and eyes closed then they may throw trash and you get sick.
7+
8+
-
9+
-SnakeYAML Engine [has a way to restrict the amount of aliases for collections](https://bitbucket.org/asomov/snakeyaml-engine/src/default/src/test/java/org/snakeyaml/engine/usecases/references/ReferencesTest.java) to fail early without allocation too much resources.
10+
+SnakeYAML 1.26+ [has a way to restrict the amount of aliases for collections](https://bitbucket.org/asomov/snakeyaml/src/default/src/test/java/org/yaml/snakeyaml/issues/issue377/ReferencesTest.java) to fail early without allocation too much resources.
11+
12+
# Solution #
13+
14+
-1. If the YAML is not coming from untrusted source (it is merely a configuration file) then it is a false positive. Just ignore it. The quality of NVD database is very low and contains tons of issues which appear to be false positives.
15+
-2. Migrate to [SnakeYAML Engine](https://bitbucket.org/asomov/snakeyaml-engine/src/default/). It has a configuration option to restrict aliases for collections (the aliases for scalars cannot grow and they are not restricted)
16+
-3. Check how it is done in SnakeYAML Engine and build your own SnakeYAML version with the same change.
17+
-4. Read the YAML and check its quality before giving the document to SnakeYAML (count `*` and `&` for instance)
18+
+1. Update to SnakeYAML 1.26. It has a configuration option to restrict aliases for collections (the aliases for scalars cannot grow and they are not restricted)
19+
+2. If the YAML is not coming from untrusted source (it is merely a configuration file) then it is a false positive. Just ignore it. The quality of NVD database is very low and contains tons of issues which appear to be false positives.
20+
+3. Read the YAML and check its quality before giving the document to SnakeYAML (count `*` and `&` for instance)
21+
22+
-Enjoy.
23+
\ No newline at end of file
24+
+Enjoy.

0 commit comments

Comments
 (0)