Divide by Zero, Dead in the Water — JavaScript Bug Hunt
Modelled on the USS Yorktown (September 1997): a crew member entered a 0 into a remote database field.
- Language: JavaScript
- Layer: Backend
- Difficulty: Easy
- Concepts: Error Handling, Validation
- Modelled on: USS Yorktown · 1997
- Visible tests: a healthy set of readings averages correctly; a zero divisor is rejected, not propagated
- Reward: 50 XP for a complete fix
Incident
- Severity: SEV-1 (priority: Critical (loss of propulsion))
- Ticket: BUG-DDG48
- Reported by: engineering watch
- Time to fix: 10 minutes, the target for an easy incident
Failing checks on the shipped build
- a zero divisor is rejected, not propagated — a zero divisor must not reach the arithmetic: expected {"ok":false,"value":0} but got {"ok":true,"value":null}
How to work this incident
- Read the report: write down the expected behaviour and the actual one in a sentence each.
- Reproduce it: run the visible tests and find the failing check that matches the report.
- Read the logs and the failing checks for the first value that is wrong — the earliest wrong value is nearer the cause than the loudest error.
- Follow the request through the handler, the validation and the service it calls; find the first step whose output disagrees with the report.
- Fix the cause rather than the symptom, run the visible tests again, and submit against the hidden ones — then write the root cause down.
Briefing
Modelled on the USS Yorktown (September 1997): a crew member entered a 0 into a remote database field. The propulsion control software divided by it, the exception cascaded through the network, and the guided-missile cruiser lost propulsion and had to be towed back to port.
calibration.js computes a ratio from operator-supplied readings and has the same missing guard.
Fix computeRatio so a zero divisor is rejected rather than propagated.
Bug report
BUG-DDG48 · Priority: Critical (loss of propulsion) · Reported by: engineering watch
computeRatio(readings):
- returns { ok: true, value } when every divisor is non-zero
- returns { ok: false, value: 0 } when any divisor is zero — the caller then falls back to the last good value instead of shutting down
Observed: a zero divisor produces Infinity (or NaN), which then poisons every downstream average and takes the whole control loop with it.
Logs
[prop] ratio=Infinity from divisor=0 at index 2
[prop] control loop aborted; manual override requiredThe code as shipped
src/control/calibration.js (editable)
// Averages numerator/divisor across all readings.
exports.computeRatio = function (readings) {
var total = 0;
for (var i = 0; i < readings.length; i++) {
total += readings[i].numerator / readings[i].divisor;
}
return { ok: true, value: total / readings.length };
};
Open the hunt to start the incident clock, edit the files, run the visible tests and submit against the hidden ones, then write the root cause down. More JavaScript bug hunts.