Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #1591 from vb-linetco/patch-1
[FIXED JENKINS-27441] avoid IOOBE in getLog when called with 0
  • Loading branch information
oleg-nenashev committed May 4, 2015
2 parents 43188ec + 75f5810 commit 3bb39bb
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
3 changes: 3 additions & 0 deletions core/src/main/java/hudson/model/Run.java
Expand Up @@ -1933,6 +1933,9 @@ public synchronized void save() throws IOException {
public @Nonnull List<String> getLog(int maxLines) throws IOException {
int lineCount = 0;
List<String> logLines = new LinkedList<String>();
if (maxLines == 0) {
return logLines;
}
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(getLogFile()),getCharset()));
try {
for (String line = reader.readLine(); line != null; line = reader.readLine()) {
Expand Down
26 changes: 26 additions & 0 deletions core/src/test/java/hudson/model/RunTest.java
Expand Up @@ -25,18 +25,27 @@
package hudson.model;

import java.io.IOException;
import hudson.model.Run.Artifact;
import java.io.File;
import java.io.PrintWriter;
import java.util.List;
import java.util.TimeZone;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import static org.junit.Assert.*;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.jvnet.hudson.test.Issue;
import org.mockito.Mockito;


public class RunTest {

@Rule public TemporaryFolder tmp = new TemporaryFolder();

@Issue("JENKINS-15816")
@SuppressWarnings({"unchecked", "rawtypes"})
@Test public void timezoneOfID() throws Exception {
Expand Down Expand Up @@ -128,4 +137,21 @@ public void getDurationString() throws IOException {
msg = r.getDurationString();
assertFalse(msg, msg.endsWith(" and counting"));
}

@Issue("JENKINS-27441")
@Test
public void getLogReturnsAnEmptyListWhenCalledWith0() throws Exception {
Job j = Mockito.mock(Job.class);
File tempBuildDir = tmp.newFolder();
Mockito.when(j.getBuildDir()).thenReturn(tempBuildDir);
Run r = new Run(j, 0) {};
File f = r.getLogFile();
f.getParentFile().mkdirs();
PrintWriter w = new PrintWriter(f, "utf-8");
w.println("dummy");
w.close();
List<String> logLines = r.getLog(0);
assertTrue(logLines.isEmpty());
}

}

0 comments on commit 3bb39bb

Please sign in to comment.