Skip to content

Guard against a null chunk in GenPonds' pond search - #52

Open
Zaldaryon wants to merge 1 commit into
anegostudios:masterfrom
Zaldaryon:fix/genponds-null-chunk-guard
Open

Guard against a null chunk in GenPonds' pond search#52
Zaldaryon wants to merge 1 commit into
anegostudios:masterfrom
Zaldaryon:fix/genponds-null-chunk-guard

Conversation

@Zaldaryon

Copy link
Copy Markdown

GenPonds.genPond looks up the chunk it is about to write into, falls back to a second lookup when the first misses, and then calls Unpack() on the result without checking it:

chunk = (IServerChunk)blockAccessor.GetChunk(curChunkX, pondYPos / chunksize, curChunkZ);
if (chunk == null) chunk = api.WorldManager.GetChunk(curChunkX, pondYPos / chunksize, curChunkZ);
chunk.Unpack();          // NRE if the fallback missed too

The fallback can return null, so this throws a NullReferenceException when a neighbouring column is absent or being unloaded while the pond search walks into it. It is reachable with MaxWorldgenThreads above 1, where columns come and go around the one currently generating.

The sibling lookup six lines below already handles the same situation by bailing out:

chunkOneBlockBelow = ((IServerChunk)blockAccessor.GetChunk(curChunkX, (pondYPos - 1) / chunksize, curChunkZ));
if (chunkOneBlockBelow == null) return;
chunkOneBlockBelow.Unpack();

This adds the same guard to the first lookup, so the two are consistent. One line, and it only changes behaviour on a path that currently throws.

Found while running world generation with several worldgen threads. Related, though a separate bug: anegostudios/VintageStory-Issues#9871.

The fallback lookup on the line above can return null, and the very next
statement calls Unpack() on it unconditionally, so the pond search NREs when a
neighbouring column is absent or mid-unload. This is reachable with
MaxWorldgenThreads above 1, where columns are loaded and unloaded around the
searching column while it runs.

The sibling lookup six lines below already handles exactly this by returning, so
this just makes the two consistent.
@Zaldaryon
Zaldaryon force-pushed the fix/genponds-null-chunk-guard branch from fa2d570 to c3ce029 Compare July 27, 2026 19:26
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.

1 participant