The Sunset That Crashed the Phone — Java Bug Hunt
Modelled on the Android wallpaper crash of 2020. A photograph of a lake at sunset, shared widely online, put some Android phones into a crash loop when it…
- Language: Java
- Layer: Frontend
- Difficulty: Easy
- Concepts: Overflow, Bounds
- Modelled on: Android · 2020
- Visible tests: black and white land in the end bins; a pixel just above 255 is counted in the top bin
- Reward: 50 XP for a complete fix
Briefing
Modelled on the Android wallpaper crash of 2020. A photograph of a lake at sunset, shared widely online, put some Android phones into a crash loop when it was set as the wallpaper. The image used a wide-gamut colour space; after conversion, a pixel's computed luminance rounded up to 256 and was used to index a 256-entry histogram — one past the end. System UI crashed on every start until the wallpaper was removed. The fix clamped the value to the histogram's range.
Histogram.java builds the luminance histogram System UI uses to pick light or dark status-bar icons. Converted pixels arrive on the 0–255 scale but can land slightly outside it.
Fix build so every pixel lands in a valid bin.
Bug report
BUG-WALLPAPER · Priority: Critical (crash loop) · Reported by: device QA
Histogram.build(pixels) returns int[256]:
- each pixel's luminance is Luminance.of(px), rounded to the nearest integer
- a value above 255 counts in bin 255; a value below 0 counts in bin 0
- every pixel is counted exactly once (the bins sum to pixels.length)
Observed: ArrayIndexOutOfBoundsException: Index 256 out of bounds for length 256 when the sunset photo is set as wallpaper.
Logs
E AndroidRuntime: FATAL EXCEPTION: main Process: com.android.systemui
E AndroidRuntime: java.lang.ArrayIndexOutOfBoundsException: length=256; index=256
I ActivityManager: Process com.android.systemui has died (restart 7)The code as shipped
src/ui/Histogram.java (editable)
class Histogram {
static final int BINS = 256;
static int[] build(double[][] pixels) {
int[] bins = new int[BINS];
for (double[] px : pixels) {
int y = (int) Math.round(Luminance.of(px));
bins[y]++;
}
return bins;
}
}Read-only context: src/ui/Luminance.java.
Open the hunt to edit the files, run the visible tests and submit against the hidden ones. More Java bug hunts.