{"record":{"id":"d3b5cc087a4f33a2","repo":"apache/hadoop","slug":"overflow-for-n-n-the-least-power-of-two-the","errorCode":null,"errorMessage":"Overflow: for n = {n}, the least power of two (the least integer x with x >= n and x a power of two) = {overflow} > Integer.MAX_VALUE = 2147483647","messagePattern":"Overflow: for n = (.+?), the least power of two \\(the least integer x with x >= n and x a power of two\\) = (.+?) > Integer\\.MAX_VALUE = 2147483647","errorType":"exception","errorClass":"ArithmeticException","httpStatus":null,"severity":"error","filePath":"hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/util/ByteArrayManager.java","lineNumber":75,"sourceCode":"   * @return the least power of two greater than or equal to n, i.e. return\n   *         the least integer x with x &gt;= n and x a power of two.\n   *\n   * @throws HadoopIllegalArgumentException\n   *           if n &lt;= 0.\n   */\n  public static int leastPowerOfTwo(final int n) {\n    if (n <= 0) {\n      throw new HadoopIllegalArgumentException(\"n = \" + n + \" <= 0\");\n    }\n\n    final int highestOne = Integer.highestOneBit(n);\n    if (highestOne == n) {\n      return n; // n is a power of two.\n    }\n    final int roundUp = highestOne << 1;\n    if (roundUp < 0) {\n      final long overflow = ((long) highestOne) << 1;\n      throw new ArithmeticException(\n          \"Overflow: for n = \" + n + \", the least power of two (the least\"\n          + \" integer x with x >= n and x a power of two) = \"\n          + overflow + \" > Integer.MAX_VALUE = \" + Integer.MAX_VALUE);\n    }\n    return roundUp;\n  }\n\n  /**\n   * A counter with a time stamp so that it is reset automatically\n   * if there is no increment for the time period.\n   */\n  static class Counter {\n    private final long countResetTimePeriodMs;\n    private long count = 0L;\n    private long timestamp = Time.monotonicNow();\n\n    Counter(long countResetTimePeriodMs) {\n      this.countResetTimePeriodMs = countResetTimePeriodMs;","sourceCodeStart":57,"sourceCodeEnd":93,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/util/ByteArrayManager.java#L57-L93","documentation":"leastPowerOfTwo rounds up via highestOneBit(n) << 1; for any n in (2^30, Integer.MAX_VALUE] the true answer is 2^31, which does not fit in an int. The helper detects the wrap (roundUp < 0), recomputes the value as a long, and throws ArithmeticException explaining that the least power of two exceeds Integer.MAX_VALUE. This protects callers from silently receiving a negative bucket size.","triggerScenarios":"leastPowerOfTwo(n) with n > 1073741824 - requesting a pooled byte-buffer bucket for a roughly 2 GB allocation, or passing a size that grew past 1 GiB through int arithmetic.","commonSituations":"Applications wiring user-provided buffer sizes into ByteArrayManager-backed paths; stress tests with multi-gigabyte chunks; int multiplication overflowing before the call.","solutions":["Cap the requested size at 1 GiB or reject larger requests before rounding","Do sizing math in long and validate it fits an int earlier in the pipeline","If multi-GB buffers are legitimate, allocate them directly (new byte[]) instead of through the power-of-two bucket scheme"],"exampleFix":"// before\nint bucket = ByteArrayManager.leastPowerOfTwo(requestedBytes); // 2_000_000_000 -> throws\n\n// after\nif (requestedBytes > (1 << 30)) {\n  throw new IllegalArgumentException(\"buffer too large for pooling: \" + requestedBytes);\n}\nint bucket = ByteArrayManager.leastPowerOfTwo(requestedBytes);","handlingStrategy":"validation","validationCode":"if (n > (1 << 30)) {\n  throw new IllegalArgumentException(\"size too large for power-of-two bucketing: \" + n);\n}\nint rounded = ByteArrayManager.leastPowerOfTwo(n);","typeGuard":null,"tryCatchPattern":"try {\n  return ByteArrayManager.leastPowerOfTwo(n);\n} catch (ArithmeticException e) {\n  // the rounded size exceeds Integer.MAX_VALUE; reject or switch to direct allocation\n  throw new IllegalArgumentException(\"buffer too large: \" + n, e);\n}","preventionTips":["Do buffer-size math in long and validate it fits an int before calling","Cap user-supplied buffer sizes at 1 GiB when they feed ByteArrayManager-style pooling"],"tags":["hdfs","util","integer-overflow","byte-buffer","power-of-two"],"backgroundTag":"integer-overflow","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}