In a Packet() class, I have this method that will self encapsulate the packet iteslf (i.e. adding the 6 bytes of header in front of it, but of course first allocating the necessary memory) However, it is giving me a * stack smashing detected *:
int TmcPckt::EncapsulateCCSDS(void) {
// packet identification in primary header
UI16 primaryHdrPcktID;
primaryHdrPcktID = (TMC_CCSDS_PKT_VR_NBR << TMC_CCSDS_OFFSET_PKT_VR_NBR)
| (this->_tmcDirection << TMC_CCSDS_OFFSET_PKT_ID_PKT_TYP)
| (TMC_CCSDS_PKT_ID_HDR_FLG << TMC_CCSDS_OFFSET_PKT_ID_HDR_FLG)
| (this->_appId << TMC_CCSDS_OFFSET_PKT_ID_APP_ID);
// sequence control in primary header
UI16 primaryHdrSeqCtrl;
primaryHdrSeqCtrl = (TMC_CCSDS_PKT_SEC_CTRL_SEQ_FLG << TMC_CCSDS_OFFSET_PKT_SEC_CTRL_SEQ_FLG)
| (TMC_CCSDS_PKT_SEC_CTRL_SEQ_CNT << TMC_CCSDS_OFFSET_PKT_SEC_CTRL_SEQ_CNT);
// packet length in primary header;
// should be the length in bytes of entire packet except primary header minus 1
UI16 primaryHdrPcktLen;
primaryHdrPcktLen = this->_dataSize - 1;
// convert data to network
primaryHdrPcktID = htons(primaryHdrPcktID);
primaryHdrSeqCtrl = htons(primaryHdrSeqCtrl);
primaryHdrPcktLen = htons(primaryHdrPcktLen);
// convert data to netwaSaSork
for (int i = 0; i < this->_dataSize; i++)
{
this->_dataBuffer[i] = htons(this->_dataBuffer[i]);
}
this->_packetSize = TMC_CCSDS_PRIM_HDR_LEN_BYTES + this->_dataSize;
this->_packetBuffer = (UI16 *) operator new(this->_packetSize/sizeof(UI16));
// copy data to destination memory: headers
this->_packetBuffer[TMC_CCSDS_PRIMARY_HDR_PCKT_ID] = primaryHdrPcktID;
this->_packetBuffer[TMC_CCSDS_PRIMARY_HDR_SEQ_CTRL] = primaryHdrSeqCtrl;
this->_packetBuffer[TMC_CCSDS_PRIMARY_HDR_PCKT_LEN] = primaryHdrPcktLen;
// copy data to destination memory: payload
// memcpy((void*) &this->_packetBuffer[TMC_CCSDS_DATA_PAYLOAD_AND_CMD], (void*) this->_dataBuffer, sizeof(UI16)*this->_dataSize);
memcpy((void*) &this->_packetBuffer[TMC_CCSDS_DATA_PAYLOAD_AND_CMD], (void*) this->_dataBuffer, this->_dataSize);
LogPrintBuffer(this->_packetBuffer, this->_packetSize/sizeof(UI16));
this->_encapsulated = true;
return STATUS_SUCCESS;
}
And I would like to find out where (line of code) it is giving issue... :/ I can recompile with a flag in order to avoid the detection but can't have my program to seg-fault...so I guess I can't use gdb here. (plus this is called in a g_timeout_add() so kind of threaded)
Can Valgrind be of any use? Am I doint something ugly/wrong by doing a new() that won't never be freed
Can I do this with instead ?