fix: parse AV1 LEB128 sizes in 64 bits instead of shifting an int past its width, fixes #5484

This commit is contained in:
Jash committed 2026-11-08 15:59:43 +05:30
1 parent 8556830515
commit c15eefbfdc
4 files changed
+73 -15

No files matched your search

+2 -15
View File
@@ -10,6 +10,7 @@
#include "zm_config.h"
#include "zm_logger.h"
#include "zm_utils.h"
#include "xop/AV1Source.h"
#if HAVE_RTSP_SERVER
@@ -26,21 +27,7 @@ AV1_ZoneMinderStreamSource::AV1_ZoneMinderStreamSource(
// Parse LEB128 variable-length integer (used for OBU sizes in AV1)
size_t AV1_ZoneMinderStreamSource::parseLEB128(
unsigned char* data, size_t max_size, uint32_t& value) {
value = 0;
size_t bytes_read = 0;
for (size_t i = 0; i < max_size && i < 8; i++) {
uint8_t byte = data[i];
value |= ((byte & 0x7F) << (7 * i));
bytes_read++;
if ((byte & 0x80) == 0) {
// Last byte of LEB128
break;
}
}
return bytes_read;
return zm_parse_leb128(data, max_size, value);
}
// Parse OBU header
+18
View File
@@ -674,3 +674,21 @@ std::string escape_json_string(std::string input) {
}
return output;
}
size_t zm_parse_leb128(const unsigned char *data, size_t max_size, uint32_t &value) {
uint64_t result = 0;
size_t bytes_read = 0;
for (size_t i = 0; i < max_size && i < 8; i++) {
uint8_t byte = data[i];
result |= static_cast<uint64_t>(byte & 0x7F) << (7 * i);
bytes_read++;
if ((byte & 0x80) == 0) {
break;
}
}
value = static_cast<uint32_t>(result);
return bytes_read;
}
+3
View File
@@ -112,6 +112,9 @@ std::string stringtf(const char* format, ...);
std::string ByteArrayToHexString(nonstd::span<const uint8> bytes);
// Parses an AV1 LEB128 value (at most 8 bytes) and returns the bytes consumed.
size_t zm_parse_leb128(const unsigned char *data, size_t max_size, uint32_t &value);
std::string Base64Encode(const std::string &str);
std::string TimevalToString(timeval tv);
+50
View File
@@ -582,3 +582,53 @@ TEST_CASE("url_for_log") {
REQUIRE(out.find("eyJ") == std::string::npos);
}
}
TEST_CASE("zm_parse_leb128", "[utils]") {
uint32_t value;
size_t n;
SECTION("single byte") {
static const unsigned char data[] = {0x05};
n = zm_parse_leb128(data, sizeof(data), value);
REQUIRE(n == 1);
REQUIRE(value == 5);
}
SECTION("two bytes, 128") {
static const unsigned char data[] = {0x80, 0x01};
n = zm_parse_leb128(data, sizeof(data), value);
REQUIRE(n == 2);
REQUIRE(value == 128);
}
SECTION("max 5-byte 32-bit value") {
static const unsigned char data[] = {0xFF, 0xFF, 0xFF, 0xFF, 0x0F};
n = zm_parse_leb128(data, sizeof(data), value);
REQUIRE(n == 5);
REQUIRE(value == 4294967295u);
}
SECTION("padded 8-byte encoding of 1") {
static const unsigned char data[] = {
0x81, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00};
n = zm_parse_leb128(data, sizeof(data), value);
REQUIRE(n == 8);
REQUIRE(value == 1);
}
SECTION("max_size truncation") {
static const unsigned char data[] = {0xFF, 0xFF, 0xFF, 0xFF, 0x0F};
// Only 3 bytes available: 0x7F | (0x7F << 7) | (0x7F << 14) = 0x1FFFFF,
// and the third byte still has the continuation bit set, so it stops
// without a terminator but still returns the bytes it consumed.
n = zm_parse_leb128(data, 3, value);
REQUIRE(n == 3);
REQUIRE(value == 0x1FFFFFu);
}
SECTION("empty input") {
static const unsigned char data[] = {0x05};
n = zm_parse_leb128(data, 0, value);
REQUIRE(n == 0);
}
}