Skip to content

Commit

Permalink
StringIndexOutOfBoundsException in AbstractItem.getUrl (#8481)
Browse files Browse the repository at this point in the history
  • Loading branch information
jglick authored Sep 20, 2023
1 parent 41cd799 commit 2a07b98
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
12 changes: 9 additions & 3 deletions core/src/main/java/hudson/model/AbstractItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -555,9 +555,15 @@ public final String getUrl() {
View view = (View) last.getObject();
if (view.getOwner().getItemGroup() == getParent() && !view.isDefault()) {
// Showing something inside a view, so should use that as the base URL.
String base = last.getUrl().substring(req.getContextPath().length() + 1) + '/';
LOGGER.log(Level.FINER, "using {0}{1} for {2} from {3}", new Object[] {base, shortUrl, this, uri});
return base + shortUrl;
String prefix = req.getContextPath() + "/";
String url = last.getUrl();
if (url.startsWith(prefix)) {
String base = url.substring(prefix.length()) + '/';
LOGGER.log(Level.FINER, "using {0}{1} for {2} from {3} given {4}", new Object[] {base, shortUrl, this, uri, prefix});
return base + shortUrl;
} else {
LOGGER.finer(() -> url + " does not start with " + prefix + " as expected");
}
} else {
LOGGER.log(Level.FINER, "irrelevant {0} for {1} from {2}", new Object[] {view.getViewName(), this, uri});
}
Expand Down
4 changes: 4 additions & 0 deletions test/src/test/java/jenkins/widgets/BuildListTableTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,25 @@

import static org.junit.Assert.assertEquals;

import hudson.model.AbstractItem;
import hudson.model.FreeStyleProject;
import hudson.model.ListView;
import java.net.URI;
import java.net.URL;
import java.util.logging.Level;
import org.htmlunit.html.HtmlAnchor;
import org.htmlunit.html.HtmlPage;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.LoggerRule;
import org.jvnet.hudson.test.MockFolder;

public class BuildListTableTest {

@Rule public JenkinsRule r = new JenkinsRule();
@Rule public LoggerRule logging = new LoggerRule().record(AbstractItem.class, Level.FINER);

@Issue("JENKINS-19310")
@Test public void linksFromFolders() throws Exception {
Expand Down

0 comments on commit 2a07b98

Please sign in to comment.