{"record":{"id":"db4f044caaae093c","repo":"google/gson","slug":"invalid-number","errorCode":null,"errorMessage":"Invalid number: {}","messagePattern":"Invalid number: (.+?)","errorType":"exception","errorClass":"NumberFormatException","httpStatus":null,"severity":"error","filePath":"gson/src/main/java/com/google/gson/internal/bind/util/ISO8601Utils.java","lineNumber":344,"sourceCode":"   * @param value the string to parse\n   * @param beginIndex the start index for the integer in the string\n   * @param endIndex the end index for the integer in the string\n   * @return the int\n   * @throws NumberFormatException if the value is not a number\n   */\n  private static int parseInt(String value, int beginIndex, int endIndex)\n      throws NumberFormatException {\n    if (beginIndex < 0 || endIndex > value.length() || beginIndex > endIndex) {\n      throw new NumberFormatException(value);\n    }\n    // use same logic as in Integer.parseInt() but less generic we're not supporting negative values\n    int i = beginIndex;\n    int result = 0;\n    int digit;\n    if (i < endIndex) {\n      digit = Character.digit(value.charAt(i++), 10);\n      if (digit < 0) {\n        throw new NumberFormatException(\"Invalid number: \" + value.substring(beginIndex, endIndex));\n      }\n      result = -digit;\n    }\n    while (i < endIndex) {\n      digit = Character.digit(value.charAt(i++), 10);\n      if (digit < 0) {\n        throw new NumberFormatException(\"Invalid number: \" + value.substring(beginIndex, endIndex));\n      }\n      result *= 10;\n      result -= digit;\n    }\n    return -result;\n  }\n\n  /**\n   * Zero pad a number to a specified length\n   *\n   * @param buffer buffer to use for padding","sourceCodeStart":326,"sourceCodeEnd":362,"githubUrl":"https://github.com/google/gson/blob/310ac341f2f92a454b229bf21f70d2d18b2b6db7/gson/src/main/java/com/google/gson/internal/bind/util/ISO8601Utils.java#L326-L362","documentation":"Thrown by ISO8601Utils.parseInt when the FIRST character of a date/time numeric field is not a decimal digit (Character.digit returns < 0). parseInt parses substrings for year/month/day/hour/minute/second/fraction; a non-digit at the very first position triggers NumberFormatException(\"Invalid number: <substring>\"), which ISO8601Utils rewraps as a ParseException.","triggerScenarios":"A date component that should be numeric contains a non-digit at its start: e.g. month value starting with a letter, a separator character where a digit is expected, or an off-by-one parse where the position lands on a delimiter. Example: \"2020-0a-01\" or a malformed date where offsets into the string are wrong.","commonSituations":"Truncated date strings (too short, so offset walks into next field's delimiter); corrupted log timestamps; locale-specific separators ('/' instead of '-'); producers that left-pad with spaces instead of zeros; encoding issues inserting non-ASCII digits.","solutions":["Validate the date string against an ISO8601 regex before parsing (e.g. ^\\d{4}-\\d{2}-\\d{2}T...).","Fix the producer to emit well-formed ISO8601 with zero-padded numeric components.","Register a custom TypeAdapter<Date> tolerant to the actual format you receive.","Sanitize input (replace alternate separators, trim whitespace) before Gson."],"exampleFix":"// before\n// JSON: {\"at\":\"2020-0a-01T00:00:00Z\"} -> 'Invalid number: 0a'\n\n// after: validate at boundary\nString s = jsonValue;\nif (!s.matches(\"\\\\d{4}-\\\\d{2}-\\\\d{2}T\\\\d{2}:\\\\d{2}:\\\\d{2}(Z|[+-]\\\\d{2}:\\\\d{2})\")) {\n  throw new IllegalArgumentException(\"not ISO8601: \" + s);\n}\nDate d = gson.fromJson(json, MyType.class).at;","handlingStrategy":"validation","validationCode":"private static final Pattern ISO =\n  Pattern.compile(\"^\\\\d{4}-\\\\d{2}-\\\\d{2}T\\\\d{2}:\\\\d{2}:\\\\d{2}(\\\\.\\\\d+)?(Z|[+-]\\\\d{2}:\\\\d{2})$\");\nString raw = jsonNode.get(\"at\").getAsString();\nif (raw == null || !ISO.matcher(raw).matches()) {\n  throw new IllegalArgumentException(\"Malformed ISO8601 date (non-digit component): \" + raw);\n}","typeGuard":null,"tryCatchPattern":"try {\n  return gson.fromJson(json, Event.class);\n} catch (JsonSyntaxException e) {\n  if (e.getCause() instanceof ParseException && e.getCause().getMessage().contains(\"Invalid number\")) {\n    throw new IllegalArgumentException(\"Corrupt date field\", e);\n  }\n  throw e;\n}","preventionTips":["Validate ISO8601 with a strict regex before parsing.","Ensure producers zero-pad all numeric components (month, day, hour, minute, second).","Run fuzz/property tests with random character substitutions to ensure clean rejection.","Normalize separators and trim whitespace at ingestion."],"tags":["gson","date-parsing","iso8601","number-format","parse-exception"],"backgroundTag":null,"analyzedSha":"310ac341f2f92a454b229bf21f70d2d18b2b6db7","analyzedAt":"2026-08-10T02:58:47.455Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}