Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

fix: three foundational audit follow-ups

esengine6b1faa87137c
diff --git a/sdk/src/material.ts b/sdk/src/material.ts
index 4efb0fd80..9a980e82e 100644
--- a/sdk/src/material.ts
+++ b/sdk/src/material.ts
@@ -384,6 +384,9 @@ export const Material = {
let materialCallbackRegistered = false;
let uniformBuffer: number = 0;
const UNIFORM_BUFFER_SIZE = 16384;
+// Cap the cache so apps that generate many unique uniform names (e.g. per-
+// instance shader variants) don't leak Uint8Arrays for the whole session.
+const ENCODED_NAME_CACHE_LIMIT = 256;
const encodedNameCache = new Map<string, Uint8Array>();
let encoder: TextEncoder | null = null;
@@ -411,6 +414,11 @@ function serializeUniforms(uniforms: Map<string, UniformValue>): { ptr: number;
if (!nameBytes) {
if (!encoder) encoder = new TextEncoder();
nameBytes = encoder.encode(name);
+ if (encodedNameCache.size >= ENCODED_NAME_CACHE_LIMIT) {
+ // Evict the oldest entry (Map preserves insertion order).
+ const oldest = encodedNameCache.keys().next().value;
+ if (oldest !== undefined) encodedNameCache.delete(oldest);
+ }
encodedNameCache.set(name, nameBytes);
}
const nameLen = nameBytes.length;
diff --git a/src/esengine/math/Math.hpp b/src/esengine/math/Math.hpp
index 305f34588..2eb734089 100644
--- a/src/esengine/math/Math.hpp
+++ b/src/esengine/math/Math.hpp
@@ -243,14 +243,18 @@ inline void decompose(const glm::mat4& matrix, glm::vec3& position, glm::vec3& r
scale.y = glm::length(glm::vec3(matrix[1]));
scale.z = glm::length(glm::vec3(matrix[2]));
- // Remove scale from matrix for rotation extraction
+ // Guard zero-scale axes: dividing by 0 produces NaN/Inf that propagates
+ // silently into Euler angles and corrupts every downstream transform.
+ const float sx = scale.x > EPSILON ? scale.x : EPSILON;
+ const float sy = scale.y > EPSILON ? scale.y : EPSILON;
+ const float sz = scale.z > EPSILON ? scale.z : EPSILON;
+
glm::mat3 rotMatrix(
- glm::vec3(matrix[0]) / scale.x,
- glm::vec3(matrix[1]) / scale.y,
- glm::vec3(matrix[2]) / scale.z
+ glm::vec3(matrix[0]) / sx,
+ glm::vec3(matrix[1]) / sy,
+ glm::vec3(matrix[2]) / sz
);
- // Extract Euler angles from rotation matrix
rotation.x = std::atan2(rotMatrix[2][1], rotMatrix[2][2]);
rotation.y = std::atan2(-rotMatrix[2][0],
std::sqrt(rotMatrix[2][1] * rotMatrix[2][1] + rotMatrix[2][2] * rotMatrix[2][2]));
@@ -279,14 +283,17 @@ inline void decompose(const glm::mat4& matrix, glm::vec3& position, glm::quat& r
scale.y = glm::length(glm::vec3(matrix[1]));
scale.z = glm::length(glm::vec3(matrix[2]));
- // Build rotation matrix without scale
+ // Guard zero-scale axes to avoid NaN/Inf in the quaternion.
+ const float sx = scale.x > EPSILON ? scale.x : EPSILON;
+ const float sy = scale.y > EPSILON ? scale.y : EPSILON;
+ const float sz = scale.z > EPSILON ? scale.z : EPSILON;
+
glm::mat3 rotMatrix(
- glm::vec3(matrix[0]) / scale.x,
- glm::vec3(matrix[1]) / scale.y,
- glm::vec3(matrix[2]) / scale.z
+ glm::vec3(matrix[0]) / sx,
+ glm::vec3(matrix[1]) / sy,
+ glm::vec3(matrix[2]) / sz
);
- // Convert rotation matrix to quaternion
rotation = glm::quat_cast(rotMatrix);
}
diff --git a/src/esengine/resource/HotReloadManager.hpp b/src/esengine/resource/HotReloadManager.hpp
index e18d6455f..60ee2effe 100644
--- a/src/esengine/resource/HotReloadManager.hpp
+++ b/src/esengine/resource/HotReloadManager.hpp
@@ -188,19 +188,24 @@ void HotReloadManager::watch(Handle<T> handle, const std::string& path,
return;
}
- std::lock_guard<std::mutex> lock(mutex_);
+ {
+ std::lock_guard<std::mutex> lock(mutex_);
- if (watchedFiles_.find(path) != watchedFiles_.end()) {
- return;
- }
+ if (watchedFiles_.find(path) != watchedFiles_.end()) {
+ return;
+ }
- WatchEntry entry;
- entry.path = path;
- entry.type = std::type_index(typeid(T));
- entry.reloadFn = std::move(reloadFn);
+ WatchEntry entry;
+ entry.path = path;
+ entry.type = std::type_index(typeid(T));
+ entry.reloadFn = std::move(reloadFn);
- watchedFiles_[path] = std::move(entry);
+ watchedFiles_[path] = std::move(entry);
+ }
+ // Register outside the lock: if a backend delivers its first notify
+ // synchronously during registration, onFileChanged would otherwise
+ // deadlock trying to re-acquire mutex_.
FileSystem::watchFile(path, [this](const std::string& changedPath) {
onFileChanged(changedPath);
});