Skip to content

When copying the old buffer value, if the old value is undefined (o… - #321

Merged
llsansun merged 2 commits into
cocos:v4.0.0from
llsansun:terrain_fix
Sep 2, 2026
Merged

When copying the old buffer value, if the old value is undefined (o…#321
llsansun merged 2 commits into
cocos:v4.0.0from
llsansun:terrain_fix

Conversation

@llsansun

@llsansun llsansun commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

Re: #

Changelog

error:
TypeError: Cannot read properties of undefined (reading 'tileSize')
at TerrainBlock.update (terrain.ts:539:46)
at Terrain.update (terrain.ts:1752:29)
at component-scheduler.ts:299:35
at ReusableInvoker._invoke (component-scheduler.ts:251:13)
at ReusableInvoker.invoke (component-scheduler.ts:213:14)
at ComponentScheduler.updatePhase (component-scheduler.ts:504:28)
at Director.tick (director.ts:780:37)
at Game._updateCallback (game.ts:1086:22)
at Pacer.updateCallback [as _ccprivate$_callback] (pacer-web.js:4:1650)

*When terrain.rebuild(info) is called while the node is inactive, _layerBuffer is still an empty array []. When _rebuildLayerBuffer copies data from this empty array, undefined overwrites the pre‑initialized -1 values in the new buffer. Later, TerrainBlock.update() receives undefined as the layer ID. getLayer(undefined) returns undefined (not null), which bypasses the !== null check, eventually causing a crash when accessing undefined.tileSize.

Two fixes:

  1. _rebuildLayerBuffer (root‑cause fix): When copying old buffer values, if the old value is undefined (out‑of‑bounds access on an empty array), preserve the default -1 instead of writing undefined.

  2. getLayer (defensive fix): Return null when _layerList[id] is undefined, and filter cases where id === undefined. Ensure the caller’s !== null check works correctly for interception.


Continuous Integration

This pull request:

  • needs automatic test cases check.

    Manual trigger with @cocos-robot run test cases afterward.

  • does not change any runtime related code or build configuration

    If any reviewer thinks the CI checks are needed, please uncheck this option, then close and reopen the issue.


Compatibility Check

This pull request:

  • changes public API, and have ensured backward compatibility with deprecated features.
  • affects platform compatibility, e.g. system version, browser version, platform sdk version, platform toolchain, language version, hardware compatibility etc.
  • affects file structure of the build package or build configuration which requires user project upgrade.
  • introduces breaking changes, please list all changes, affected features and the scope of violation.

…ut‑of‑bounds access to an empty array), preserve the default value of `-1` instead of writing `undefined`.
@github-actions

github-actions Bot commented Sep 2, 2026

Copy link
Copy Markdown

Code Size Check Report

Wechat (WASM) Before After Diff
2D Empty (legacy pipeline) 1015436 bytes 1015436 bytes ✅ 0 bytes
2D All (legacy pipeline) 2683579 bytes 2683579 bytes ✅ 0 bytes
2D All (new pipeline) 2775832 bytes 2775832 bytes ✅ 0 bytes
(2D + 3D) All 10033405 bytes 10033446 bytes ⚠️ +41 bytes
Web (WASM + ASMJS) Before After Diff
(2D + 3D) All 16869823 bytes 16869864 bytes ⚠️ +41 bytes

Interface Check Report

This pull request does not change any public interfaces !

Comment thread cocos/terrain/terrain.ts Outdated
}

return this._layerList[id];
return this._layerList[id] ?? null;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

polyfill of ?? will increase code size a bit.

The following code might have smaller size.

const v = this._layerList[id];
return v !== undefined ? v | null;

@star-e

star-e commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

@cocos-robot run test cases

@llsansun
llsansun merged commit 6722aac into cocos:v4.0.0 Sep 2, 2026
14 checks passed
@llsansun
llsansun deleted the terrain_fix branch September 2, 2026 10:04
@github-actions

github-actions Bot commented Sep 2, 2026

Copy link
Copy Markdown

@llsansun, Please check the result of run test cases:

Task Details

Platform build boot runned crashScene FailScene
web-mobile PASS PASS FAIL frame-anim
ios PASS PASS PASS
mac PASS PASS PASS

@github-actions

github-actions Bot commented Sep 2, 2026

Copy link
Copy Markdown

@llsansun, Please check the result of run test cases:

Task Details

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.

3 participants