Skip to content

Commit b1fa5b2

Browse files
committed
Check the validity of all mbc created
1 parent a0910bb commit b1fa5b2

File tree

4 files changed

+16
-1
lines changed

4 files changed

+16
-1
lines changed

src/org/joni/OptMapInfo.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
import org.jcodings.CaseFoldCodeItem;
2323
import org.jcodings.Encoding;
24+
import org.joni.exception.ErrorMessages;
25+
import org.joni.exception.ValueException;
2426

2527
final class OptMapInfo {
2628

@@ -61,7 +63,10 @@ void addCharAmb(byte[]bytes, int p, int end, Encoding enc, int caseFoldFlag) {
6163

6264
byte[] buf = new byte[Config.ENC_CODE_TO_MBC_MAXLEN];
6365
for (int i=0; i<items.length; i++) {
64-
enc.codeToMbc(items[i].code[0], buf, 0);
66+
int len = enc.codeToMbc(items[i].code[0], buf, 0);
67+
if (enc.length(buf, 0, len) < 0) {
68+
throw new ValueException(ErrorMessages.ERR_INVALID_UNICODE);
69+
}
6570
addChar(buf[0], enc);
6671
}
6772
}

src/org/joni/Parser.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -741,6 +741,9 @@ private Node parseExp(TokenType term) {
741741
case CODE_POINT:
742742
byte[]buf = new byte[Config.ENC_CODE_TO_MBC_MAXLEN];
743743
int num = enc.codeToMbc(token.getCode(), buf, 0);
744+
if (enc.length(buf, 0, num) < 0) {
745+
newValueException(ERR_INVALID_UNICODE);
746+
}
744747
// #ifdef NUMBERED_CHAR_IS_NOT_CASE_AMBIG ... // setRaw() #else
745748
node = new StringNode(buf, 0, num);
746749
break;

src/org/joni/ast/StringNode.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import org.jcodings.Encoding;
2323
import org.joni.Config;
2424
import org.joni.constants.StringType;
25+
import org.joni.exception.ErrorMessages;
26+
import org.joni.exception.ValueException;
2527

2628
public final class StringNode extends Node implements StringType {
2729

@@ -154,7 +156,11 @@ public void cat(byte c) {
154156

155157
public void catCode(int code, Encoding enc) {
156158
ensure(Config.ENC_CODE_TO_MBC_MAXLEN);
159+
int oldEnd = end;
157160
end += enc.codeToMbc(code, bytes, end);
161+
if (enc.length(bytes, oldEnd, end) < 0) {
162+
throw new ValueException(ErrorMessages.ERR_INVALID_UNICODE);
163+
}
158164
}
159165

160166
public void clear() {

src/org/joni/exception/ErrorMessages.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,5 +90,6 @@ public interface ErrorMessages extends org.jcodings.exception.ErrorMessages {
9090
final String ERR_INVALID_COMBINATION_OF_OPTIONS = "invalid combination of options";
9191
final String ERR_OVER_THREAD_PASS_LIMIT_COUNT = "over thread pass limit count";
9292
final String ERR_TOO_BIG_SB_CHAR_VALUE = "too big singlebyte char value";
93+
final String ERR_INVALID_UNICODE = "invalid unicode";
9394

9495
}

0 commit comments

Comments
 (0)