Skip to content

Commit 940784b

Browse files
pgalbraithcstroe
authored andcommitted
Remove endless loop detection to address linkedin#15.
So the obvious downside is the risk of actually getting into an endless loop but I think the risk is worth it. If the code is solid then this is not possible, so I'd rather address those cases as (if?) they come up, instead causing false failures like the case in this issue.
1 parent e016ac9 commit 940784b

File tree

4 files changed

+36
-62
lines changed

4 files changed

+36
-62
lines changed

src/main/java/com/linkedin/urls/detection/InputTextReader.java

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,6 @@
1414
*/
1515
public class InputTextReader {
1616

17-
/**
18-
* The number of times something can be backtracked is this multiplier times the length of the string.
19-
*/
20-
protected static final int MAX_BACKTRACK_MULTIPLIER = 10;
21-
2217
/**
2318
* The content to read.
2419
*/
@@ -29,16 +24,6 @@ public class InputTextReader {
2924
*/
3025
private int _index = 0;
3126

32-
/**
33-
* Contains the amount of characters that were backtracked. This is used for performance analysis.
34-
*/
35-
private int _backtracked = 0;
36-
37-
/**
38-
* When detecting for exceeding the backtrack limit, make sure the text is at least 20 characters.
39-
*/
40-
private final static int MINIMUM_BACKTRACK_LENGTH = 20;
41-
4227
/**
4328
* Creates a new instance of the InputTextReader using the content to read.
4429
* @param content The content to read.
@@ -102,47 +87,18 @@ public int getPosition() {
10287
return _index;
10388
}
10489

105-
/**
106-
* Gets the total number of characters that were backtracked when reading.
107-
*/
108-
public int getBacktrackedCount() {
109-
return _backtracked;
110-
}
111-
11290
/**
11391
* Moves the index to the specified position.
11492
* @param position The position to set the index to.
11593
*/
11694
public void seek(int position) {
117-
int backtrackLength = Math.max(_index - position, 0);
118-
_backtracked += backtrackLength;
11995
_index = position;
120-
checkBacktrackLoop(backtrackLength);
12196
}
12297

12398
/**
12499
* Goes back a single character.
125100
*/
126101
public void goBack() {
127-
_backtracked++;
128102
_index--;
129-
checkBacktrackLoop(1);
130-
}
131-
132-
private void checkBacktrackLoop(int backtrackLength) {
133-
if (_backtracked > (_content.length * MAX_BACKTRACK_MULTIPLIER)) {
134-
if (backtrackLength < MINIMUM_BACKTRACK_LENGTH) {
135-
backtrackLength = MINIMUM_BACKTRACK_LENGTH;
136-
}
137-
138-
int start = Math.max(_index, 0);
139-
if (start + backtrackLength > _content.length) {
140-
backtrackLength = _content.length - start;
141-
}
142-
143-
String badText = new String(_content, start, backtrackLength);
144-
throw new NegativeArraySizeException("Backtracked max amount of characters. Endless loop detected. Bad Text: '"
145-
+ badText + "'");
146-
}
147103
}
148104
}

src/main/java/com/linkedin/urls/detection/UrlDetector.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -125,15 +125,6 @@ public UrlDetector(String content, UrlDetectorOptions options) {
125125
_options = options;
126126
}
127127

128-
/**
129-
* Gets the number of characters that were backtracked while reading the input. This is useful for performance
130-
* measurement.
131-
* @return The count of characters that were backtracked while reading.
132-
*/
133-
public int getBacktracked() {
134-
return _reader.getBacktrackedCount();
135-
}
136-
137128
/**
138129
* Detects the urls and returns a list of detected url strings.
139130
* @return A list with detected urls.

src/test/java/com/linkedin/urls/detection/TestInputTextReader.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,4 @@ public void testSeek() {
5959
reader.seek(1);
6060
Assert.assertEquals(reader.read(), CONTENT.charAt(1));
6161
}
62-
63-
@Test(expectedExceptions = NegativeArraySizeException.class, expectedExceptionsMessageRegExp = ".*" + CONTENT + ".*")
64-
public void testEndlessLoopDetection() {
65-
InputTextReader reader = new InputTextReader(CONTENT);
66-
for (int i = 0; i < InputTextReader.MAX_BACKTRACK_MULTIPLIER + 1; i++) {
67-
reader.seek(CONTENT.length());
68-
reader.seek(0);
69-
}
70-
}
7162
}

src/test/java/com/linkedin/urls/detection/TestUriDetection.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,42 @@ public void testBacktrackInvalidUsernamePassword() {
649649
runTest("http://hello:asdf.com", UrlDetectorOptions.Default, "asdf.com");
650650
}
651651

652+
/*
653+
* https://github.com/linkedin/URL-Detector/issues/12
654+
*/
655+
@Test
656+
public void testIssue12() {
657+
runTest("http://user:[email protected] host.com", UrlDetectorOptions.Default, "http://user:[email protected]", "host.com");
658+
}
659+
660+
/*
661+
* https://github.com/linkedin/URL-Detector/issues/13
662+
*/
663+
@Test
664+
public void testIssue13() {
665+
runTest("[email protected]/page", UrlDetectorOptions.Default, "[email protected]/page");
666+
runTest("[email protected]", UrlDetectorOptions.Default, "[email protected]");
667+
runTest("[email protected]", UrlDetectorOptions.Default, "[email protected]");
668+
runTest("[email protected]", UrlDetectorOptions.Default, "[email protected]");
669+
runTest("first.middle.reallyreallyreallyreallyreallyreallyreallyreallyreallyreallylonglastname@gmail.com", UrlDetectorOptions.Default, "first.middle.reallyreallyreallyreallyreallyreallyreallyreallyreallyreallylonglastname@gmail.com");
670+
}
671+
672+
/*
673+
* https://github.com/linkedin/URL-Detector/issues/15
674+
*/
675+
@Test
676+
public void testIssue15() {
677+
runTest(".............:::::::::::;;;;;;;;;;;;;;;::...............................................:::::::::::::::::::::::::::::....................", UrlDetectorOptions.Default);
678+
}
679+
680+
/*
681+
* https://github.com/linkedin/URL-Detector/issues/16
682+
*/
683+
@Test
684+
public void testIssue16() {
685+
runTest("://VIVE MARINE LE PEN//:@.", UrlDetectorOptions.Default);
686+
}
687+
652688
private void runTest(String text, UrlDetectorOptions options, String... expected) {
653689
//do the detection
654690
UrlDetector parser = new UrlDetector(text, options);

0 commit comments

Comments
 (0)