apache / apache/lucene

RegExp.parseRepeatExp is not fully checked.

Open
#12,251 0 comments 0 reactions 0 assignees View on GitHub
type:bug
Dominant language
Java
Stars
3.6k
Forks
1.4k
Avg merge
2d 11h
Merged PRs (30d)
88

Description

### Description

parseRepeatExp has the following code:

final RegExp parseRepeatExp() throws IllegalArgumentException {
..
if (start == pos) throw new IllegalArgumentException("integer expected at position " + pos);
if (start != pos) m = Integer.parseInt(originalString.substring(start, pos));
...
}
Integer.parseInt(originalString.substring(start, pos)) can throw NumberFormatException, but not IllegalArgumentException. Please note that the above statement throws IllegalArgumentException.

Indeed, in the same class, another method rethrow IllegalArgumentException as follows:

final RegExp parseSimpleExp() throws IllegalArgumentException {
...
try {
if (i == 0 || i == s.length() - 1 || i != s.lastIndexOf('-'))
throw new NumberFormatException();
String smin = s.substring(0, i);
String smax = s.substring(i + 1, s.length());
int imin = Integer.parseInt(smin);
int imax = Integer.parseInt(smax);
int digits;
if (smin.length() == smax.length()) digits = smin.length();
else digits = 0;
if (imin > imax) {
int t = imin;
imin = imax;
imax = t;
}
return makeInterval(flags, imin, imax, digits);
} catch (NumberFormatException e) {
throw new IllegalArgumentException("interval syntax error at position " + (pos - 1), e);
}
}

It can be better for parseRepeatExp to rethrow IllegalArgumentException. Can this problem be fixed?

### Version and environment details

_No response_

Contributor guide

Open the contributing guide

Research direction

Start at RegExp.parseRepeatExp and compare its integer parsing with the rethrow behavior shown for parseSimpleExp. Confirm the expected exception type and message for an invalid repeat expression, then add coverage for that case and run the relevant Lucene tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
search
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
50/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.