Skip to content

Commit

Permalink
Check the validity of all mbc created
Browse files Browse the repository at this point in the history
  • Loading branch information
haozhun committed Mar 26, 2015
1 parent 005b574 commit 7719e28
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/org/joni/OptMapInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

import org.jcodings.CaseFoldCodeItem;
import org.jcodings.Encoding;
import org.joni.exception.ErrorMessages;
import org.joni.exception.ValueException;

final class OptMapInfo {

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

byte[] buf = new byte[Config.ENC_CODE_TO_MBC_MAXLEN];
for (int i=0; i<items.length; i++) {
enc.codeToMbc(items[i].code[0], buf, 0);
int len = enc.codeToMbc(items[i].code[0], buf, 0);
if (enc.length(buf, 0, len) < 0) {
throw new ValueException(ErrorMessages.ERR_INVALID_UNICODE);
}
addChar(buf[0], enc);
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/org/joni/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,9 @@ private Node parseExp(TokenType term) {
case CODE_POINT:
byte[]buf = new byte[Config.ENC_CODE_TO_MBC_MAXLEN];
int num = enc.codeToMbc(token.getCode(), buf, 0);
if (enc.length(buf, 0, num) < 0) {
newValueException(ERR_INVALID_UNICODE);
}
// #ifdef NUMBERED_CHAR_IS_NOT_CASE_AMBIG ... // setRaw() #else
node = new StringNode(buf, 0, num);
break;
Expand Down
6 changes: 6 additions & 0 deletions src/org/joni/ast/StringNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import org.jcodings.Encoding;
import org.joni.Config;
import org.joni.constants.StringType;
import org.joni.exception.ErrorMessages;
import org.joni.exception.ValueException;

public final class StringNode extends Node implements StringType {

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

public void catCode(int code, Encoding enc) {
ensure(Config.ENC_CODE_TO_MBC_MAXLEN);
int oldEnd = end;
end += enc.codeToMbc(code, bytes, end);
if (enc.length(bytes, oldEnd, end) < 0) {
throw new ValueException(ErrorMessages.ERR_INVALID_UNICODE);
}
}

public void clear() {
Expand Down
1 change: 1 addition & 0 deletions src/org/joni/exception/ErrorMessages.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,6 @@ public interface ErrorMessages extends org.jcodings.exception.ErrorMessages {
final String ERR_INVALID_COMBINATION_OF_OPTIONS = "invalid combination of options";
final String ERR_OVER_THREAD_PASS_LIMIT_COUNT = "over thread pass limit count";
final String ERR_TOO_BIG_SB_CHAR_VALUE = "too big singlebyte char value";
final String ERR_INVALID_UNICODE = "invalid unicode";

}

0 comments on commit 7719e28

Please sign in to comment.