Skip to content

Commit

Permalink
Fix some null pointer exceptions
Browse files Browse the repository at this point in the history
(cherry picked from commit 14d56c9)
  • Loading branch information
peternewman committed Apr 5, 2024
1 parent 2c91e70 commit d6a13dd
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
10 changes: 10 additions & 0 deletions libs/acn/PDU.cpp
Expand Up @@ -50,6 +50,16 @@ unsigned int PDU::Size() const {
* @return false on error, true otherwise
*/
bool PDU::Pack(uint8_t *buffer, unsigned int *length) const {
if (!buffer) {
OLA_WARN << "PDU::Pack: missing buffer";
return false;
}

if (!length) {
OLA_WARN << "PDU::Pack: missing length";
return false;
}

unsigned int size = Size();
unsigned int offset = 0;

Expand Down
11 changes: 11 additions & 0 deletions libs/acn/PDU.h
Expand Up @@ -22,6 +22,7 @@
#define LIBS_ACN_PDU_H_

#include <stdint.h>
#include <ola/Logging.h>
#include <ola/acn/ACNFlags.h>
#include <ola/io/OutputStream.h>
#include <ola/io/OutputBuffer.h>
Expand Down Expand Up @@ -160,6 +161,16 @@ class PDUBlock {
*/
template <class C>
bool PDUBlock<C>::Pack(uint8_t *data, unsigned int *length) const {
if (!data) {
OLA_WARN << "PDUBlock::Pack: missing buffer";
return false;
}

if (!length) {
OLA_WARN << "PDUBock::Pack: missing length";
return false;
}

bool status = true;
unsigned int i = 0;
typename std::vector<const C*>::const_iterator iter;
Expand Down
55 changes: 55 additions & 0 deletions libs/acn/PDUTest.cpp
Expand Up @@ -35,11 +35,13 @@ using ola::io::OutputStream;

class PDUTest: public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(PDUTest);
CPPUNIT_TEST(testPDU);
CPPUNIT_TEST(testPDUBlock);
CPPUNIT_TEST(testBlockToOutputStream);
CPPUNIT_TEST_SUITE_END();

public:
void testPDU();
void testPDUBlock();
void testBlockToOutputStream();

Expand All @@ -51,6 +53,41 @@ class PDUTest: public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE_REGISTRATION(PDUTest);


/*
* Test that packing a PDUBlock works.
*/
void PDUTest::testPDU() {
MockPDU pdu(0x1234, 0x2468);

OLA_ASSERT_EQ(4u, pdu.HeaderSize());
OLA_ASSERT_EQ(4u, pdu.DataSize());
OLA_ASSERT_EQ(14u, pdu.Size());

unsigned int size = pdu.Size();
uint8_t *data = new uint8_t[size];
unsigned int bytes_used = size;
OLA_ASSERT(pdu.Pack(data, &bytes_used));
OLA_ASSERT_EQ(size, bytes_used);

// test null data
OLA_ASSERT_FALSE(pdu.Pack(NULL, &bytes_used));

// test a null length
OLA_ASSERT_FALSE(pdu.Pack(data, NULL));

// test undersized buffer
bytes_used = size - 1;
OLA_ASSERT_FALSE(pdu.Pack(data, &bytes_used));
OLA_ASSERT_EQ(0u, bytes_used);

// test oversized buffer
bytes_used = size + 1;
OLA_ASSERT(pdu.Pack(data, &bytes_used));
OLA_ASSERT_EQ(size, bytes_used);
delete[] data;
}


/*
* Test that packing a PDUBlock works.
*/
Expand All @@ -74,6 +111,24 @@ void PDUTest::testPDUBlock() {
OLA_ASSERT_EQ(1u, *test++);
OLA_ASSERT_EQ(2u, *test++);
OLA_ASSERT_EQ(42u, *test);

// test null data
OLA_ASSERT_FALSE(block.Pack(NULL, &bytes_used));

// test a null length
OLA_ASSERT_FALSE(block.Pack(data, NULL));

// test undersized buffer
bytes_used = block_size - 1;
OLA_ASSERT_FALSE(block.Pack(data, &bytes_used));
// TODO(Peter): Work out what behaviour we want for the bytes used, it's
// currently the actual total used, not zero like the PDU::Pack returns
// OLA_ASSERT_EQ(0u, bytes_used);

// test oversized buffer
bytes_used = block_size + 1;
OLA_ASSERT(block.Pack(data, &bytes_used));
OLA_ASSERT_EQ(block_size, bytes_used);
delete[] data;

block.Clear();
Expand Down

0 comments on commit d6a13dd

Please sign in to comment.