Skip to content

fix: throw JsonSyntaxException for null elements in AtomicIntegerArray - #3126

Closed
priyadarshnisundararajan wants to merge 1 commit into
google:mainfrom
priyadarshnisundararajan:fix/issue-3047-atomic-integer-array-null
Closed

priyadarshnisundararajan wants to merge 1 commit into
google:mainfrom
priyadarshnisundararajan:fix/issue-3047-atomic-integer-array-null

Conversation

@priyadarshnisundararajan

Copy link
Copy Markdown

What

new Gson().fromJson("[1,null,3]", AtomicIntegerArray.class) threw a raw IllegalStateException ("Expected an int but was NULL") instead of JsonSyntaxException.

Why

In TypeAdapters.ATOMIC_INTEGER_ARRAY.read(), in.nextInt() was called directly inside a try block that only caught NumberFormatException. A JSON null token makes nextInt() throw IllegalStateException, which escaped uncaught.

Fix

Mirrors the already-merged AtomicLongArray fix from PR #3038: peek for a NULL token before reading each element and throw JsonSyntaxException("null is not a valid AtomicIntegerArray element; at path " + in.getPath()). in.getPath() (not getPreviousPath()) is used because the null token has not been consumed yet, which yields the identical $[1] path format as the AtomicLongArray fix.

Fixes #3047

Tests

Added testAtomicIntegerArrayWithNullElement to JavaUtilConcurrentAtomicTest, asserting JsonSyntaxException with message null is not a valid AtomicIntegerArray element; at path $[1] (mirrors testAtomicLongArrayWithNullElement). Ran the full JavaUtilConcurrentAtomicTest class: 9 tests, all passing (8 existing + 1 new). Maven could not resolve build extensions over this network (TLS interception on repo.maven.apache.org), so tests were compiled and run directly with javac/java 21 using jars fetched from Maven Central (junit 4.13.2, truth 1.4.5, guava 33.7.0-jre + transitive deps).

The ATOMIC_INTEGER_ARRAY type adapter called in.nextInt() directly and only
caught NumberFormatException, so a null array element escaped as a raw
IllegalStateException. Mirror the AtomicLongArray fix from google#3038: peek for a
NULL token and throw JsonSyntaxException with the element path.

Fixes google#3047
@google-cla

google-cla Bot commented Sep 21, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@priyadarshnisundararajan
priyadarshnisundararajan marked this pull request as ready for review September 21, 2026 22:26
@Marcono1234

Copy link
Copy Markdown
Contributor

Thanks for the pull request! However, have you verified that the issue you are trying to fix here even existed in the first place, and have you read the discussion in #3047?

Even the author of that issue confirmed in the end that the claimed issue does not actually exist: #3047 (comment)

@priyadarshnisundararajan

Copy link
Copy Markdown
Author

You're right, thank you for checking. I verified against current main and the reported crash does not reproduce: Gson.fromJson(JsonReader, TypeToken) already catches the IllegalStateException and wraps it as JsonSyntaxException, so there is no uncaught exception here. Closing this as invalid, apologies for the noise.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AtomicIntegerArray deserializer crashes on JSON null (IllegalStateException) — symmetric fix to #3038

2 participants