Java traps · Chapter 8 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
  • Java
  • In the all labs plan

The first 2 chapters of Java traps are free. This one comes with the all labs plan.

Why you need this

bytes is a long, so why the second line? Java works out the right side first, with the types it finds there: gb and every number are int, so every multiplication is an int multiplication, and the result has already wrapped when it is widened to long for the assignment. A cast in front of the brackets (the third line) is the same thing: too late.

The fix is to make the first step a long: 1024L, so gb * 1024L is long and every step after it too. Math.multiplyExact and Math.addExact throw instead of wrapping, which turns a silent wrong answer into a loud one. For time, Duration.ofDays(n).toMillis() does the arithmetic in long for you.

Why did a week work? Small numbers fit: the overflow only starts past the limit, so every test with small values passes.

What you build: 3 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    Repro.java: a monthly pass that expires before it is sold

    Write reproduce() in Repro.java. Take the moment the first monthly pass was sold, 2026-10-25T06:00:00Z, as epoch milliseconds, ask ChaiPass.expiresAt when a 30-day pass sold then expires, and return how long the pass lasts: the expiry minus the sale time, a long.

    The check runs your reproduce() twice: beside the original ChaiPass.java it must return -1702967296 (a pass that ends before it starts), and beside a fixed ChaiPass.java it must return 2592000000 (30 days in milliseconds). So it must really ask expiresAt. Use that fixed sale time, never the clock. No real waiting and no reading input: reproduce() must finish within 3 seconds.

  2. Task 2

    ChaiPass.java: a monthly pass that lasts a month

    Fix expiresAt(long issuedAtMillis, int days) in ChaiPass.java so that a pass of any length, from 1 day to 365, expires exactly that many days of 24 hours after it was sold, in epoch milliseconds, without an exception. isValid then works for every pass: true from the sale up to, not including, the expiry.

    Keep isValid's rule, sell and sell's log line (pass <id>: <days> days, sold <time>, expires <time>) as they are. The check sells its own passes at its own times, for 1, 7, 24, 25, 30, 90 and 365 days, and asks isValid at the sale, the day after it, just before and at the expiry, and the day after the expiry.

  3. Task 3

    ChaiPassTest.java: a test that would have caught it

    Write a JUnit test class, ChaiPassTest.java, that would have caught this bug before the monthly pass went on sale. The check runs your file three times: beside the original ChaiPass.java, where at least one test must fail; beside the fixed ChaiPass.java, where every test must pass; and beside a wrong fix that casts to long after multiplying in int, where at least one test must fail.

    So test both sides: a short pass still lasts its days, and a pass of 25 days or more lasts exactly its days and is valid the day after it is sold. Use fixed times, never the clock, and write expected values that cannot overflow themselves. No real waiting: each test must finish within 3 seconds.

What you will learn

  • Repro.java: a monthly pass that expires before it is sold
  • ChaiPass.java: a monthly pass that lasts a month
  • ChaiPassTest.java: a test that would have caught it

More in Everyday traps

Build "Integer overflow" yourself

Tapri Counter runs on Java, and Kabir, its senior developer, 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 Java traps are free. This one comes with the all labs plan.