{"record":{"id":"04f17c23910a42fa","repo":"ReactiveX/RxJava","slug":"integer-overflow","errorCode":null,"errorMessage":"Integer overflow","messagePattern":"Integer overflow","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/io/reactivex/rxjava4/core/Observable.java","lineNumber":2836,"sourceCode":"     * @see <a href=\"http://reactivex.io/documentation/operators/range.html\">ReactiveX operators documentation: Range</a>\n     * @see #rangeLong(long, long)\n     * @see #intervalRange(long, long, long, long, TimeUnit)\n     */\n    @CheckReturnValue\n    @SchedulerSupport(SchedulerSupport.NONE)\n    @NonNull\n    public static Observable<Integer> range(int start, int count) {\n        if (count < 0) {\n            throw new IllegalArgumentException(\"count >= 0 required but it was \" + count);\n        }\n        if (count == 0) {\n            return empty();\n        }\n        if (count == 1) {\n            return just(start);\n        }\n        if ((long)start + (count - 1) > Integer.MAX_VALUE) {\n            throw new IllegalArgumentException(\"Integer overflow\");\n        }\n        return RxJavaPlugins.onAssembly(new ObservableRange(start, count));\n    }\n\n    /**\n     * Returns an {@code Observable} that emits a sequence of {@link Long}s within a specified range.\n     * <p>\n     * <img width=\"640\" height=\"195\" src=\"https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/rangeLong.png\" alt=\"\">\n     * <dl>\n     *  <dt><b>Scheduler:</b></dt>\n     *  <dd>{@code rangeLong} does not operate by default on a particular {@link Scheduler}.</dd>\n     * </dl>\n     *\n     * @param start\n     *            the value of the first {@code Long} in the sequence\n     * @param count\n     *            the number of sequential {@code Long}s to generate\n     * @return the new {@code Observable} instance","sourceCodeStart":2818,"sourceCodeEnd":2854,"githubUrl":"https://github.com/ReactiveX/RxJava/blob/a8ab5356143f37a8f1dd6d76c79191bec5fa343b/src/main/java/io/reactivex/rxjava4/core/Observable.java#L2818-L2854","documentation":"Thrown by Observable.range when the inclusive endpoint start + (count - 1) exceeds Integer.MAX_VALUE. Because the operator emits ints, the range must fit in the int domain; the library casts to long to detect overflow safely and fails fast rather than wrapping.","triggerScenarios":"Calling Observable.range(start, count) where (long)start + (count - 1) > Integer.MAX_VALUE. Happens with high positive start values combined with a large count.","commonSituations":"Generating ranges that cross the top of the int domain; paging counters near Integer.MAX_VALUE; unvalidated computed ranges.","solutions":["Reduce start and/or count so the inclusive end stays <= Integer.MAX_VALUE.","Validate up front: boolean overflow = (long)start + (count - 1) > Integer.MAX_VALUE.","Switch to Observable.rangeLong if you need the long domain."],"exampleFix":"// before\nObservable.range(Integer.MAX_VALUE - 10, 100);\n\n// after\n// option A: clamp into the int domain\nint start = Integer.MAX_VALUE - 10;\nint count = Math.min(100, Integer.MAX_VALUE - start + 1);\nObservable.range(start, count);\n// option B: move to the long domain\nObservable.rangeLong((long)Integer.MAX_VALUE - 10, 100L);","handlingStrategy":"validation","validationCode":"if ((long)start + (count - 1) > Integer.MAX_VALUE) {\n    // reduce count or switch to rangeLong\n    count = Integer.MAX_VALUE - start + 1;\n}","typeGuard":null,"tryCatchPattern":"try {\n    return Observable.range(start, count);\n} catch (IllegalArgumentException e) {\n    // fall back to long-domain range\n    return Observable.rangeLong((long)start, (long)count);\n}","preventionTips":["Cast to long when validating the inclusive endpoint: (long)start + (count - 1).","Avoid seeding start near Integer.MAX_VALUE.","Prefer rangeLong when magnitudes may exceed the int domain."],"tags":["rxjava","observable","range","overflow","arithmetic"],"backgroundTag":null,"analyzedSha":"a8ab5356143f37a8f1dd6d76c79191bec5fa343b","analyzedAt":"2026-08-13T23:25:30.069Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}