fix(android): coerce string width/height in ImageAssetOptions (#10862)

This commit is contained in:
Abdelkarim Ait Bourich
2025-10-30 13:26:53 -07:00
committed by GitHub
parent d856826b7a
commit 9db6369aed
4 changed files with 93 additions and 5 deletions

View File

@ -170,6 +170,36 @@ public class Utils {
boolean autoScaleFactor;
}
private static int parsePositiveInt(JSONObject object, String key) {
if (object == null || key == null) {
return 0;
}
try {
if (!object.has(key) || object.isNull(key)) {
return 0;
}
Object value = object.get(key);
if (value instanceof Number) {
int parsed = (int) Math.floor(((Number) value).doubleValue());
return parsed > 0 ? parsed : 0;
}
if (value instanceof String) {
String s = ((String) value).trim();
if (s.length() == 0) {
return 0;
}
try {
int parsed = Integer.parseInt(s);
return parsed > 0 ? parsed : 0;
} catch (NumberFormatException ignored) {
return 0;
}
}
} catch (JSONException ignored) {
}
return 0;
}
private static final Executor executors = Executors.newCachedThreadPool();
@ -297,8 +327,9 @@ public class Utils {
try {
JSONObject object = new JSONObject(options);
opts.width = object.optInt("width", 0);
opts.height = object.optInt("height", 0);
// Coerce numeric strings or numbers; fallback to 0 for invalid values
opts.width = parsePositiveInt(object, "width");
opts.height = parsePositiveInt(object, "height");
opts.keepAspectRatio = object.optBoolean("keepAspectRatio", true);
opts.autoScaleFactor = object.optBoolean("autoScaleFactor", true);
} catch (JSONException ignored) {