beemdevelopment/Aegis · error · OtpInfoException

bad period

Error message

bad period: %d

What it means

TotpInfo.setPeriod validates the TOTP period: it must be > 0 (checked in isPeriodValid) and small enough that converting seconds to milliseconds cannot overflow int (period <= Integer.MAX_VALUE/1000). Otherwise it throws OtpInfoException('bad period: %d').

Solutions

  1. Check TotpInfo.isPeriodValid(period) before calling setPeriod
  2. Fix the period parameter in the otpauth URI/import data (typical valid value is 30)
  3. Clamp the period to a sane range and warn instead of propagating the exception
  4. Catch OtpInfoException around parseUri/setPeriod and reject the malformed entry

Example fix

// before
totp.setPeriod(uriInfo.getPeriod()); // throws for period=0
// after
int period = uriInfo.getPeriod();
if (!TotpInfo.isPeriodValid(period)) {
    period = 30; // safe default
}
totp.setPeriod(period);
Defensive patterns

Strategy: validation

Validate before calling

if (!TotpInfo.isPeriodValid(period)) {
    throw new IllegalArgumentException("period must be > 0 and <= " + (Integer.MAX_VALUE / 1000));
}
totp.setPeriod(period);

Type guard

boolean validPeriod(int p) { return p > 0 && p <= Integer.MAX_VALUE / 1000; }

Try / catch

try {
    totp.setPeriod(period);
} catch (OtpInfoException e) {
    Log.w(TAG, "Bad period " + period + ", falling back to 30s", e);
    totp.setPeriod(30);
}

Prevention

When it happens

Trigger: Calling TotpInfo.setPeriod(int) with period <= 0 or period > 2147483 (Integer.MAX_VALUE/1000), directly or via parseUri when an otpauth URI carries an invalid period parameter.

Common situations: Hand-edited or third-party otpauth:// URIs with period=0, negative, or absurdly large values (e.g. period=3153600000), importing entries from other authenticator apps that permit unusual periods.

Understand the failure class

Background: "Invalid value" and "allowed values are" config errors: what your library rejected and how to fix it — this error's family across 41 libraries.

Related errors


AI-assisted analysis of beemdevelopment/Aegis@d6f4e5925a (2026-09-08). Data as JSON: /api/errors/66090c2c0a00cff8. Report an issue: GitHub.

Appendix: source

Thrown at app/src/main/java/com/beemdevelopment/aegis/otp/TotpInfo.java:75

        return obj;
    }

    public int getPeriod() {
        return _period;
    }

    public static boolean isPeriodValid(int period) {
        if (period <= 0) {
            return false;
        }

        // check for the possibility of an overflow when converting to milliseconds
        return period <= Integer.MAX_VALUE / 1000;
    }

    public void setPeriod(int period) throws OtpInfoException {
        if (!isPeriodValid(period)) {
            throw new OtpInfoException(String.format("bad period: %d", period));
        }
        _period = period;
    }

    public long getMillisTillNextRotation() {
        return TotpInfo.getMillisTillNextRotation(_period);
    }

    public static long getMillisTillNextRotation(int period) {
        long p = period * 1000;
        return p - (System.currentTimeMillis() % p);
    }

    @Override
    public boolean equals(Object o) {
        if (!(o instanceof TotpInfo)) {
            return false;
        }

View on GitHub (pinned to d6f4e5925a)