{"record":{"id":"687947411c3e209e","repo":"bazelbuild/bazel","slug":"invalid-base-d-literal-s","errorCode":null,"errorMessage":"invalid base-%d literal: %s","messagePattern":"invalid base-(.+?) literal: (.+?)","errorType":"exception","errorClass":"NumberFormatException","httpStatus":null,"severity":"error","filePath":"src/main/java/net/starlark/java/eval/StarlarkInt.java","lineNumber":159,"sourceCode":"\n    // No prefix, no base? Use decimal.\n    if (digits == s && base == 0) {\n      // Don't infer base when input starts with '0' due to octal/decimal ambiguity.\n      if (s.length() > 1 && s.charAt(0) == '0') {\n        throw new NumberFormatException(\n            \"cannot infer base when string begins with a 0: \"\n                + Starlark.repr(stringForErrors, StarlarkSemantics.DEFAULT));\n      }\n      base = 10;\n    }\n    if (base < 2 || base > 36) {\n      throw new NumberFormatException(\n          String.format(\"invalid base %d (want 2 <= base <= 36)\", base));\n    }\n\n    // Do not allow Long.parseLong and new BigInteger to accept another +/- sign.\n    if (digits.startsWith(\"+\") || digits.startsWith(\"-\")) {\n      throw new NumberFormatException(\n          String.format(\n              \"invalid base-%d literal: %s\",\n              base, Starlark.repr(stringForErrors, StarlarkSemantics.DEFAULT)));\n    }\n\n    StarlarkInt result;\n    try {\n      result = StarlarkInt.of(Long.parseLong(digits, base));\n    } catch (NumberFormatException unused1) {\n      try {\n        result = StarlarkInt.of(new BigInteger(digits, base));\n      } catch (NumberFormatException unused2) {\n        throw new NumberFormatException(\n            String.format(\n                \"invalid base-%d literal: %s\",\n                base, Starlark.repr(stringForErrors, StarlarkSemantics.DEFAULT)));\n      }\n    }","sourceCodeStart":141,"sourceCodeEnd":177,"githubUrl":"https://github.com/bazelbuild/bazel/blob/e6e199d0601a244511b4cf18c8b2828aa73db1fd/src/main/java/net/starlark/java/eval/StarlarkInt.java#L141-L177","documentation":"StarlarkInt.parse's parse-failure error: it is raised in two places — first when, after stripping one leading sign and any 0x/0o/0b prefix, the digit string still starts with '+' or '-' (Long.parseLong/BigInteger must not see an embedded second sign), and again when neither Long.parseLong nor BigInteger can parse the digits in the requested base. The message includes the base and the repr of the original input.","triggerScenarios":"int(\"--5\"), int(\"+-3\"), int(\"0x-10\", 16) (sign after prefix), int(\"abc\", 10), int(\"129\", 8) (digit 9 invalid in base 8), int(\"1_000\") (underscores unsupported), int(\" \"+\"5\") — whitespace not stripped.","commonSituations":"Parsing numbers with sign conventions the parser disallows (double negatives for 'minus a negative'); digit separators like 1_000 copied from Java/Python 3.6; hex strings with 0x prefix plus explicit base 16 AND a sign in the wrong place; whitespace or units left in the string (\"5px\", \" 5\").","solutions":["Normalize input before parsing: strip whitespace, collapse duplicate signs, remove separators: s = s.replace(\"_\", \"\").strip().","Apply the sign yourself: int(s.lstrip(\"+-\"), base) * (-1 if s.startswith(\"-\") else 1).","Check the digit alphabet matches the base (base 8 has no 8/9; base 16 allows a-f) and remove 0x/0o/0b when passing an explicit base."],"exampleFix":"# before\nn = int(raw, 16)  # raw = \"-0xff\" or \"0xff\" with base given is fine, but \"ff_00\" or \" -0xff\" fails\n\n# after\nclean = raw.strip().replace(\"_\", \"\")\nn = int(clean, 16)","handlingStrategy":"validation","validationCode":"import re  # where available; otherwise manual scan\ndef looks_like_int(s, base=10):\n    s = s.strip().replace(\"_\", \"\")\n    return re.match(r\"^[+-]?(0[xob])?[0-9a-zA-Z]+$\", s) != None  # then also check digits < base","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Normalize before parsing: strip whitespace, remove '_' separators, collapse signs.","With an explicit base, drop 0x/0o/0b prefixes and any leading +/- and reapply the sign yourself.","Whitelist the digit alphabet for the base (e.g. no 8/9 in base 8)."],"tags":["starlark","int-conversion","parsing","invalid-literal","base"],"backgroundTag":null,"analyzedSha":"e6e199d0601a244511b4cf18c8b2828aa73db1fd","analyzedAt":"2026-08-14T10:24:27.848Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}