Fix static-analysis issues for FFmpegVideoEncoder and FFmpegVideoDecoder.cpp
Categories
(Core :: Audio/Video, enhancement, P1)
Tracking
()
| Tracking | Status | |
|---|---|---|
| firefox150 | --- | fixed |
People
(Reporter: chunmin, Assigned: chunmin)
References
Details
Attachments
(4 external files)
cppcoreguidelines-narrowing-conversions fires in FFmpegVideoEncoder.cpp and FFmpegVideoDecoder.cpp due to implicit conversions between integer and floating-point types of differing widths. These are genuine narrowing conversions that should be made explicit.
Steps to Reproduce
Run ./mach static-analysis check on the affected files:
./mach static-analysis check dom/media/platforms/ffmpeg/FFmpegVideoEncoder.cpp
./mach static-analysis check dom/media/platforms/ffmpeg/FFmpegVideoDecoder.cpp
Expected Behavior
No cppcoreguidelines-narrowing-conversions warnings.
Actual Behavior
FFmpegVideoEncoder.cpp:
warning: narrowing conversion from 'size_t' (aka 'unsigned long') to 'int' [cppcoreguidelines-narrowing-conversions]
mCodecContext->thread_count =
std::clamp<int>(threads, 1, GetNumberOfProcessors() - 1);
^
GetNumberOfProcessors() returns size_t; it is passed directly as the upper bound of std::clamp<int>, narrowing from size_t to int.
FFmpegVideoDecoder.cpp:
warning: narrowing conversion from 'double' to 'float' [cppcoreguidelines-narrowing-conversions]
float decodeTime = (now - mDecodeStart).ToMilliseconds();
^
warning: narrowing conversion from 'int64_t' to 'float' [cppcoreguidelines-narrowing-conversions]
const float frameDuration = aDuration / 1000.0f;
^
warning: narrowing conversion from 'uint32_t' to 'float' [cppcoreguidelines-narrowing-conversions]
mAverageFrameDuration =
(mAverageFrameDuration * (mDecodedFrames - 1) + frameDuration) /
^
TimeDuration::ToMilliseconds() returns double; aDuration is int64_t; mDecodedFrames, mLastDelayedFrameNum, and mDelayedFrameReset are uint32_t. All are used in expressions that produce a float result without an explicit cast.
Fix
Add explicit static_cast at each narrowing site.
FFmpegVideoEncoder.cpp:
mCodecContext->thread_count = std::clamp<int>(
threads, 1, static_cast<int>(GetNumberOfProcessors()) - 1);
FFmpegVideoDecoder.cpp:
float decodeTime = static_cast<float>((now - mDecodeStart).ToMilliseconds());
const float frameDuration = static_cast<float>(aDuration) / 1000.0f;
mAverageFrameDuration =
(mAverageFrameDuration * static_cast<float>(mDecodedFrames - 1) +
frameDuration) /
static_cast<float>(mDecodedFrames);
mAverageFrameDecodeTime =
(mAverageFrameDecodeTime * static_cast<float>(mDecodedFrames - 1) +
decodeTime) /
static_cast<float>(mDecodedFrames);
float correctPlaybackTime =
static_cast<float>(mDecodedFrames - mLastDelayedFrameNum) *
mAverageFrameDuration;
if (correctPlaybackTime > static_cast<float>(mDelayedFrameReset)) {
| Assignee | ||
Comment 1•8 months ago
|
||
GetNumberOfProcessors() returns size_t; cast to int before passing to
std::clamp<int> to avoid an implicit narrowing conversion.
| Assignee | ||
Comment 2•8 months ago
|
||
| Assignee | ||
Comment 3•8 months ago
|
||
| Assignee | ||
Comment 4•8 months ago
|
||
AssertedCast provides debug-mode bounds checking (MOZ_CRASH on overflow)
while compiling to the same code as static_cast in release builds. This
catches truncation and sign-change bugs early in development without
any runtime cost in production.
Conversions covered:
- uint32_t/size_t -> int (config values, pref values, packet sizes)
- int -> size_t (sign change on packet/extradata sizes)
- uint32_t -> FFmpegBitRate (int on older ffmpeg)
- int64_t -> int (USECS_PER_S constant)
- size_t -> float (audio buffer size)
- enum -> uint8_t (ColorDepth)
Updated•8 months ago
|
Updated•8 months ago
|
Updated•8 months ago
|
Updated•8 months ago
|
https://hg.mozilla.org/mozilla-central/rev/635f16eaaa7a
https://hg.mozilla.org/mozilla-central/rev/5a9fd3eff353
https://hg.mozilla.org/mozilla-central/rev/bbba94ecbfdf
https://hg.mozilla.org/mozilla-central/rev/257c23ca81a8
Updated•8 months ago
|
Description
•