Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/demuxer/ChunkDemuxer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -626,8 +626,7 @@ double ChunkDemuxer::ieee80ToDouble(const uint8_t ieee80[10]) const {
}

// Convert to double
double result = static_cast<double>(mantissa) / (1ULL << 63);
result *= std::pow(2.0, static_cast<int>(exponent) - 16383);
double result = std::ldexp(static_cast<double>(mantissa), exponent - 16383 - 63);
Copy link

Copilot AI Apr 8, 2026

Choose a reason for hiding this comment

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

exponent is a uint16_t; the expression exponent - 16383 - 63 relies on integer promotions and may become unsigned arithmetic on platforms where int can’t represent all uint16_t values (e.g., 16-bit int), causing wraparound and incorrect scaling. To preserve the previous semantics and avoid implementation-defined conversions into std::ldexp’s int exponent parameter, cast exponent to int (or assign to an int local) before doing the bias/shift subtraction.

Suggested change
double result = std::ldexp(static_cast<double>(mantissa), exponent - 16383 - 63);
const int adjusted_exponent = static_cast<int>(exponent) - 16383 - 63;
double result = std::ldexp(static_cast<double>(mantissa), adjusted_exponent);

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

The removal of the explicit static_cast<int>(exponent) introduces a potential risk of unsigned underflow if exponent is an unsigned type that does not promote to int (e.g., uint32_t). In the original code, the cast ensured that the subtraction exponent - 16383 was performed using signed arithmetic. Without it, if exponent is uint32_t and its value is less than 16446, the result will wrap around to a very large positive value, causing std::ldexp to return infinity or overflow. Even if exponent is uint16_t, keeping the cast is safer and more explicit about the intention to perform signed arithmetic.

Suggested change
double result = std::ldexp(static_cast<double>(mantissa), exponent - 16383 - 63);
double result = std::ldexp(static_cast<double>(mantissa), static_cast<int>(exponent) - 16383 - 63);


return sign ? -result : result;
}
Expand Down
Loading