Skip to content

Commit

Permalink
Fix more null pointer exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
peternewman committed Apr 6, 2024
1 parent 14d56c9 commit 91e014f
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
15 changes: 15 additions & 0 deletions libs/acn/PDU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ bool PDU::Pack(uint8_t *buffer, unsigned int *length) const {
* Write this PDU to an OutputStream.
*/
void PDU::Write(OutputStream *stream) const {
if (!stream) {
OLA_WARN << "PDU::Write: missing stream";
return;
}

unsigned int size = Size();

if (size <= TWOB_LENGTH_LIMIT && !m_force_length_flag) {
Expand Down Expand Up @@ -173,6 +178,11 @@ void PDU::Write(OutputStream *stream) const {
void PDU::PrependFlagsAndLength(ola::io::OutputBufferInterface *output,
uint8_t flags,
bool force_length_flag) {
if (!output) {
OLA_WARN << "PDU::PrependFlagsAndLength: missing output";
return;
}

PrependFlagsAndLength(output, output->Size(), flags, force_length_flag);
}

Expand All @@ -184,6 +194,11 @@ void PDU::PrependFlagsAndLength(ola::io::OutputBufferInterface *output,
unsigned int size,
uint8_t flags,
bool force_length_flag) {
if (!output) {
OLA_WARN << "PDU::PrependFlagsAndLength: missing output";
return;
}

if (size + 2 <= TWOB_LENGTH_LIMIT && !force_length_flag) {
size += 2;
uint16_t flags_and_length = static_cast<uint16_t>(size);
Expand Down
5 changes: 5 additions & 0 deletions libs/acn/PDU.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,11 @@ bool PDUBlock<C>::Pack(uint8_t *data, unsigned int *length) const {
*/
template <class C>
void PDUBlock<C>::Write(ola::io::OutputStream *stream) const {
if (!stream) {
OLA_WARN << "PDUBlock::Write: missing stream";
return;
}

typename std::vector<const C*>::const_iterator iter;
for (iter = m_pdus.begin(); iter != m_pdus.end(); ++iter) {
// TODO(simon): optimize repeated headers & vectors here
Expand Down
35 changes: 35 additions & 0 deletions libs/acn/PDUTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@ class PDUTest: public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(PDUTest);
CPPUNIT_TEST(testPDU);
CPPUNIT_TEST(testPDUBlock);
CPPUNIT_TEST(testPDUToOutputStream);
CPPUNIT_TEST(testBlockToOutputStream);
CPPUNIT_TEST_SUITE_END();

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

void setUp() {
Expand Down Expand Up @@ -136,6 +138,36 @@ void PDUTest::testPDUBlock() {
}


/*
* Test that writing a PDU to an OutputStream works.
*/
void PDUTest::testPDUToOutputStream() {
MockPDU pdu(0x1234, 0x2468);

IOQueue output;
OutputStream stream(&output);
pdu.Write(&stream);
OLA_ASSERT_EQ(14u, output.Size());

uint8_t *data = new uint8_t[output.Size()];
unsigned int data_size = output.Peek(data, output.Size());
OLA_ASSERT_EQ(output.Size(), data_size);

uint8_t EXPECTED[] = {
0x70, 0x0e, 0, 0,
0, 0x2b,
0x34, 0x12, 0, 0,
0x68, 0x24, 0, 0
};
OLA_ASSERT_DATA_EQUALS(EXPECTED, sizeof(EXPECTED), data, data_size);
output.Pop(output.Size());

// test null stream
pdu.Write(NULL);
delete[] data;
}


/*
* Test that writing to an OutputStream works.
*/
Expand Down Expand Up @@ -164,6 +196,9 @@ void PDUTest::testBlockToOutputStream() {
};
OLA_ASSERT_DATA_EQUALS(EXPECTED, sizeof(EXPECTED), block_data, block_size);
output.Pop(output.Size());

// test null stream
block.Write(NULL);
delete[] block_data;
}
} // namespace acn
Expand Down

0 comments on commit 91e014f

Please sign in to comment.