Skip to content
Merged
Show file tree
Hide file tree
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
9 changes: 5 additions & 4 deletions src/org/joni/ast/StringNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public StringNode() {

public static StringNode fromCodePoint(int code, Encoding enc) {
StringNode str = new StringNode(Config.ENC_CODE_TO_MBC_MAXLEN);
str.end = enc.codeToMbc(code, str.bytes, str.p);
str.catCode(code, enc);
return str;
}

Expand Down Expand Up @@ -141,13 +141,14 @@ public void catByte(byte c) {
public void catCode(int code, Encoding enc) {
modifyEnsure(Config.ENC_CODE_TO_MBC_MAXLEN);
int oldEnd = end;
end += enc.codeToMbc(code, bytes, end);
if (enc.length(bytes, oldEnd, end) < 0) {
int length = enc.codeToMbc(code, bytes, end);
if (length <= 0 || enc.length(bytes, oldEnd, oldEnd + length) <= 0) {
throw new ValueException(ErrorMessages.ERR_INVALID_UNICODE, code <= 0xFFFF
? String.format("\\\\u%04X", code)
: String.format("\\\\u%04X\\\\u%04X", Character.highSurrogate(code), Character.lowSurrogate(code))
: String.format("\\\\u%04X\\\\u%04X", (int) Character.highSurrogate(code), (int) Character.lowSurrogate(code))
);
}
end += length;
}

public void setRaw() {
Expand Down
92 changes: 92 additions & 0 deletions test/org/joni/test/TestInvalidUnicodeEncoding.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.joni.test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.UnsupportedEncodingException;

import org.jcodings.Encoding;
import org.jcodings.specific.UTF16BEEncoding;
import org.jcodings.specific.UTF16LEEncoding;
import org.jcodings.specific.UTF32BEEncoding;
import org.jcodings.specific.UTF32LEEncoding;
import org.joni.Matcher;
import org.joni.Option;
import org.joni.Regex;
import org.joni.Syntax;
import org.joni.exception.JOniException;
import org.junit.Test;

public class TestInvalidUnicodeEncoding {
private static final Encoding[] ENCODINGS = {
UTF16LEEncoding.INSTANCE,
UTF16BEEncoding.INSTANCE,
UTF32LEEncoding.INSTANCE,
UTF32BEEncoding.INSTANCE
};

private static final String[] CHARSETS = {
"UTF-16LE",
"UTF-16BE",
"UTF-32LE",
"UTF-32BE"
};

@Test
public void invalidEscapedCodepointsAreRejected() throws Exception {
String[] invalidPatterns = {
"\\x{D801}",
"a\\x{D801}",
"a\\x{110000}",
"[\\x{D801}]",
"\\x{110000}"
};

for (int i = 0; i < ENCODINGS.length; i++) {
for (String source : invalidPatterns) {
assertInvalidPattern(source, CHARSETS[i], ENCODINGS[i]);
}
assertValidSupplementaryCodepoint(CHARSETS[i], ENCODINGS[i]);
}
}

private static void assertInvalidPattern(String source, String charset, Encoding encoding)
throws UnsupportedEncodingException {
byte[] pattern = source.getBytes(charset);
try {
new Regex(pattern, 0, pattern.length, Option.DEFAULT, encoding, Syntax.RUBY);
fail("Expected invalid Unicode code point to be rejected in " + charset + ": " + source);
} catch (JOniException expected) {
assertTrue(expected.getMessage(), expected.getMessage().startsWith("invalid unicode:"));
}
}

private static void assertValidSupplementaryCodepoint(String charset, Encoding encoding)
throws Exception {
byte[] pattern = "\\x{1F600}".getBytes(charset);
byte[] subject = "\uD83D\uDE00".getBytes(charset);
Regex regex = new Regex(pattern, 0, pattern.length, Option.DEFAULT, encoding, Syntax.RUBY);
Matcher matcher = regex.matcher(subject, 0, subject.length);
assertEquals(0, matcher.search(0, subject.length, Option.DEFAULT));
}
}
Loading