0

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 ?

bli
  • 93
  • 6
  • Yes [Valgrind](http://valgrind.org/) could definitely be of use. Build your program with debug information and run through Valgrind and it should detect out-of-bounds write and other similar errors (which is the most likely cause of "stack smashing" errors). – Some programmer dude Apr 03 '18 at 09:46
  • `this->_packetBuffer = (UI16 *) operator new(this->_packetSize/sizeof(UI16));` looks weird. The syntax is `pointer = new Type[size of the array]`. – mch Apr 03 '18 at 09:48
  • *"I can't use gdb here"* how is that? Why do you write `this->_packetBuffer = (UI16 *) operator new(this->_packetSize/sizeof(UI16));` instead of `this->_packetBuffer = new UI16 [this->_packetSize / sizeof(UI16)];` Also why do you post this part if you don't know what part of the code causes the problem yet? You should supply [mcve](https://stackoverflow.com/help/mcve). – user7860670 Apr 03 '18 at 09:49
  • @mch No that's correct. It calls the `operator new` *function* directly. It might not allocate enough memory though, as the size argument should be the size in *bytes* (and should therefore probably be multiplication instead of division). – Some programmer dude Apr 03 '18 at 09:50

0 Answers0