Skip to content

Fix StackOverflow in variable parsing #7809

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions src/main/java/ch/njol/skript/variables/FlatFileStorage.java
Original file line number Diff line number Diff line change
@@ -530,8 +530,17 @@ static byte[] decode(String hex) {

/**
* A regex pattern of a line in a CSV file.
* <ul>
* <li>{@code (?<=^|,)}: assert that the match is preceded by the start of the line or a comma</li>
* <li>{@code (?:([^",]*)|"((?:[^"]+|"")*)")}: match either a quoted or unquoted value</li>
* <ul>
* <li>- {@code ([^",]*)}: match an unquoted value</li>
* <li>- {@code "((?:[^"]+|"")*)"}: match a quoted value</li>
* </ul>
* <li>{@code (?:,|$)}: match either a comma or the end of the line</li>
* </ul>
*/
private static final Pattern CSV_LINE_PATTERN = Pattern.compile("(?<=^|,)\\s*([^\",]*|\"([^\"]|\"\")*\")\\s*(,|$)");
private static final Pattern CSV_LINE_PATTERN = Pattern.compile("(?<=^|,)\\s*(?:([^\",]*)|\"((?:[^\"]+|\"\")*)\")\\s*(?:,|$)");

/**
* Splits the given CSV line into its values.
@@ -550,14 +559,15 @@ static String[] splitCSV(String line) {

while (matcher.find()) {
if (lastEnd != matcher.start())
return null; // other stuff inbetween finds

String value = matcher.group(1);
if (value.startsWith("\""))
// Unescape value
result.add(value.substring(1, value.length() - 1).replace("\"\"", "\""));
else
result.add(value.trim());
return null; // other stuff in between finds

if (matcher.group(1) != null) {
// unquoted, leave as is
result.add(matcher.group(1).trim());
} else {
// quoted, remove quotes
result.add(matcher.group(2).replace("\"\"", "\""));
}

lastEnd = matcher.end();
}