Commit 7c4abef9 authored by Eugen Sawin's avatar Eugen Sawin
Browse files

Bug 1677838 - [1.1] Verify the image resource size string is valid before...

Bug 1677838 - [1.1] Verify the image resource size string is valid before accepting it. r=geckoview-reviewers,agi

Differential Revision: https://phabricator.services.mozilla.com/D97801
parent 1437b21b
Loading
Loading
Loading
Loading
+31 −0
Original line number Diff line number Diff line
@@ -133,6 +133,37 @@ class ImageResourceTest : BaseSessionTest() {
        testValidImage(kValidTestImage6)
    }

    @Test
    fun invalidImageSize() {
        val invalidImage1 = TestImage(
            "path.ico", "image/icon", "16x16 32",
            arrayOf(16),
            arrayOf(16)
        )
        testValidImage(invalidImage1)

        val invalidImage2 = TestImage(
            "path.ico", "image/icon", "16x16 32xa32",
            arrayOf(16),
            arrayOf(16)
        )
        testValidImage(invalidImage2)

        val invalidImage3 = TestImage(
            "path.ico", "image/icon", "",
            null,
            null
        )
        testValidImage(invalidImage3)

        val invalidImage4 = TestImage(
            "path.ico", "image/icon", "abxab",
            null,
            null
        )
        testValidImage(invalidImage4)
    }

    @Test
    fun getBestRegular() {
        val collection = buildCollection(arrayOf(
+20 −9
Original line number Diff line number Diff line
@@ -109,20 +109,31 @@ public class ImageResource {
        }

        final String[] sizesStrs = sizesStr.toLowerCase().split(" ");
        final Size[] sizes = new Size[sizesStrs.length];
        final List<Size> sizes = new ArrayList<Size>();

        for (int i = 0; i < sizesStrs.length; ++i) {
            if (sizesStrs[i].equals("any")) {
        for (final String sizeStr: sizesStrs) {
            if (sizesStr.equals("any")) {
                // 0-width size will always be favored.
                sizes[i] = new Size(0, 0);
                sizes.add(new Size(0, 0));
                continue;
            }
            final String[] widthHeight = sizesStrs[i].split("x");
            sizes[i] = new Size(
            final String[] widthHeight = sizeStr.split("x");
            if (widthHeight.length != 2) {
                // Not spec-compliant size.
                continue;
            }
            try {
                sizes.add(new Size(
                        Integer.valueOf(widthHeight[0]),
                    Integer.valueOf(widthHeight[1]));
                        Integer.valueOf(widthHeight[1])));
            } catch (final NumberFormatException e) {
                Log.e(LOGTAG, "Invalid image resource size", e);
            }
        }
        if (sizes.isEmpty()) {
            return null;
        }
        return sizes;
        return sizes.toArray(new Size[0]);
    }

    public static @NonNull ImageResource fromBundle(