{"record":{"id":"4fa3ac6fac2029cb","repo":"apache/hadoop","slug":"n-n-0","errorCode":null,"errorMessage":"n = {n} <= 0","messagePattern":"n = (.+?) <= 0","errorType":"validation","errorClass":"HadoopIllegalArgumentException","httpStatus":null,"severity":"error","filePath":"hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/util/ByteArrayManager.java","lineNumber":65,"sourceCode":"  private static void logDebugMessage() {\n    final StringBuilder b = DEBUG_MESSAGE.get();\n    LOG.debug(b.toString());\n    b.setLength(0);\n  }\n\n  static final int MIN_ARRAY_LENGTH = 32;\n  static final byte[] EMPTY_BYTE_ARRAY = {};\n\n  /**\n   * @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  /**","sourceCodeStart":47,"sourceCodeEnd":83,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/util/ByteArrayManager.java#L47-L83","documentation":"ByteArrayManager pools byte arrays in power-of-two buckets, and leastPowerOfTwo(int) is its public rounding helper (used internally by newByteArray for lengths above 32). 'Least power of two >= n' is undefined for n <= 0, so such input is rejected immediately with HadoopIllegalArgumentException. Internal callers pre-validate (arrayLength >= 0, and 0 short-circuits to an empty array), so this throw comes from direct API use or from upstream code that computed a non-positive size.","triggerScenarios":"Calling ByteArrayManager.leastPowerOfTwo(n) directly with n = 0 or negative - typically a size derived from configuration that defaulted to 0, or arithmetic that underflowed before the call.","commonSituations":"Buffer-size plumbing where an unset property yields 0; subtraction or overflow producing a negative length; unit tests probing the helper's edge cases.","solutions":["Trace the argument: log or inspect n at the call site - the real bug is upstream in how the size was computed","Guard the call: only round sizes already validated as positive","If zero is legitimate in your domain, branch on it before rounding (return a default bucket or your own domain error)"],"exampleFix":"// before\nint bucket = ByteArrayManager.leastPowerOfTwo(size); // size == 0 -> throws\n\n// after\nint bucket = (size <= 0)\n    ? 32  // sensible default bucket, or throw your own domain error\n    : ByteArrayManager.leastPowerOfTwo(size);","handlingStrategy":"validation","validationCode":"if (n <= 0) {\n  throw new IllegalArgumentException(\"size must be positive, got \" + n);\n}\nint rounded = ByteArrayManager.leastPowerOfTwo(n);","typeGuard":null,"tryCatchPattern":"try {\n  return ByteArrayManager.leastPowerOfTwo(n);\n} catch (org.apache.hadoop.HadoopIllegalArgumentException e) {\n  // surface a domain-meaningful error naming the offending size\n  throw new IllegalArgumentException(\"invalid buffer size: \" + n, e);\n}","preventionTips":["Validate sizes derived from configuration before feeding them to rounding/allocation helpers","Use org.apache.hadoop.util.Preconditions or explicit checks at the boundary where sizes enter your code"],"tags":["hdfs","util","byte-buffer","argument-validation","power-of-two"],"backgroundTag":"invalid-argument-value","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-23T01:17:44.959Z"}