{"record":{"id":"80a0f63288752b6c","repo":"microg/GmsCore","slug":"width-and-height-must-not-be-negative","errorCode":null,"errorMessage":"width and height must not be negative","messagePattern":"width and height must not be negative","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"play-services-base/src/main/java/com/google/android/gms/common/images/WebImage.java","lineNumber":64,"sourceCode":"     *\n     * @param url The URL of the image.\n     * @throws IllegalArgumentException If the URL is null or empty.\n     */\n    public WebImage(Uri url) {\n        this(url, 0, 0);\n    }\n\n    /**\n     * Constructs a new {@link WebImage} with the given URL and dimensions.\n     *\n     * @param url    The URL of the image.\n     * @param width  The width of the image, in pixels.\n     * @param height The height of the image, in pixels.\n     * @throws IllegalArgumentException If the URL is null or empty, or the dimensions are invalid.\n     */\n    public WebImage(Uri url, int width, int height) {\n        if (url == null) throw new IllegalArgumentException(\"url cannot be null\");\n        if (width < 0 || height < 0) throw new IllegalArgumentException(\"width and height must not be negative\");\n        this.url = url;\n        this.width = width;\n        this.height = height;\n    }\n\n    /**\n     * Gets the image height, in pixels.\n     */\n    public int getHeight() {\n        return height;\n    }\n\n    /**\n     * Gets the image URL.\n     */\n    public Uri getUrl() {\n        return url;\n    }","sourceCodeStart":46,"sourceCodeEnd":82,"githubUrl":"https://github.com/microg/GmsCore/blob/157c9d86ac46c195a86c2f15ab55c84036223f95/play-services-base/src/main/java/com/google/android/gms/common/images/WebImage.java#L46-L82","documentation":"WebImage's constructor validates image dimensions and throws IllegalArgumentException when width or height is negative. The library requires non-negative pixel dimensions because they are written into a Bundle/Parcel and sent to remote clients, where negative values would be meaningless or corrupt the payload.","triggerScenarios":"Calling new WebImage(uri, width, height) with a negative width or negative height (e.g. passing an uninitialized or computed -1 sentinel value).","commonSituations":"Using -1 as a 'not yet measured' default for image size; computing dimensions from a failed bitmap decode or layout measurement that returns -1.","solutions":["Check width >= 0 and height >= 0 before constructing WebImage","Use 0 as the default/unknown dimension instead of -1","Clamp computed dimensions with Math.max(0, dimension)"],"exampleFix":"// before\nWebImage img = new WebImage(url, measuredWidth, measuredHeight); // measuredWidth = -1\n// after\nWebImage img = new WebImage(url, Math.max(0, measuredWidth), Math.max(0, measuredHeight));","handlingStrategy":"validation","validationCode":"if (url == null) throw new IllegalArgumentException(\"url required\");\nif (width < 0 || height < 0) throw new IllegalArgumentException(\"dimensions must be >= 0\");\nWebImage img = new WebImage(url, width, height);","typeGuard":null,"tryCatchPattern":"try { new WebImage(url, w, h); } catch (IllegalArgumentException e) { /* fall back to 0x0 or skip */ }","preventionTips":["Never use -1 as an 'unknown size' sentinel; use 0","Clamp computed dimensions with Math.max(0, d)","Validate dimensions at the measurement source"],"tags":["arguments","validation","android"],"backgroundTag":"invalid-argument-value","analyzedSha":"157c9d86ac46c195a86c2f15ab55c84036223f95","analyzedAt":"2026-09-06T17:27:33.892Z","contentChangedAt":"2026-09-06T17:27:33.892Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}