Skip to content

Commit

Permalink
ICU-21295 Fix for Java version number overflow problem
Browse files Browse the repository at this point in the history
Merging the fix for ICU-21219 (PR unicode-org#1245) to ICU 62.

Internal API VersionInfo.javaVersion() maps Java version number to  4 integer fields. Each field must be up to 255. However, recent OpenJDK 8 update exceed this range.

Luckily, we have only one reference in our code base for checking Java version. CharsetUTF16 uses maxBytePerChar = 4 for Java 5 and older, maxBytePerChar = 2 for newer Java version. Because we no longer support Java 5 runtime, we don't need this conditional check.

We don't have any other uses of VersionInfo.javaVersion(). Java's version range is not what we can control, so I decided to delete the internal use only API completely.
  • Loading branch information
yumaoka committed Nov 10, 2020
1 parent 7c7b8bd commit aaf7ca2
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import com.ibm.icu.text.UTF16;
import com.ibm.icu.text.UnicodeSet;
import com.ibm.icu.util.VersionInfo;

/**
* @author Niti Hantaweepant
Expand Down Expand Up @@ -65,13 +64,10 @@ public CharsetUTF16(String icuCanonicalName, String javaCanonicalName, String[]
this.endianXOR = ENDIAN_XOR_LE;
}

/* UnicodeBig and UnicodeLittle requires maxBytesPerChar set to 4 in Java 5 or less */
if ((VersionInfo.javaVersion().getMajor() == 1 && VersionInfo.javaVersion().getMinor() <= 5)
&& (isEndianSpecified && version == 1)) {
maxBytesPerChar = 4;
} else {
maxBytesPerChar = 2;
}
// UnicodeBig and UnicodeLittle used to require maxBytesPerChar set to 4 in Java 5 or less,
// but it's no longer necessary for newer Java versions. Java 5 or older runtime is no
// longer supported.
maxBytesPerChar = 2;

minBytesPerChar = 2;
maxCharsPerByte = 1;
Expand Down
50 changes: 0 additions & 50 deletions icu4j/main/classes/core/src/com/ibm/icu/util/VersionInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -354,56 +354,6 @@ public static VersionInfo getInstance(int major)
return getInstance(major, 0, 0, 0);
}

private static volatile VersionInfo javaVersion;

/**
* @internal
* @deprecated This API is ICU internal only.
*/
@Deprecated
public static VersionInfo javaVersion() {
if (javaVersion == null) {
synchronized(VersionInfo.class) {
if (javaVersion == null) {
String s = System.getProperty("java.version");
// clean string
// preserve only digits, separated by single '.'
// ignore over 4 digit sequences
// does not test < 255, very odd...

char[] chars = s.toCharArray();
int r = 0, w = 0, count = 0;
boolean numeric = false; // ignore leading non-numerics
while (r < chars.length) {
char c = chars[r++];
if (c < '0' || c > '9') {
if (numeric) {
if (count == 3) {
// only four digit strings allowed
break;
}
numeric = false;
chars[w++] = '.';
++count;
}
} else {
numeric = true;
chars[w++] = c;
}
}
while (w > 0 && chars[w-1] == '.') {
--w;
}

String vs = new String(chars, 0, w);

javaVersion = VersionInfo.getInstance(vs);
}
}
}
return javaVersion;
}

/**
* Returns the String representative of VersionInfo in the format of
* "major.minor.milli.micro"
Expand Down

0 comments on commit aaf7ca2

Please sign in to comment.