{"id":"195e40ea4def0dca","repo":"google/gson","slug":"too-big-for-an-int-x","errorCode":null,"errorMessage":"Too big for an int: \" + x","messagePattern":"Too big for an int: \" \\+ x","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"gson/src/main/java/com/google/gson/internal/bind/TypeAdapters.java","lineNumber":910,"sourceCode":"\n        @Override\n        long[] integerValues(Calendar calendar) {\n          return new long[] {\n            calendar.get(Calendar.YEAR),\n            calendar.get(Calendar.MONTH),\n            calendar.get(Calendar.DAY_OF_MONTH),\n            calendar.get(Calendar.HOUR_OF_DAY),\n            calendar.get(Calendar.MINUTE),\n            calendar.get(Calendar.SECOND)\n          };\n        }\n      };\n\n  // TODO: update this when we are on at least Android API Level 24.\n  private static int toIntExact(long x) {\n    int i = (int) x;\n    if (i != x) {\n      throw new IllegalArgumentException(\"Too big for an int: \" + x);\n    }\n    return i;\n  }\n\n  public static final TypeAdapterFactory CALENDAR_FACTORY =\n      newFactoryForMultipleTypes(Calendar.class, GregorianCalendar.class, CALENDAR);\n\n  public static final TypeAdapter<Locale> LOCALE =\n      new TypeAdapter<Locale>() {\n        @Override\n        public Locale read(JsonReader in) throws IOException {\n          if (in.peek() == JsonToken.NULL) {\n            in.nextNull();\n            return null;\n          }\n          String locale = in.nextString();\n          StringTokenizer tokenizer = new StringTokenizer(locale, \"_\");\n          String language = null;","sourceCodeStart":892,"sourceCodeEnd":928,"githubUrl":"https://github.com/google/gson/blob/8b8628c65699bc4421696183c62ae0c1b9b281dc/gson/src/main/java/com/google/gson/internal/bind/TypeAdapters.java#L892-L928","documentation":"Gson's Calendar/IntegerFieldsTypeAdapter reads year/month/day/hour/minute/second as long values then narrows them to int via the internal toIntExact helper. If a field exceeds Integer.MAX_VALUE or is below Integer.MIN_VALUE, toIntExact throws IllegalArgumentException 'Too big for an int: <x>'. This guards against silent truncation of absurdly large numeric fields.","triggerScenarios":"Deserializing a Calendar (legacy mode) where one of the six integer fields (year/month/day/hour/minute/second) is a JSON number outside the int range, e.g. year 5000000000.","commonSituations":"Garbage or sentinel values from upstream, epoch-millisecond accidentally placed in the year field, or non-Gregorian calendars producing out-of-range components.","solutions":["Inspect the offending field reported in the exception and correct the source data.","Validate/sanitize the JSON before deserialization, clamping or rejecting out-of-range numbers.","Switch to java.time types (Instant, ZonedDateTime) with a proper date-time adapter instead of the legacy Calendar representation.","Register a custom TypeAdapter<Calendar> that interprets the numeric fields differently."],"exampleFix":"// before\nCalendar cal = gson.fromJson(\"{\\\"year\\\":1700000000000,\\\"month\\\":0,...}\", Calendar.class);\n\n// after: use java.time + ISO format\nInstant inst = gson.fromJson(\"\\\"2024-01-01T00:00:00Z\\\"\", Instant.class);","handlingStrategy":"validation","validationCode":"boolean calendarFieldsInRange(long[] v) {\n  for (long x : v) if (x < Integer.MIN_VALUE || x > Integer.MAX_VALUE) return false;\n  return true;\n}","typeGuard":"static boolean isIntRange(long x) { return x >= Integer.MIN_VALUE && x <= Integer.MAX_VALUE; }","tryCatchPattern":"try {\n  Calendar c = gson.fromJson(json, Calendar.class);\n} catch (IllegalArgumentException e) {\n  // one of the six fields overflowed int; sanitize and retry, or reject\n}","preventionTips":["Prefer java.time over legacy Calendar.","Validate numeric ranges in the source data before mapping to Calendar.","Treat epoch-millis-in-year-field as a producer bug, not a Gson bug."],"tags":["gson","deserialization","calendar","integer-overflow","json"],"analyzedSha":"8b8628c65699bc4421696183c62ae0c1b9b281dc","analyzedAt":"2026-08-04T19:12:22.202Z","schemaVersion":2}