Skip to content

Commit 1eac243

Browse files
syoyoclaude
andcommitted
Fix signed integer overflow UB in opt_tryParseDouble exponent parsing
The exponent accumulation loop `exponent *= 10` could overflow int for inputs like "1.0e9999999999", which is undefined behavior in C++. Clamped to 0x7FFFFFF (~134M) before the multiply — values above 308 already exceed double range, so further digits are irrelevant. Only affects TINYOBJLOADER_DISABLE_FAST_FLOAT builds (the hand-written float parser fallback). Verified with UBSan fuzzing (15K iterations). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 19f8f03 commit 1eac243

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

tiny_obj_loader.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10159,8 +10159,12 @@ static bool opt_tryParseDouble(const char *s, const char *s_end,
1015910159
read = 0;
1016010160
end_not_reached = (curr != s_end);
1016110161
while (end_not_reached && TINYOBJ_OPT_IS_DIGIT(*curr)) {
10162-
exponent *= 10;
10163-
exponent += static_cast<int>(*curr - '0');
10162+
// Clamp to avoid signed integer overflow (UB). |exponent| > 308
10163+
// already exceeds double range, so further digits are irrelevant.
10164+
if (exponent < 0x7FFFFFF) {
10165+
exponent *= 10;
10166+
exponent += static_cast<int>(*curr - '0');
10167+
}
1016410168
curr++;
1016510169
read++;
1016610170
end_not_reached = (curr != s_end);

0 commit comments

Comments
 (0)