Skip to content

Commit dc05a48

Browse files
authored
Merge pull request #423 from das7pad/reset-position
Avoid including unrelated content in step log after parse errors
2 parents ecaaefe + 1296b5b commit dc05a48

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

src/main/java/org/jenkinsci/plugins/workflow/log/FileLogStorage.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ private void maybeFlush() {
292292
// Note that NumberFormatException is nonfatal in the case of the overall build log:
293293
// the whole-build HTML output always includes exactly what is in the main log file,
294294
// at worst with some missing or inaccurate startStep/endStep annotations.
295+
pos = -1;
295296
continue;
296297
}
297298
if (pos == -1) {
@@ -317,7 +318,10 @@ private void maybeFlush() {
317318
raf.readFully(data);
318319
buf.write(data);
319320
pos = -1;
320-
} // else some sort of mismatch
321+
} else {
322+
// Some sort of mismatch. Do not emit this section.
323+
pos = -1;
324+
}
321325
}
322326
if (pos != -1 && /* otherwise race condition? */ end > pos) {
323327
// In case the build is ongoing and we are still actively writing content for this step,

src/test/java/org/jenkinsci/plugins/workflow/log/FileLogStorageTest.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828

2929
import hudson.model.TaskListener;
3030
import java.io.File;
31+
import java.nio.charset.StandardCharsets;
32+
import org.apache.commons.io.FileUtils;
3133
import org.junit.Before;
3234
import org.junit.Rule;
3335
import org.junit.Test;
@@ -55,6 +57,48 @@ public class FileLogStorageTest extends LogStorageTestBase {
5557
assertOverallLog(0, lines("stuff"), true);
5658
}
5759

60+
@Test public void corruptIndex() throws Exception {
61+
FileUtils.writeStringToFile(log, "before\n1\nbetween1\n2\nbetween2\n3\nafter", StandardCharsets.UTF_8);
62+
FileUtils.writeStringToFile(new File(log + "-index"), "7 1\n?\n18 2\n20\n29 3\n31", StandardCharsets.UTF_8);
63+
assertStepLog("1", 0, "", false);
64+
assertStepLog("2", 0, "2\n", false);
65+
assertStepLog("3", 0, "3\n", false);
66+
}
67+
68+
@Test public void samePositionInIndex() throws Exception {
69+
FileUtils.writeStringToFile(log, "before\n1\nbetween1\n2\nbetween2\n3\nafter", StandardCharsets.UTF_8);
70+
FileUtils.writeStringToFile(new File(log + "-index"), "7 1\n7\n18 2\n20\n29 3\n31", StandardCharsets.UTF_8);
71+
assertStepLog("1", 0, "", false);
72+
assertStepLog("2", 0, "2\n", false);
73+
assertStepLog("3", 0, "3\n", false);
74+
}
75+
76+
@Test public void decrementedPositionInIndex() throws Exception {
77+
FileUtils.writeStringToFile(log, "before\n1\nbetween1\n2\nbetween2\n3\nafter", StandardCharsets.UTF_8);
78+
FileUtils.writeStringToFile(new File(log + "-index"), "7 1\n0\n18 2\n20\n29 3\n31", StandardCharsets.UTF_8);
79+
assertStepLog("1", 0, "", false);
80+
assertStepLog("2", 0, "2\n", false);
81+
assertStepLog("3", 0, "3\n", false);
82+
}
83+
84+
@Test public void corruptIndexAtEnd() throws Exception {
85+
FileUtils.writeStringToFile(log, "before\n1\nafter", StandardCharsets.UTF_8);
86+
FileUtils.writeStringToFile(new File(log + "-index"), "7 1\n?", StandardCharsets.UTF_8);
87+
assertStepLog("1", 0, "", false);
88+
}
89+
90+
@Test public void samePositionInIndexAtEnd() throws Exception {
91+
FileUtils.writeStringToFile(log, "before\n1\nafter", StandardCharsets.UTF_8);
92+
FileUtils.writeStringToFile(new File(log + "-index"), "7 1\n7", StandardCharsets.UTF_8);
93+
assertStepLog("1", 0, "", false);
94+
}
95+
96+
@Test public void decrementedPositionInIndexAtEnd() throws Exception {
97+
FileUtils.writeStringToFile(log, "before\n1\nafter", StandardCharsets.UTF_8);
98+
FileUtils.writeStringToFile(new File(log + "-index"), "7 1\n0", StandardCharsets.UTF_8);
99+
assertStepLog("1", 0, "", false);
100+
}
101+
58102
@Test public void interruptionDoesNotCloseStream() throws Exception {
59103
LogStorage ls = createStorage();
60104
TaskListener overall = ls.overallListener();

0 commit comments

Comments
 (0)