From d7e03c61dd1b6fc5a70c55bddf0d1b768baa507c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Re=CC=81gis=20De=CC=81camps?= Date: Wed, 25 Mar 2020 21:18:07 +0100 Subject: [PATCH] Cast long yychar to int. Starting from JFlex 1.8.0, `yychar` is a long. However, it is used to build an antlr `CommonToken` which takes an int. Hence, explicit casting is needed. I propose to cast the `yychar` with `java.lang.Math.toIntExact()` which throw an ArithmeticException in case of overflow. This is acceptable because the previous code would have returned an overflowed (negative) position. See https://github.com/jflex-de/jflex/pull/605 for details. --- smali/src/main/jflex/smaliLexer.jflex | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/smali/src/main/jflex/smaliLexer.jflex b/smali/src/main/jflex/smaliLexer.jflex index 03edc119b..68c83c5c7 100644 --- a/smali/src/main/jflex/smaliLexer.jflex +++ b/smali/src/main/jflex/smaliLexer.jflex @@ -1,5 +1,7 @@ package org.jf.smali; +import static java.lang.Math.toIntExact; + import java.io.*; import java.util.Stack; import org.antlr.runtime.*; @@ -104,9 +106,9 @@ import static org.jf.smali.smaliParser.*; if (hidden) { token.setChannel(Token.HIDDEN_CHANNEL); } - - token.setStartIndex(yychar); - token.setStopIndex(yychar + yylength() - 1); + // yychar is long, but antlr CommonToken only takes an int. + token.setStartIndex(toIntExact(yychar)); + token.setStopIndex(stopIndex()); token.setLine(getLine()); token.setCharPositionInLine(getColumn()); return token; @@ -126,9 +128,9 @@ import static org.jf.smali.smaliParser.*; private Token invalidToken(String message, String text) { InvalidToken token = new InvalidToken(message, text); - - token.setStartIndex(yychar); - token.setStopIndex(yychar + yylength() - 1); + // yychar is long, but antlr CommonToken only takes an int. + token.setStartIndex(toIntExact(yychar)); + token.setStopIndex(stopIndex()); token.setLine(getLine()); token.setCharPositionInLine(getColumn()); @@ -145,7 +147,8 @@ import static org.jf.smali.smaliParser.*; sb.setLength(0); tokenStartLine = getLine(); tokenStartCol = getColumn(); - tokenStartChar = yychar; + // yychar is long, but antlr CommonToken only takes an int. + tokenStartChar = toIntExact(yychar); tokenError = null; } @@ -158,7 +161,7 @@ import static org.jf.smali.smaliParser.*; CommonToken token = new CommonToken(type, sb.toString()); token.setStartIndex(tokenStartChar); - token.setStopIndex(yychar + yylength() - 1); + token.setStopIndex(stopIndex()); token.setLine(tokenStartLine); token.setCharPositionInLine(tokenStartCol); return token; @@ -175,7 +178,7 @@ import static org.jf.smali.smaliParser.*; InvalidToken token = new InvalidToken(message, sb.toString()); token.setStartIndex(tokenStartChar); - token.setStopIndex(yychar + yylength() - 1); + token.setStopIndex(stopIndex()); token.setLine(tokenStartLine); token.setCharPositionInLine(tokenStartCol); return token; @@ -211,6 +214,12 @@ import static org.jf.smali.smaliParser.*; } return processQuotedSimpleName(text); } + + private int stopIndex() { + // jflex yychar is long, but antlr CommonToken only takes an int for + // stopIndex. + return toIntExact(yychar + yylength() - 1); + } %} HexPrefix = 0 [xX]