{"record":{"id":"491f025c1cdbd372","repo":"bazelbuild/bazel","slug":"empty-string","errorCode":null,"errorMessage":"empty string","messagePattern":"empty string","errorType":"exception","errorClass":"NumberFormatException","httpStatus":null,"severity":"error","filePath":"src/main/java/net/starlark/java/eval/StarlarkInt.java","lineNumber":108,"sourceCode":"   * Returns the StarlarkInt value that most closely approximates x.\n   *\n   * @throws IllegalArgumentException is x is not finite.\n   */\n  static StarlarkInt ofFiniteDouble(double x) {\n    return StarlarkFloat.finiteDoubleToIntExact(x);\n  }\n\n  /**\n   * Returns the int denoted by a literal string in the specified base, as if by the Starlark\n   * expression {@code int(s, base)}.\n   *\n   * @throws NumberFormatException if the input is invalid.\n   */\n  public static StarlarkInt parse(String s, int base) {\n    String stringForErrors = s;\n\n    if (s.isEmpty()) {\n      throw new NumberFormatException(\"empty string\");\n    }\n\n    // +/- prefix?\n    boolean isNegative = false;\n    char c = s.charAt(0);\n    if (c == '+') {\n      s = s.substring(1);\n    } else if (c == '-') {\n      s = s.substring(1);\n      isNegative = true;\n    }\n\n    String digits = s;\n\n    // 0b 0o 0x prefix?\n    if (s.length() > 1 && s.charAt(0) == '0') {\n      int prefixBase = 0;\n      c = s.charAt(1);","sourceCodeStart":90,"sourceCodeEnd":126,"githubUrl":"https://github.com/bazelbuild/bazel/blob/e6e199d0601a244511b4cf18c8b2828aa73db1fd/src/main/java/net/starlark/java/eval/StarlarkInt.java#L90-L126","documentation":"StarlarkInt.parse (backing int(s, base)) rejects the empty string immediately with NumberFormatException('empty string'). The host then typically converts this into an EvalException like int(): <input>. The parser cannot infer any value from zero characters, unlike len-based defaults a caller might imagine.","triggerScenarios":"int(\"\"), int(\"\", 16), or int(s.strip()) where s is whitespace-only or an unset string attribute; parsing empty fields from split lines (\"a,,b\".split(\",\")).","commonSituations":"Parsing CSV/TSV or config lines with optional numeric fields; calling .strip() on user input before int() so \"  \" becomes \"\"; defaults like \"\" on string attributes that later must be numeric.","solutions":["Check for empty/blank before converting: if s.strip(): n = int(s) else: n = 0.","Fix upstream producers so numeric fields are never empty (emit \"0\").","Treat blank as a sentinel explicitly instead of relying on the parser."],"exampleFix":"# before\nport = int(raw_port_field)  # field may be \"\"\n\n# after\nport = int(raw_port_field) if raw_port_field.strip() else 0","handlingStrategy":"validation","validationCode":"def parse_int(s, base=10, default=None):\n    s = s.strip()\n    if not s:\n        if default == None:\n            fail(\"empty string passed to int()\")\n        return default\n    return int(s, base)","typeGuard":"def is_parseable_int(s):\n    return len(s.strip()) > 0","tryCatchPattern":null,"preventionTips":["Strip and check for empty before int().","Make producers emit \"0\" instead of \"\" for optional numeric fields."],"tags":["starlark","int-conversion","parsing","empty-string"],"backgroundTag":null,"analyzedSha":"e6e199d0601a244511b4cf18c8b2828aa73db1fd","analyzedAt":"2026-08-14T10:24:27.848Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}