Skip to content

Commit

Permalink
Fixed allocation bug
Browse files Browse the repository at this point in the history
  • Loading branch information
staropram committed Dec 19, 2021
1 parent 2cb193c commit e7bb846
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 23 deletions.
6 changes: 3 additions & 3 deletions Makefile
Expand Up @@ -5,16 +5,16 @@ CURDIR=/opt/local/lib -L.
TEST_LIBS=-L$(CURDIR) -lcunit

CPPFLAGS=-I/opt/local/include
#Uncomment this to enabel debug output and symbols
CPPFLAGS+=-DDEBUG -O0 -g
# Uncomment this to enable debug output and symbols
#CPPFLAGS+=-DDEBUG -O0 -g

CFLAGS=-Wall -std=c99
CXXFLAGS=-Wall -std=c++11

default: nethelper.o staticlib test

test: test.cpp libcantcoap.a
$(CXX) $(CPPFLAGS) $(CXXFLAGS) $< -o $@ -lcantcoap $(TEST_LIBS)
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -fsanitize=address $< -o $@ -lcantcoap $(TEST_LIBS)

cantcoap.o: cantcoap.cpp cantcoap.h
$(CXX) $(CPPFLAGS) $(CXXFLAGS) $< -c -o $@
Expand Down
25 changes: 13 additions & 12 deletions cantcoap.cpp
Expand Up @@ -1124,7 +1124,7 @@ int CoapPDU::addOption(uint16_t insertedOptionNumber, uint16_t optionValueLength
* \return Either a pointer to the payload buffer, or NULL if there wasn't enough space / allocation failed.
*/
uint8_t* CoapPDU::mallocPayload(int len) {
DBG("Entering mallocPayload");
DBG("Entering mallocPayload with len %d",len);
// sanity checks
if(len==0) {
DBG("Cannot allocate a zero length payload");
Expand Down Expand Up @@ -1152,10 +1152,12 @@ uint8_t* CoapPDU::mallocPayload(int len) {
markerSpace = 0;
// compute new payload length (can be negative if shrinking payload)
payloadSpace = len-_payloadLength;
DBG("New payload space(%d): len(%d)-_payloadLength(%d)",payloadSpace,len,_payloadLength);
}

// make space for payload (and payload marker if necessary)
int newLen = _pduLength+payloadSpace+markerSpace;
DBG("Allocating %d bytes: _pduLength(%d), payloadSpace(%d), markerSpace(%d)",newLen,_pduLength,payloadSpace,markerSpace);
if(!_constructedFromBuffer) {
uint8_t* newPDU = (uint8_t*)realloc(_pdu,newLen);
if(newPDU==NULL) {
Expand All @@ -1170,10 +1172,10 @@ uint8_t* CoapPDU::mallocPayload(int len) {
_pdu = newPDU;
DBG("This is a new allocation");
// adjust payload pointer to point into the new memory
_payloadPointer = &_pdu[_pduLength+1];
_payloadPointer = &_pdu[newLen-len];
// and also set the payload marker if it wasn't already set
if(markerSpace!=0) {
_pdu[_pduLength] = 0xFF;
_pdu[newLen-len-1] = 0xFF;
}
}

Expand All @@ -1186,20 +1188,19 @@ uint8_t* CoapPDU::mallocPayload(int len) {
}
}

_pduLength = newLen;
_payloadLength = len;

// deal with fresh allocation case separately
if(_payloadPointer==NULL) {
// set payload marker
_pdu[_pduLength] = 0xFF;
DBG("Fresh allocation");
// payload at end of old PDU
_payloadPointer = &_pdu[_pduLength+1];
_pduLength = newLen;
_payloadLength = len;
_payloadPointer = &_pdu[newLen-len];
// set payload marker
_pdu[newLen-len-1] = 0xFF;
return _payloadPointer;
}

// otherwise, just adjust length of PDU
_pduLength = newLen;
_payloadLength = len;
DBG("Leaving mallocPayload");
return _payloadPointer;
}
Expand Down Expand Up @@ -1785,7 +1786,7 @@ void CoapPDU::printHuman() {
if((c>='!'&&c<='~')||c==' ') {
INFOX("%c",c);
} else {
INFOX("\\%.2x",c);
INFOX("\\%.2x",(uint8_t)c);
}
}
INFO("\"");
Expand Down
18 changes: 10 additions & 8 deletions test.cpp
Expand Up @@ -461,39 +461,41 @@ void testPayload() {
}
pdu->setType(CoapPDU::COAP_CONFIRMABLE);
#ifdef DEBUG
pdu->printBin();
//pdu->printBin();
#endif
pdu->setCode(CoapPDU::COAP_GET);
pdu->setVersion(1);
#ifdef DEBUG
pdu->printBin();
//pdu->printBin();
#endif
pdu->setMessageID(0x1234);
pdu->setURI((char*)"test",4);
#ifdef DEBUG
pdu->printBin();
//pdu->printBin();
#endif
CU_ASSERT_FATAL(pdu->setPayload(NULL,4)==1);
CU_ASSERT_FATAL(pdu->setPayload((uint8_t*)"test",0)==1);
pdu->setPayload((uint8_t*)"\1\2\3",3);
#ifdef DEBUG
pdu->printBin();
//pdu->printBin();
pdu->printHex();
#endif
CU_ASSERT_EQUAL_FATAL(pdu->getPayloadLength(),3);
CU_ASSERT_NSTRING_EQUAL_FATAL(pdu->getPDUPointer(),payloadTestPDUA,pdu->getPDULength());
CU_ASSERT_NSTRING_EQUAL_FATAL(pdu->getPayloadPointer(),"\1\2\3",pdu->getPayloadLength());
DBG("Trying to increase payload size");
pdu->setPayload((uint8_t*)"\4\3\2\1",4);
#ifdef DEBUG
pdu->printBin();
//pdu->printBin();
#endif
CU_ASSERT_EQUAL_FATAL(pdu->getPayloadLength(),4);
CU_ASSERT_NSTRING_EQUAL_FATAL(pdu->getPDUPointer(),payloadTestPDUB,pdu->getPDULength());

CU_ASSERT_NSTRING_EQUAL_FATAL(pdu->getPayloadPointer(),"\4\3\2\1",pdu->getPayloadLength());
DBG("Trying to reduce payload size");
pdu->setPayload((uint8_t*)"\1\2",2);
#ifdef DEBUG
pdu->printBin();
//pdu->printBin();
#endif
CU_ASSERT_EQUAL_FATAL(pdu->getPayloadLength(),2);
CU_ASSERT_NSTRING_EQUAL_FATAL(pdu->getPDUPointer(),payloadTestPDUC,pdu->getPDULength());
Expand Down Expand Up @@ -558,7 +560,7 @@ void testBigRealloc() {
pdu->setVersion(1);
pdu->setMessageID(0x1234);
#ifdef DEBUG
pdu->printBin();
//pdu->printBin();
#endif
pdu->setPayload(testPayload,testPayloadLen);

Expand All @@ -570,7 +572,7 @@ void testBigRealloc() {
DBG("Trying to reduce payload size");
pdu->setPayload((uint8_t*)"\1\2",2);
#ifdef DEBUG
pdu->printBin();
//pdu->printBin();
#endif
CU_ASSERT_EQUAL_FATAL(pdu->getPayloadLength(),2);
CU_ASSERT_NSTRING_EQUAL_FATAL(pdu->getPayloadPointer(),"\1\2",pdu->getPayloadLength());
Expand Down

0 comments on commit e7bb846

Please sign in to comment.