From 5dfa17d14bd165f40c63d30301f2f3b502df5de3 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 18 Mar 2026 20:16:01 +0000 Subject: [PATCH] Final review: fix stray *out_len=len UB, add get primary template for size_t Co-authored-by: syoyo <18676+syoyo@users.noreply.github.com> --- tinygltf_json.h | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/tinygltf_json.h b/tinygltf_json.h index 8451014..0e7e9e1 100644 --- a/tinygltf_json.h +++ b/tinygltf_json.h @@ -1195,6 +1195,23 @@ template<> inline std::string tinygltf_json::get() const { return std::string(); } +/* Primary template for any T not explicitly specialised (e.g. size_t on + * platforms where it is a distinct type from all of the above, such as + * macOS 64-bit where uint64_t=unsigned long long but size_t=unsigned long). + * Falls back to a static_cast from the stored integer or floating-point value. + * For unsigned T: negative integer values produce 0 rather than wrapping. */ +template +inline T tinygltf_json::get() const { + if (type_ == CJ_INT) { + /* Guard unsigned types against sign-extension of negative values */ + if ((T)(-1) > (T)(0) && i_ < 0) return (T)(0); + return static_cast(i_); + } + if (type_ == CJ_REAL) return static_cast(d_); + if (type_ == CJ_BOOL) return static_cast(b_); + return T(); +} + /* ====================================================================== * PARSER (C-style iterative, explicit frame stack) * @@ -1292,7 +1309,6 @@ static void cj_parse_string_to(cj_parse_ctx *ctx, char **out_str, *out_str = cj_unescape_string(ctx->cur, scan, out_len); if (!*out_str) { cj_ctx_error(ctx, "string unescape failed"); } ctx->cur = scan + 1; - *out_len = len; return; } /* Control char (< 0x20) - treat as parse error (invalid JSON) */