Skip to content

Commit 3a0c4a3

Browse files
committed
fix: add defensive guards in Camera1Provider zoom methods
- getMaxZoomRatio: guard against null/empty getZoomRatios() list and use Math.min to avoid ArrayIndexOutOfBoundsException on vendor devices where list size < getMaxZoom() + 1 - setZoomRatio: reject NaN/Infinity/non-positive ratio early; guard against null/empty zoomRatios list; use long arithmetic to avoid int overflow in diff computation Addresses coderabbitai review comments (Critical + Major).
1 parent 1f328f1 commit 3a0c4a3

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

library/src/main/java/org/wysaid/camera/Camera1Provider.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,22 +159,30 @@ public float getMaxZoomRatio() {
159159
Camera.Parameters params = CameraInstance.getInstance().getParams();
160160
if (params == null || !params.isZoomSupported()) return 1.0f;
161161
List<Integer> ratios = params.getZoomRatios();
162-
return ratios.get(params.getMaxZoom()) / 100.0f;
162+
if (ratios == null || ratios.isEmpty()) return 1.0f;
163+
int maxIndex = Math.min(params.getMaxZoom(), ratios.size() - 1);
164+
return ratios.get(maxIndex) / 100.0f;
163165
}
164166

165167
@Override
166168
public void setZoomRatio(float ratio) {
167169
Camera.Parameters params = CameraInstance.getInstance().getParams();
168170
if (params == null || !params.isZoomSupported()) return;
169171

172+
if (!Float.isFinite(ratio) || ratio <= 0f) {
173+
Log.e(LOG_TAG, "Camera1: invalid zoom ratio: " + ratio);
174+
return;
175+
}
176+
170177
List<Integer> zoomRatios = params.getZoomRatios();
178+
if (zoomRatios == null || zoomRatios.isEmpty()) return;
171179
int targetHundredths = Math.round(ratio * 100.0f);
172180

173181
// Find the closest discrete zoom index for the requested ratio.
174182
int bestIndex = 0;
175-
int bestDiff = Integer.MAX_VALUE;
183+
long bestDiff = Long.MAX_VALUE;
176184
for (int i = 0; i < zoomRatios.size(); i++) {
177-
int diff = Math.abs(zoomRatios.get(i) - targetHundredths);
185+
long diff = Math.abs((long) zoomRatios.get(i) - targetHundredths);
178186
if (diff < bestDiff) {
179187
bestDiff = diff;
180188
bestIndex = i;

0 commit comments

Comments
 (0)