Skip to content

Commit

Permalink
Fix "JsonUtil.unescape() IndexOutOfBoundsException for string startin…
Browse files Browse the repository at this point in the history
…g with escape", re #15
  • Loading branch information
safris committed May 21, 2024
1 parent a306eea commit d4bb31e
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/main/java/org/openjax/json/JsonUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ else if (ch == 'f')
else if (ch == 'u') {
++i;
final char[] unicode = new char[4];
for (int j = 0, j$ = unicode.length; j < j$; ++j) // [A]
for (int j = 0; j < 4; ++j) // [A]
unicode[j] = str.charAt(i + j);

i += unicode.length - 1;
Expand Down Expand Up @@ -526,7 +526,10 @@ public static StringBuilder unescape(final StringBuilder out, final CharSequence
for (int i = 0, i$ = str.length(); i < i$; ++i) { // [N]
char ch = str.charAt(i);
if (ch == '\\') {
ch = str.charAt(++i);
if (++i == i$)
break;

ch = str.charAt(i);
if (ch == 'n')
ch = '\n';
else if (ch == 'r')
Expand All @@ -538,12 +541,16 @@ else if (ch == 'b')
else if (ch == 'f')
ch = '\f';
else if (ch == 'u') {
++i;
if (++i == i$)
break;

final char[] unicode = new char[4];
for (int j = 0, j$ = unicode.length; j < j$; ++j) // [A]
for (int j = 0; j < 4; ++j) // [A]
unicode[j] = str.charAt(i + j);

i += unicode.length - 1;
if ((i += unicode.length - 1) >= i$)
break;

ch = (char)Integer.parseInt(new String(unicode), 16);
}
}
Expand Down Expand Up @@ -601,7 +608,7 @@ else if (ch == 'f')
else if (ch == 'u') {
++i;
final char[] unicode = new char[4];
for (int j = 0, j$ = unicode.length; j < j$; ++j) // [A]
for (int j = 0; j < 4; ++j) // [A]
unicode[j] = chars[i + j];

i += unicode.length - 1;
Expand Down

0 comments on commit d4bb31e

Please sign in to comment.