Merge pull request #2331 from eclipse/jetty-9.4.x-2284-saferVersionParse

Safer simpler version parsing #2284
This commit is contained in:
Joakim Erdfelt 2018-03-15 04:44:04 -05:00 committed by GitHub
commit 3f33013fb3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 103 deletions

View File

@ -34,23 +34,20 @@ public class JavaVersion
public static final String JAVA_TARGET_PLATFORM = "org.eclipse.jetty.javaTargetPlatform"; public static final String JAVA_TARGET_PLATFORM = "org.eclipse.jetty.javaTargetPlatform";
/** Regex for Java version numbers */ /** Regex for Java version numbers */
private static final String VNUM = "(?<VNUM>[1-9][0-9]*(?:(?:\\.0)*\\.[0-9]+)*)"; private static final String VSTR_FORMAT = "(?<VNUM>[1-9][0-9]*(?:(?:\\.0)*\\.[0-9]+)*).*";
private static final String UPDATE = "(?:(?<UNDERSCORE>_)(?<UPDATE>[0-9]+))?";
private static final String PRE = "(?:-(?<PRE>[a-zA-Z0-9]+))?";
private static final String BUILD = "(?:(?<PLUS>\\+)(?<BUILD>[0-9]+))?";
private static final String OPT = "(?:-(?<OPT>[-a-zA-Z0-9.~]+))?";
private static final String VSTR_FORMAT = VNUM + UPDATE + PRE + BUILD + OPT;
static final Pattern VSTR_PATTERN = Pattern.compile(VSTR_FORMAT); static final Pattern VSTR_PATTERN = Pattern.compile(VSTR_FORMAT);
public static final JavaVersion VERSION = parse(System.getProperty("java.runtime.version",System.getProperty("java.version"))); public static final JavaVersion VERSION = parse(System.getProperty("java.runtime.version",System.getProperty("java.version","1.8")));
public static JavaVersion parse(String v) public static JavaVersion parse(String v)
{ {
Matcher m = VSTR_PATTERN.matcher(v); Matcher m = VSTR_PATTERN.matcher(v);
if (!m.matches()) if (!m.matches() || m.group("VNUM")==null)
throw new IllegalArgumentException("Invalid version string: '" + v + "'"); {
System.err.println("ERROR: Invalid version string: '" + v + "'");
return new JavaVersion(v+"-UNKNOWN",8,1,8,0);
}
// $VNUM is a dot-separated list of integers of arbitrary length // $VNUM is a dot-separated list of integers of arbitrary length
String[] split = m.group("VNUM").split("\\."); String[] split = m.group("VNUM").split("\\.");
@ -58,72 +55,12 @@ public class JavaVersion
for (int i = 0; i < split.length; i++) for (int i = 0; i < split.length; i++)
version[i] = Integer.parseInt(split[i]); version[i] = Integer.parseInt(split[i]);
if (m.group("UNDERSCORE")!=null)
{
return new JavaVersion( return new JavaVersion(
v, v,
(version[0]>=9 || version.length==1)?version[0]:version[1], (version[0]>=9 || version.length==1)?version[0]:version[1],
version[0], version[0],
version.length>1?version[1]:0, version.length>1?version[1]:0,
version.length>2?version[2]:0, version.length>2?version[2]:0);
Integer.parseInt(m.group("UPDATE")),
suffix(version,m.group("PRE"),m.group("OPT"))
);
}
if (m.group("PLUS")!=null)
{
return new JavaVersion(
v,
(version[0]>=9 || version.length==1)?version[0]:version[1],
version[0],
version.length>1?version[1]:0,
version.length>2?version[2]:0,
Integer.parseInt(m.group("BUILD")),
suffix(version,m.group("PRE"),m.group("OPT"))
);
}
return new JavaVersion(
v,
(version[0]>=9 || version.length==1)?version[0]:version[1],
version[0],
version.length>1?version[1]:0,
version.length>2?version[2]:0,
0,
suffix(version,m.group("PRE"),m.group("OPT"))
);
}
private static String suffix(int[] version, String pre, String opt)
{
StringBuilder buf = new StringBuilder();
for (int i=3;i<version.length;i++)
{
if (i>3)
buf.append(".");
buf.append(version[i]);
}
if (pre!=null)
{
if (buf.length()>0)
buf.append('-');
buf.append(pre);
}
if (opt!=null)
{
if (buf.length()>0)
buf.append('-');
buf.append(opt);
}
if (buf.length()==0)
return null;
return buf.toString();
} }
private final String version; private final String version;
@ -131,18 +68,14 @@ public class JavaVersion
private final int major; private final int major;
private final int minor; private final int minor;
private final int micro; private final int micro;
private final int update;
private final String suffix;
private JavaVersion(String version, int platform, int major, int minor, int micro, int update, String suffix) private JavaVersion(String version, int platform, int major, int minor, int micro)
{ {
this.version = version; this.version = version;
this.platform = platform; this.platform = platform;
this.major = major; this.major = major;
this.minor = minor; this.minor = minor;
this.micro = micro; this.micro = micro;
this.update = update;
this.suffix = suffix;
} }
/** /**
@ -198,9 +131,10 @@ public class JavaVersion
* *
* @return the update number version * @return the update number version
*/ */
@Deprecated
public int getUpdate() public int getUpdate()
{ {
return update; return 0;
} }
/** /**
@ -209,9 +143,10 @@ public class JavaVersion
* *
* @return the remaining string after the version numbers * @return the remaining string after the version numbers
*/ */
@Deprecated
public String getSuffix() public String getSuffix()
{ {
return suffix; return null;
} }
@Override @Override

View File

@ -19,7 +19,6 @@
package org.eclipse.jetty.util; package org.eclipse.jetty.util;
import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
import org.junit.Test; import org.junit.Test;
@ -33,132 +32,120 @@ public class JavaVersionTest
public void test9() public void test9()
{ {
JavaVersion version = JavaVersion.parse("9.0.1"); JavaVersion version = JavaVersion.parse("9.0.1");
assertThat(version.toString(),is("9.0.1"));
assertThat(version.getPlatform(),is(9)); assertThat(version.getPlatform(),is(9));
assertThat(version.getMajor(),is(9)); assertThat(version.getMajor(),is(9));
assertThat(version.getMinor(),is(0)); assertThat(version.getMinor(),is(0));
assertThat(version.getMicro(),is(1)); assertThat(version.getMicro(),is(1));
assertThat(version.getUpdate(),is(0));
assertThat(version.getSuffix(),nullValue());
} }
@Test @Test
public void test9nano() public void test9nano()
{ {
JavaVersion version = JavaVersion.parse("9.0.1.3"); JavaVersion version = JavaVersion.parse("9.0.1.3");
assertThat(version.toString(),is("9.0.1.3"));
assertThat(version.getPlatform(),is(9)); assertThat(version.getPlatform(),is(9));
assertThat(version.getMajor(),is(9)); assertThat(version.getMajor(),is(9));
assertThat(version.getMinor(),is(0)); assertThat(version.getMinor(),is(0));
assertThat(version.getMicro(),is(1)); assertThat(version.getMicro(),is(1));
assertThat(version.getUpdate(),is(0));
assertThat(version.getSuffix(),is("3"));
} }
@Test @Test
public void test9build() public void test9build()
{ {
JavaVersion version = JavaVersion.parse("9.0.1+11"); JavaVersion version = JavaVersion.parse("9.0.1+11");
assertThat(version.toString(),is("9.0.1+11"));
assertThat(version.getPlatform(),is(9)); assertThat(version.getPlatform(),is(9));
assertThat(version.getMajor(),is(9)); assertThat(version.getMajor(),is(9));
assertThat(version.getMinor(),is(0)); assertThat(version.getMinor(),is(0));
assertThat(version.getMicro(),is(1)); assertThat(version.getMicro(),is(1));
assertThat(version.getUpdate(),is(11));
assertThat(version.getSuffix(),nullValue());
} }
@Test @Test
public void test9all() public void test9all()
{ {
JavaVersion version = JavaVersion.parse("9.0.1-ea+11-b01"); JavaVersion version = JavaVersion.parse("9.0.1-ea+11-b01");
assertThat(version.toString(),is("9.0.1-ea+11-b01"));
assertThat(version.getPlatform(),is(9)); assertThat(version.getPlatform(),is(9));
assertThat(version.getMajor(),is(9)); assertThat(version.getMajor(),is(9));
assertThat(version.getMinor(),is(0)); assertThat(version.getMinor(),is(0));
assertThat(version.getMicro(),is(1)); assertThat(version.getMicro(),is(1));
assertThat(version.getUpdate(),is(11));
assertThat(version.getSuffix(),is("ea-b01"));
} }
@Test @Test
public void test9yuck() public void test9yuck()
{ {
JavaVersion version = JavaVersion.parse("9.0.1.2.3-ea+11-b01"); JavaVersion version = JavaVersion.parse("9.0.1.2.3-ea+11-b01");
assertThat(version.toString(),is("9.0.1.2.3-ea+11-b01"));
assertThat(version.getPlatform(),is(9)); assertThat(version.getPlatform(),is(9));
assertThat(version.getMajor(),is(9)); assertThat(version.getMajor(),is(9));
assertThat(version.getMinor(),is(0)); assertThat(version.getMinor(),is(0));
assertThat(version.getMicro(),is(1)); assertThat(version.getMicro(),is(1));
assertThat(version.getUpdate(),is(11));
assertThat(version.getSuffix(),is("2.3-ea-b01"));
} }
@Test @Test
public void test10ea() public void test10ea()
{ {
JavaVersion version = JavaVersion.parse("10-ea"); JavaVersion version = JavaVersion.parse("10-ea");
assertThat(version.toString(),is("10-ea"));
assertThat(version.getPlatform(),is(10)); assertThat(version.getPlatform(),is(10));
assertThat(version.getMajor(),is(10)); assertThat(version.getMajor(),is(10));
assertThat(version.getMinor(),is(0)); assertThat(version.getMinor(),is(0));
assertThat(version.getMicro(),is(0)); assertThat(version.getMicro(),is(0));
assertThat(version.getUpdate(),is(0));
assertThat(version.getSuffix(),is("ea"));
} }
@Test @Test
public void test8() public void test8()
{ {
JavaVersion version = JavaVersion.parse("1.8.0_152"); JavaVersion version = JavaVersion.parse("1.8.0_152");
assertThat(version.toString(),is("1.8.0_152"));
assertThat(version.getPlatform(),is(8)); assertThat(version.getPlatform(),is(8));
assertThat(version.getMajor(),is(1)); assertThat(version.getMajor(),is(1));
assertThat(version.getMinor(),is(8)); assertThat(version.getMinor(),is(8));
assertThat(version.getMicro(),is(0)); assertThat(version.getMicro(),is(0));
assertThat(version.getUpdate(),is(152));
assertThat(version.getSuffix(),nullValue());
} }
@Test @Test
public void test8ea() public void test8ea()
{ {
JavaVersion version = JavaVersion.parse("1.8.1_03-ea"); JavaVersion version = JavaVersion.parse("1.8.1_03-ea");
assertThat(version.toString(),is("1.8.1_03-ea"));
assertThat(version.getPlatform(),is(8)); assertThat(version.getPlatform(),is(8));
assertThat(version.getMajor(),is(1)); assertThat(version.getMajor(),is(1));
assertThat(version.getMinor(),is(8)); assertThat(version.getMinor(),is(8));
assertThat(version.getMicro(),is(1)); assertThat(version.getMicro(),is(1));
assertThat(version.getUpdate(),is(3));
assertThat(version.getSuffix(),is("ea"));
} }
@Test @Test
public void test3eaBuild() public void test3eaBuild()
{ {
JavaVersion version = JavaVersion.parse("1.3.1_05-ea-b01"); JavaVersion version = JavaVersion.parse("1.3.1_05-ea-b01");
assertThat(version.toString(),is("1.3.1_05-ea-b01"));
assertThat(version.getPlatform(),is(3)); assertThat(version.getPlatform(),is(3));
assertThat(version.getMajor(),is(1)); assertThat(version.getMajor(),is(1));
assertThat(version.getMinor(),is(3)); assertThat(version.getMinor(),is(3));
assertThat(version.getMicro(),is(1)); assertThat(version.getMicro(),is(1));
assertThat(version.getUpdate(),is(5));
assertThat(version.getSuffix(),is("ea-b01"));
} }
@Test @Test
public void testUbuntu() public void testUbuntu()
{ {
JavaVersion version = JavaVersion.parse("9-Ubuntu+0-9b181-4"); JavaVersion version = JavaVersion.parse("9-Ubuntu+0-9b181-4");
assertThat(version.toString(),is("9-Ubuntu+0-9b181-4"));
assertThat(version.getPlatform(),is(9)); assertThat(version.getPlatform(),is(9));
assertThat(version.getMajor(),is(9)); assertThat(version.getMajor(),is(9));
assertThat(version.getMinor(),is(0)); assertThat(version.getMinor(),is(0));
assertThat(version.getMicro(),is(0)); assertThat(version.getMicro(),is(0));
assertThat(version.getUpdate(),is(0));
assertThat(version.getSuffix(),is("Ubuntu-9b181-4"));
} }
@Test @Test
public void testUbuntu8() public void testUbuntu8()
{ {
JavaVersion version = JavaVersion.parse("1.8.0_151-8u151-b12-1~deb9u1-b12"); JavaVersion version = JavaVersion.parse("1.8.0_151-8u151-b12-1~deb9u1-b12");assertThat(version.toString(),is("1.8.0_151-8u151-b12-1~deb9u1-b12"));
assertThat(version.getPlatform(),is(8)); assertThat(version.getPlatform(),is(8));
assertThat(version.getMajor(),is(1)); assertThat(version.getMajor(),is(1));
assertThat(version.getMinor(),is(8)); assertThat(version.getMinor(),is(8));
assertThat(version.getMicro(),is(0)); assertThat(version.getMicro(),is(0));
assertThat(version.getUpdate(),is(151));
assertThat(version.getSuffix(),is("8u151-b12-1~deb9u1-b12"));
} }
} }