Skip to content

Commit

Permalink
Add boundary checks
Browse files Browse the repository at this point in the history
  • Loading branch information
deiteris committed Jan 26, 2023
1 parent 42bff72 commit 03b1e28
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions alvr/server/cpp/alvr_server/ClientConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ void sendHeaders(uint8_t **buf, int *len, int nalNum) {
int headersLen = 0;
int foundHeaders = -1; // Offset by 1 header to find the length until the next header
while (b != end) {
if (memcmp(b, NAL_HEADER, sizeof(NAL_HEADER)) == 0) {
if (b + sizeof(NAL_HEADER) <= end && memcmp(b, NAL_HEADER, sizeof(NAL_HEADER)) == 0) {
foundHeaders++;
if (foundHeaders == nalNum) {
break;
Expand All @@ -58,8 +58,8 @@ void processH264Nals(uint8_t **buf, int *len) {
uint8_t *b = *buf;
int l = *len;
uint8_t nalType = b[4] & 0x1F;
if (nalType == H264_NAL_TYPE_AUD) {

if (nalType == H264_NAL_TYPE_AUD && l > sizeof(NAL_HEADER) * 2 + 2) {
b += sizeof(NAL_HEADER) + 2;
l -= sizeof(NAL_HEADER) + 2;
nalType = b[4] & 0x1F;
Expand All @@ -75,8 +75,8 @@ void processH265Nals(uint8_t **buf, int *len) {
uint8_t *b = *buf;
int l = *len;
uint8_t nalType = (b[4] >> 1) & 0x3F;
if (nalType == H265_NAL_TYPE_AUD) {

if (nalType == H265_NAL_TYPE_AUD && l > sizeof(NAL_HEADER) * 2 + 3) {
b += sizeof(NAL_HEADER) + 3;
l -= sizeof(NAL_HEADER) + 3;
nalType = (b[4] >> 1) & 0x3F;
Expand All @@ -92,6 +92,10 @@ void ClientConnection::SendVideo(uint8_t *buf, int len, uint64_t targetTimestamp
// Report before the frame is packetized
ReportEncoded(targetTimestampNs);

if (len < sizeof(NAL_HEADER)) {
return;
}

int codec = Settings::Instance().m_codec;
if (codec == ALVR_CODEC_H264) {
processH264Nals(&buf, &len);
Expand Down

0 comments on commit 03b1e28

Please sign in to comment.