18 Commits

Author SHA1 Message Date
Syoyo Fujita
34a166cdac Complete freestanding v3 C JSON conversion tests 2026-06-01 16:11:22 +09:00
Syoyo Fujita
0e3043f3e9 Harden and optimize v3 C parser 2026-05-31 22:20:46 +09:00
Syoyo Fujita
a8fb48fa91 Harden v3 C parser against untrusted glTF input
Threat model: parser is intended for server-side processing of attacker-
supplied glTF/GLB. Two adversarial review rounds plus a 1-hour libFuzzer
run (4 workers, ASan+UBSan, ~420M execs total, zero new artifacts) drove
this set of fixes. Concrete PoCs in tests/v3/security/ confirmed each
issue was exploitable on the prior code.

Path traversal (CRITICAL): tg3__load_external_file concatenated base_dir
with the JSON-supplied URI verbatim. A glTF with
"uri":"../../../tmp/secret" successfully loaded the file from outside
base_dir (verified by FNV64 match). New tg3__uri_is_safe rejects empty,
NUL, leading / or \\, Windows drive prefixes, and any '..' segment.
Path-buffer length checks switched to saturating subtraction so 32-bit
size_t cannot wrap.

Sign-coercion in byteStride: int32_t -1 was cast directly to uint32_t,
producing 0xFFFFFFFF and propagating into downstream count*stride math.
Restrict to glTF spec range: 0 (tightly packed) or [4, 252].

Index validation: parsed int32 index fields (accessor.bufferView,
primitive.indices/material/attributes, node.mesh/skin/camera/light,
scene.nodes[], skin.joints[], animation channel/sampler refs, MSFT_lod
ids, KHR_audio emitter/source refs, etc.) were stored unchecked. New
tg3__validate_indices walks every index field and returns
TG3_ERR_INVALID_INDEX on out-of-range. Gated by
tg3_parse_options.validate_indices, defaulting to 1.

Use-after-free on parse failure (PRE-EXISTING, surfaced by ASan during
fix verification): tg3_parse and tg3_parse_glb destroyed model->arena_
on error paths, but error messages on the user-facing tg3_error_stack
were arena-allocated. Any caller reading errors.entries[i].message
after parse failure read freed memory. tg3_model_free is now sole arena
owner; arena lives across error paths so messages stay valid until the
caller frees the model.

Other fixes:
- tg3_parse_glb: hoist tg3__model_init before header parse so callers
  can safely tg3_model_free on header failure.
- tg3__parse_primitive morph targets: when arena alloc returns NULL,
  pair with target_counts[ti]=0 so validators do not deref.
- Defensive 'if (!tarr) continue' in the morph-target validator loop.
- New Security Considerations block in tiny_gltf_v3.h documents the
  threat model, default-on validation, fs-callback contract, and error
  message lifetime.

Verification: 13 internal tests in tester_v3_c (incl. 7 new security
regressions covering path traversal absolute and relative, fs-callback
no-call assertion, byteStride wrap, OOB index, opt-in raw mode, ext
fields, and arena-message lifetime), 134/134 Khronos sample models
match v1 ground truth digest, 1-hour ASan+UBSan fuzz on the final code
clean.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-09 14:02:34 +09:00
Syoyo Fujita
85441bbe19 Add pure-C TinyGLTF v3 runtime
Introduce a C-first TinyGLTF v3 runtime in tiny_gltf_v3.c with a pure-C JSON backend, hook the public header to the new implementation, and add CMake/test coverage for parse and write round-trips.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-05-06 04:28:41 +09:00
Syoyo Fujita
9422613562 Modify copyright notice in tiny_gltf_v3.h
Updated copyright year and authorship information.
2026-03-25 02:35:54 +09:00
Syoyo Fujita
ad316367b9 Merge pull request #541 from syoyo/copilot/sub-pr-537-yet-again
Fix `tg3__arena_strdup` conflating empty strings with absent strings
2026-03-21 07:04:10 +09:00
Syoyo Fujita
1f15c2d140 Merge pull request #538 from syoyo/copilot/sub-pr-537
Fix tg3_writer allocation: replace calloc/free with new/delete
2026-03-21 06:38:06 +09:00
Syoyo Fujita
1d5e721a24 Merge pull request #542 from syoyo/copilot/sub-pr-537-one-more-time
Guard TINYGLTF3_IMPLEMENTATION against C translation units
2026-03-21 06:37:42 +09:00
copilot-swe-agent[bot]
5e0c5b9ada Fix tg3__arena_strdup to distinguish empty strings from absent strings
Co-authored-by: syoyo <18676+syoyo@users.noreply.github.com>
Agent-Logs-Url: https://github.com/syoyo/tinygltf/sessions/445ab61b-4294-45e6-8faf-4f2fc8dfe369
2026-03-20 21:21:35 +00:00
copilot-swe-agent[bot]
03b9db782e Add C++ compilation guard for TINYGLTF3_IMPLEMENTATION
Co-authored-by: syoyo <18676+syoyo@users.noreply.github.com>
Agent-Logs-Url: https://github.com/syoyo/tinygltf/sessions/9d34bfe8-6b91-44f8-aedc-adb3bfeadf84
2026-03-20 21:21:26 +00:00
Syoyo Fujita
c99e713fab Merge pull request #539 from syoyo/copilot/sub-pr-537-again
Use `__VA_OPT__` for variadic comma elision in C++20, `##__VA_ARGS__` fallback for C++17
2026-03-21 06:19:35 +09:00
copilot-swe-agent[bot]
b76cf7aa21 Replace ##__VA_ARGS__ with portable TG3__COMMA_VA_ARGS helper (C++17/C++20)
Co-authored-by: syoyo <18676+syoyo@users.noreply.github.com>
Agent-Logs-Url: https://github.com/syoyo/tinygltf/sessions/a7105342-8673-4241-b727-29026461cc67
2026-03-20 20:42:17 +00:00
copilot-swe-agent[bot]
946c5a2d9b Fix tg3_writer allocation: use new/delete instead of calloc/free
Co-authored-by: syoyo <18676+syoyo@users.noreply.github.com>
Agent-Logs-Url: https://github.com/syoyo/tinygltf/sessions/c2a9ce6c-1c60-4925-b67b-e5ad2ffe1cd2
2026-03-20 20:39:43 +00:00
Syoyo Fujita
f7bd377a69 Update tiny_gltf_v3.h
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2026-03-21 05:36:13 +09:00
Syoyo Fujita
5d6984b9fd Update tiny_gltf_v3.h
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2026-03-21 05:35:53 +09:00
Syoyo Fujita
2c7bf2c932 Fix fuzzer-found bugs, add libFuzzer harness for v3
Add tests/v3/fuzzer/ with libFuzzer harness covering all four parse
paths (auto-detect, JSON, GLB, float32 mode) with ASan+UBSan.

Fix two bugs found by 10+ hours of fuzzing (~23M iterations):

1. UB: (int64_t)inf in cj_parse_number when extreme exponents like
   22222222e222222 produce infinity. Add cj_dbl_to_i64() that clamps
   inf/NaN/out-of-range values before casting.

2. Null deref in tg3__parse_string when glTF array elements are not
   JSON objects (e.g. "scenes": [[3]]). Add is_object() validation
   in TG3__PARSE_ARRAY_SIMPLE and TG3__PARSE_ARRAY_IDX macros.

Verified clean: 5.8M additional runs with zero crashes after fixes.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-21 04:10:25 +09:00
Syoyo Fujita
2aeac50277 Add fast float parser and benchmark float-heavy scene
Replace strtod() with Clinger's fast path in tinygltf_json.h for ~1.5x
faster JSON float parsing. The new parser accumulates all digits into a
uint64 mantissa and uses exact power-of-10 tables for conversion,
avoiding locale-dependent strtod for ~99% of JSON float values.

Add optional float32 parse mode (parse_float32 option) that parses JSON
floats at single precision — fewer significant digits needed, wider fast
path range. Breaks strict double-precision conformance but sufficient
for glTF data which is typically single-precision.

Benchmark additions:
- gen_synthetic: add float_heavy preset (~500MB ASCII float JSON)
- bench_v3: add --float32 flag for float32 parse mode benchmarking

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-20 09:00:30 +09:00
Syoyo Fujita
78f4a5cfe8 Add tinygltf v3 single-header C API (tiny_gltf_v3.h)
Ground-up C-centric rewrite of tinygltf with pure C POD structs,
arena-based memory management, structured error reporting, streaming
callbacks, and no STL dependency in the public API. Uses tinygltf_json.h
as the sole JSON backend.

Includes complete parser (JSON + GLB), writer (JSON + GLB), streaming
writer, C++ RAII wrappers, and C++20 coroutine facade. Verified with
Cube.gltf and Fox.glb parse/write round-trips.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-20 03:31:05 +09:00