{"record":{"id":"5b2e4e3c47ce242c","repo":"apache/hadoop","slug":"does-not-fit-in-a-long","errorCode":null,"errorMessage":"{} does not fit in a Long","messagePattern":"(.+?) does not fit in a Long","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/StringUtils.java","lineNumber":923,"sourceCode":"     */\n    public static long string2long(String s) {\n      s = s.trim();\n      final int lastpos = s.length() - 1;\n      final char lastchar = s.charAt(lastpos);\n      if (Character.isDigit(lastchar))\n        return Long.parseLong(s);\n      else {\n        long prefix;\n        try {\n          prefix = TraditionalBinaryPrefix.valueOf(lastchar).value;\n        } catch (IllegalArgumentException e) {\n          throw new IllegalArgumentException(\"Invalid size prefix '\" + lastchar\n              + \"' in '\" + s\n              + \"'. Allowed prefixes are k, m, g, t, p, e(case insensitive)\");\n        }\n        long num = Long.parseLong(s.substring(0, lastpos));\n        if (num > (Long.MAX_VALUE/prefix) || num < (Long.MIN_VALUE/prefix)) {\n          throw new IllegalArgumentException(s + \" does not fit in a Long\");\n        }\n        return num * prefix;\n      }\n    }\n\n    /**\n     * Convert a long integer to a string with traditional binary prefix.\n     * \n     * @param n the value to be converted\n     * @param unit The unit, e.g. \"B\" for bytes.\n     * @param decimalPlaces The number of decimal places.\n     * @return a string with traditional binary prefix.\n     */\n    public static String long2String(long n, String unit, int decimalPlaces) {\n      if (unit == null) {\n        unit = \"\";\n      }\n      //take care a special case","sourceCodeStart":905,"sourceCodeEnd":941,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/StringUtils.java#L905-L941","documentation":"After splitting a prefixed value into a number and a 1024^n multiplier, StringUtils.string2long explicitly checks num > Long.MAX_VALUE/prefix (or the MIN_VALUE bound for negatives) so that the multiplication cannot silently overflow. When the scaled result would leave the 64-bit signed long range, it throws this IllegalArgumentException. Example: '100000000e' means 10^8 * 2^60, but Long.MAX_VALUE / 2^60 is about 8, so it is rejected.","triggerScenarios":"string2long(\"100000000e\"), string2long(\"99999999999g\"), string2long(\"9007199254740993k\"); any value whose numeric part exceeds Long.MAX_VALUE (or is below Long.MIN_VALUE) after division by the prefix multiplier. Negative overflows hit the num < Long.MIN_VALUE/prefix branch, e.g. '-100000000e'.","commonSituations":"Oversized heap/disk/cache size settings (values past 8 exabytes when suffixed with 'e'); test fixtures with extra digits; copy-pasting Long.MAX_VALUE plus a 'g'/'t' suffix; generated configs computing size expressions that explode past long range.","solutions":["Lower the configured value so number * 1024^n fits in a signed 64-bit long (e.g. <= 8 with 'e', <= 8388608 with 'g', <= 8589934592 with 'm').","Drop the suffix and use the plain digit form if you genuinely need Long.MAX_VALUE: '9223372036854775807'.","Validate the magnitude before calling string2long (Math.abs(num) <= Long.MAX_VALUE / prefix) and emit a targeted config error.","Catch IllegalArgumentException around config parsing to report which property overflowed instead of failing deep in startup."],"exampleFix":"// before\nlong quota = StringUtils.string2long(\"100000000e\"); // 10^8 * 2^60 > Long.MAX\n// throws: 100000000e does not fit in a Long\n\n// after\nlong quota = StringUtils.string2long(\"8e\"); // 8 * 2^60, fits","handlingStrategy":"validation","validationCode":"static boolean fitsInLong(String s) {\n  s = s.trim();\n  char last = s.charAt(s.length() - 1);\n  if (Character.isDigit(last)) {\n    return true; // Long.parseLong will enforce range\n  }\n  char u = Character.toUpperCase(last);\n  long[] mult = {1024L, 1L<<20, 1L<<30, 1L<<40, 1L<<50, 1L<<60};\n  char[] syms = {'K','M','G','T','P','E'};\n  for (int i = 0; i < syms.length; i++) {\n    if (u == syms[i]) {\n      long num = Long.parseLong(s.substring(0, s.length() - 1));\n      return num <= Long.MAX_VALUE / mult[i] && num >= Long.MIN_VALUE / mult[i];\n    }\n  }\n  return false;\n}\n\nif (!fitsInLong(val)) {\n  throw new IllegalArgumentException(\"Value '\" + val + \"' overflows a long\");\n}","typeGuard":null,"tryCatchPattern":"try {\n  long quota = StringUtils.string2long(value);\n} catch (IllegalArgumentException e) {\n  // e.getMessage() says '<value> does not fit in a Long' or names a bad prefix\n  throw new ConfigurationException(\"size property: \" + e.getMessage(), e);\n}","preventionTips":["Cap user-supplied size configs at sane operational bounds (e.g. <= a few petabytes) instead of long range.","For max-size defaults use the plain digit form Long.MAX_VALUE with no suffix.","Test oversized inputs in config validation unit tests."],"tags":["numeric-overflow","string-parsing","size-units","hadoop-common"],"backgroundTag":"numeric-overflow","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}