Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ce113b78 authored by Mårten Kongstad's avatar Mårten Kongstad
Browse files

Build.parseFullVersion: verify values are within acceptable range

When converting a major.minor version String to a Java int, only so many
digits can fit. This puts a hard limit on the maximum value of the major
and minor versions:

  - major must be less than 21_474
  - minor must be less than 100_000

Update Build.parseFullVersion to fail if either value is larger than the
limit.

Note: neither major nor minor version can be negative:
Build.parseFullVersion already checks for that.

Bug: 380245536
Test: atest FrameworksCoreTests:android.os.BuildTest
Flag: EXEMPT trivial bugfix
Change-Id: I5004ca7c0553110cdab3fe66fe15ece6ce6846ba
parent 10b79530
Loading
Loading
Loading
Loading
+14 −3
Original line number Diff line number Diff line
@@ -1558,6 +1558,7 @@ public class Build {
     * @hide
     */
    @SuppressWarnings("FlaggedApi") // SDK_INT_MULTIPLIER is defined in this file
    @SuppressLint("InlinedApi")
    public static @SdkIntFull int parseFullVersion(@NonNull String version) {
        int index = version.indexOf('.');
        int major;
@@ -1569,12 +1570,22 @@ public class Build {
                major = Integer.parseInt(version.substring(0, index));
                minor = Integer.parseInt(version.substring(index + 1));
            }
            if (major < 0 || minor < 0) {
                throw new NumberFormatException();
            if (major < 0) {
                throw new NumberFormatException("negative major version");
            }
            if (major >= 21474) {
                throw new NumberFormatException("major version too large, must be less than 21474");
            }
            if (minor < 0) {
                throw new NumberFormatException("negative minor version");
            }
            if (minor >= VERSION_CODES_FULL.SDK_INT_MULTIPLIER) {
                throw new NumberFormatException("minor version too large, must be less than "
                        + VERSION_CODES_FULL.SDK_INT_MULTIPLIER);
            }
        } catch (NumberFormatException e) {
            throw new IllegalArgumentException("failed to parse '" + version
                    + "' as a major.minor version code");
                    + "' as a major.minor version code", e);
        }
        return major * VERSION_CODES_FULL.SDK_INT_MULTIPLIER + minor;
    }
+14 −0
Original line number Diff line number Diff line
@@ -177,6 +177,20 @@ public class BuildTest {
        });
    }

    @Test
    public void testParseFullVersionIncorrectInputMajorVersionTooLarge() throws Exception {
        assertThrows(IllegalArgumentException.class, () -> {
            Build.parseFullVersion("40000.1");
        });
    }

    @Test
    public void testParseFullVersionIncorrectInputMinorVersionTooLarge() throws Exception {
        assertThrows(IllegalArgumentException.class, () -> {
            Build.parseFullVersion("3.99999999");
        });
    }

    @Test
    public void testFullVersionToStringCorrectInput() throws Exception {
        assertEquals("0.0", Build.fullVersionToString(0));