Go traps · Chapter 3 of 16
Integer overflow
Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Everyday traps
- 3 tasks
- Go
- In the all labs plan
The first 2 chapters of Go traps are free. This one comes with the all labs plan.
Why you need this
Go never panics on integer overflow at run time, and go vet says nothing. The compiler only checks constant expressions, which it can work out before the program runs:
var limit int32 = 2147483647 + 1
./main.go:6:20: cannot use 2147483647 + 1 (untyped int constant 2147483648) as int32 value in variable declaration (overflows)
Numbers from meters arrive at run time, so nothing stops them. Money arithmetic needs a type with room for the biggest product, and int64 holds up to 9,223,372,036,854,775,807.
What you build: 3 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
repro.go: bill a big month negative on purpose
Write reproduce() in repro.go, which returns an int64. Call Cost(wh, paisePerKWh) (or InvoiceLine) from bill.go for a fleet's month: energy in watt-hours and a tariff in paise a kWh whose product passes 2,147,483,647 and wraps negative, like the ticket's 38640000 Wh at DC60's 2400. Return the paise it gave.
The check runs your reproduce() twice, each time in a fresh build of the case's folder: beside the original bill.go it must return a negative number, and beside a fixed bill.go a positive number (the right paise). So it must really call Cost with a big month. No real waiting and no input: reproduce() must finish within 3 seconds.
- Task 2
bill.go: a big month bills the right amount
Fix Cost(wh, paisePerKWh int32) int64 in bill.go so it returns wh times paisePerKWh divided by 1000, rounded to the nearest paisa (add 500, then divide by 1000), for any int32 numbers the meters send: Cost(1234, 1400) is 1728, Cost(38640000, 2400) is 92736000, and Cost(2147483647, 2400) is 5153960753.
Keep the signature (int32 in, int64 out), the rounding, and InvoiceLine with its line invoice <fleet> <tariff>: <wh> Wh at <tariff> paise/kWh = <paise> paise. The check uses its own numbers, from one watt-hour up to the largest int32. Converting the result to int64 after multiplying is too late, and multiplying in uint32 still wraps.
- Task 3
bill_test.go: a test that would have caught it
Write a Go test file, bill_test.go, that would have caught this bug before the first invoice. The check runs your file three times, each in a fresh build: beside the original bill.go, where at least one test must fail; beside the fixed bill.go, where every test must pass; and beside a wrong fix that multiplies in uint32 (never negative, but it still wraps past 4,294,967,295), where at least one test must fail.
So check Cost's exact paise, not only that it is positive: for a small session, and for a month whose watt-hours times tariff passes 4,294,967,295, like the ticket's 38640000 Wh at 2400. Each test must finish within a second or two (the whole file is stopped after 8 seconds).
What you will learn
- repro.go: bill a big month negative on purpose
- bill.go: a big month bills the right amount
- bill_test.go: a test that would have caught it
More in Everyday traps
Build "Integer overflow" yourself
BijliGo runs its EV chargers on Go, and Joseph, its senior engineer, keeps assigning you tickets. Each is a real trap interviews love to ask about. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Go traps are free. This one comes with the all labs plan.