mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2026-11-24 08:57:16 +08:00
fix: makeRtp增加负载长度校验 (#4857)
RTP 包的长度字段为 16 位,但 `RtpInfo::makeRtp` 未校验入参长度:分配内存时 长度被截断为 16 位,拷贝数据时却使用原始的 `size_t` 长度,二者并不一致;被 截断的长度字段同样无法正确表示 RTSP interleaved 包长。 ## 改动 - `src/Rtsp/RtpCodec.cpp`:在相加、分配内存和写头部之前,拒绝超过 `UINT16_MAX - kRtpHeaderSize`(65523)的长度,抛出 `std::length_error`。 允许的最大负载恰好填满 RTSP interleaved 长度字段,不会产生被截断的头部。 - `src/Rtsp/RtpCodec.h`:补充接口注释,说明异常行为。 - `src/Rtsp/RtspMuxer.cpp`:在 `inputFrame`/`flush` 中捕获异常,避免 MTU 配置过大时异常扩散到事件循环。 - `webrtc/WebRtcPlayer.cpp`:跳过超长 config 帧,并保持后续包的 seq 序号对齐。 ## 兼容性 65523 字节的上限不会拒绝任何合法负载:其余 `makeRtp` 调用点的长度均由 `getMaxSize()`(MTU - 12)限制,而 UDP 数据报本身最大也只有 65507 字节。 正常 MTU 下的大帧仍按原有逻辑分片,行为不变。 ## 验证 在 `ENABLE_ASAN=ON` + `ENABLE_WEBRTC=ON` 下编译通过,并以 ASan 运行了 长度边界(0 / 4 / 1388 / 65523 接受,65524 及以上拒绝)、muxer 异常边界 与 WebRTC config 帧路径的检查,均无内存错误。 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
@@ -9,6 +9,7 @@
|
||||
*/
|
||||
|
||||
#include "WebRtcPlayer.h"
|
||||
#include <stdexcept>
|
||||
|
||||
#include "Common/config.h"
|
||||
#include "Extension/Factory.h"
|
||||
@@ -326,7 +327,14 @@ void WebRtcPlayer::sendConfigFrames(const RtpPacket::Ptr &packet) {
|
||||
|
||||
auto seq = packet->getSeq() - frames.size();
|
||||
for (const auto &frame : frames) {
|
||||
auto rtp = encoder->getRtpInfo().makeRtp(TrackVideo, frame->data() + frame->prefixSize(), frame->size() - frame->prefixSize(), false, 0);
|
||||
RtpPacket::Ptr rtp;
|
||||
try {
|
||||
rtp = encoder->getRtpInfo().makeRtp(TrackVideo, frame->data() + frame->prefixSize(), frame->size() - frame->prefixSize(), false, 0);
|
||||
} catch (const std::length_error &ex) {
|
||||
WarnL << "Skipping oversized WebRTC config frame: " << ex.what();
|
||||
++seq;
|
||||
continue;
|
||||
}
|
||||
auto header = rtp->getHeader();
|
||||
header->seq = htons(seq++);
|
||||
header->stamp = htonl(packet->getStamp());
|
||||
|
||||
Reference in New Issue
Block a user