diff --git a/src/org/joni/Lexer.java b/src/org/joni/Lexer.java index 3ac3338..daa3c8e 100644 --- a/src/org/joni/Lexer.java +++ b/src/org/joni/Lexer.java @@ -24,6 +24,7 @@ import static org.joni.Option.isWordBoundAllRange; import static org.joni.ast.QuantifierNode.isRepeatInfinite; +import org.jcodings.Encoding; import org.jcodings.Ptr; import org.jcodings.constants.CharacterType; import org.jcodings.exception.CharacterPropertyException; @@ -1174,7 +1175,12 @@ protected final void fetchToken() { token.type = TokenType.CODE_POINT; token.setCode(c); } else { /* string */ - p = token.backP + enc.length(bytes, token.backP, stop); + int encLength = enc.length(bytes, token.backP, stop); + /* Check and ensure the encoded character is valid */ + if (encLength == Encoding.CHAR_INVALID) { + throw new IllegalArgumentException("Invalid character found."); + } + p = token.backP + encLength; } break; } // switch (c)