Skip to content

Commit

Permalink
[JENKINS-71139] Do not even try to save NULs (#521)
Browse files Browse the repository at this point in the history
  • Loading branch information
jglick authored Apr 24, 2023
1 parent fe62d9e commit e38dbd1
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
12 changes: 8 additions & 4 deletions src/main/java/hudson/tasks/junit/CaseResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ public CaseResult(
this.errorStackTrace = stacktrace;
this.errorDetails = errorDetails;
this.parent = parent;
this.stdout = stdout;
this.stderr = stderr;
this.stdout = fixNULs(stdout);
this.stderr = fixNULs(stderr);
this.duration = duration;

this.skipped = skippedMessage != null;
Expand Down Expand Up @@ -189,8 +189,8 @@ public CaseResult(
skippedMessage = getSkippedMessage(testCase);
@SuppressWarnings("LeakingThisInConstructor")
Collection<CaseResult> _this = Collections.singleton(this);
stdout = possiblyTrimStdio(_this, keepLongStdio, testCase.elementText("system-out"));
stderr = possiblyTrimStdio(_this, keepLongStdio, testCase.elementText("system-err"));
stdout = fixNULs(possiblyTrimStdio(_this, keepLongStdio, testCase.elementText("system-out")));
stderr = fixNULs(possiblyTrimStdio(_this, keepLongStdio, testCase.elementText("system-err")));
}

static String possiblyTrimStdio(Collection<CaseResult> results, boolean keepLongStdio, String stdio) { // HUDSON-6516
Expand All @@ -209,6 +209,10 @@ static String possiblyTrimStdio(Collection<CaseResult> results, boolean keepLong
return stdio.subSequence(0, halfMaxSize) + "\n...[truncated " + middle + " chars]...\n" + stdio.subSequence(len - halfMaxSize, len);
}

static String fixNULs(String stdio) { // JENKINS-71139
return stdio == null ? null : stdio.replace("\u0000", "^@");
}

/**
* Flavor of {@link #possiblyTrimStdio(Collection, boolean, String)} that doesn't try to read the whole thing into memory.
*/
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/hudson/tasks/junit/SuiteResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ public final class SuiteResult implements Serializable {
*/
public SuiteResult(String name, String stdout, String stderr, @CheckForNull PipelineTestDetails pipelineTestDetails) {
this.name = name;
this.stderr = stderr;
this.stdout = stdout;
this.stderr = CaseResult.fixNULs(stderr);
this.stdout = CaseResult.fixNULs(stdout);
// runId is generally going to be not null, but we only care about it if both it and nodeId are not null.
if (pipelineTestDetails != null && pipelineTestDetails.getNodeId() != null) {
this.nodeId = pipelineTestDetails.getNodeId();
Expand Down Expand Up @@ -283,8 +283,8 @@ private SuiteResult(File xmlReport, Element suite, boolean keepLongStdio, @Check
}
}

this.stdout = stdout;
this.stderr = stderr;
this.stdout = CaseResult.fixNULs(stdout);
this.stderr = CaseResult.fixNULs(stderr);
}

public void addCase(CaseResult cr) {
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/hudson/tasks/junit/SuiteResultTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ public void stdioNull() throws Exception {
System.out.println();
sr = (SuiteResult) f.read();
System.out.println(TestResultAction.XSTREAM.toXML(sr));
assertEquals("foo\u0000bar", sr.getStdout());
assertEquals("foo^@bar", sr.getStdout());
}

/**
Expand Down

0 comments on commit e38dbd1

Please sign in to comment.