{"record":{"id":"d6931f6f8dfbdde4","repo":"apache/iceberg","slug":"integer-overflow","errorCode":null,"errorMessage":"integer overflow","messagePattern":"integer overflow","errorType":"exception","errorClass":"ArithmeticException","httpStatus":null,"severity":"error","filePath":"api/src/main/java/org/apache/iceberg/metrics/DefaultCounter.java","lineNumber":121,"sourceCode":"    public void increment() {\n      increment(1);\n    }\n\n    @Override\n    public void increment(Integer amount) {\n      DefaultCounter.this.increment(amount);\n    }\n\n    @Override\n    public Optional<Integer> count() {\n      return Optional.of(value());\n    }\n\n    @Override\n    public Integer value() {\n      long value = counter.longValue();\n      if (value > Integer.MAX_VALUE) {\n        throw new ArithmeticException(\"integer overflow\");\n      }\n      return (int) value;\n    }\n\n    @Override\n    public MetricsContext.Unit unit() {\n      return unit;\n    }\n  }\n\n  private class AsLongCounter implements MetricsContext.Counter<Long> {\n\n    @Override\n    public void increment() {\n      DefaultCounter.this.increment();\n    }\n\n    @Override","sourceCodeStart":103,"sourceCodeEnd":139,"githubUrl":"https://github.com/apache/iceberg/blob/86d9c8fc543e7c56c9f624eb725f76c9baff9570/api/src/main/java/org/apache/iceberg/metrics/DefaultCounter.java#L103-L139","documentation":"DefaultCounter.asIntCounter() wraps a long-backed counter but must return an Integer; if the underlying count exceeds Integer.MAX_VALUE an ArithmeticException('integer overflow') is thrown rather than silently truncating. This protects correctness of int-typed counters.","triggerScenarios":"Incrementing an int counter (obtained via MetricsContext.counter(name, Integer.class, unit)) past 2,147,483,647 and then calling value() (directly or via toString/logging/reporting).","commonSituations":"Counting large row/record totals with an int counter; aggregating many increments in long-running jobs; metric reporting pipelines that stringify the counter.","solutions":["Use Long.class as the counter type instead of Integer.class for potentially large counts","Switch to counter.longValue() on a long counter instead of int value()","Reset or split counters if int semantics are required","Catch ArithmeticException only if overflow is an acceptable, recoverable condition"],"exampleFix":"// before\nCounter<Integer> c = metrics.counter(\"records\", Integer.class, Unit.COUNT);\n// after\nCounter<Long> c = metrics.counter(\"records\", Long.class, Unit.COUNT);","handlingStrategy":"validation","validationCode":"if (counter.longValue() > Integer.MAX_VALUE) { use long; }","typeGuard":null,"tryCatchPattern":"try { return intCounter.value(); } catch (ArithmeticException e) { return Integer.MAX_VALUE; }","preventionTips":["Use Long counters for large counts","Prefer longValue()","Bound-test counters in tests"],"tags":["java","metrics","overflow"],"backgroundTag":"value-out-of-range","analyzedSha":"86d9c8fc543e7c56c9f624eb725f76c9baff9570","analyzedAt":"2026-09-12T00:46:39.097Z","contentChangedAt":"2026-09-12T00:46:39.097Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}