简体   繁体   中英

Run-Time Check Failure #2 - Stack around the variable 'NearID' was corrupted

I have read a bunch of threads dealing with this issue, and most of them seem to point to for-loop indexing being off. I double checked and found no obvious errors, what am I missing here:

 void VsuCNCtrl::resetRWID(bool state)
{   
    VsuCNComposite * thisCNAirport;
    VsuCNComposite * thisRW;
    VsuCNList        RWList;
    RWCString rwName;
    RWCString ICAO;
    int index, i, nbRW;
    char FarID[2];
    char NearID[2];

#   ifdef DEBUGRWID
    cout << "resetRWID: " << endl;
#   endif

    if (theTree().entries() < 1)
        return;

    VsuCNTreeNode * topNode = theTree().getTopTreeNode();

    if (!topNode)
    {
        cout << "VsuCNCtrl(resetRWID) Warning: empty Database." << endl;
        return;
    }

    thisCNAirport = (VsuCNComposite *)topNode->getCN();
    thisCNAirport->getRelatives("VsuCNAirport","VsuCNRunway", RWList);

    nbRW = RWList.entries();

    if (state == true)
    {
        for (i = 0; i < nbRW; i++)
        {
            thisRW = RWList[i];
            RWCString rwName = thisRW->getName();

            int index = VsuCNRunwayId::getRunwayId(rwName);

            sprintf(NearID, "%02d", index);
            sprintf(FarID,  "%02d", (index + nbRW));

            updateUICNData(rwName, RWCString("RwNearId"), RWCString(NearID));
            updateUICNData(rwName, RWCString("RwFarId"),  RWCString(FarID));
        }
    }

#   ifdef DEBUGRWID
    cout << "resetRWID: nbRW " << nbRW << endl;
#   endif

    VsuCNRunwayId::resetRunwayId();

    for (i = 0; i < nbRW; i++)
    {
        thisRW = RWList[i];

        VsuCNRunwayId::addRunway(thisRW->getName());
    }

    for (i = 0; i < nbRW; i++)
    {
        thisRW = RWList[i];

        rwName = thisRW->getName();

        int nid = thisRW->get("RwNearId");
        int fid = thisRW->get("RwFarId");

        index = VsuCNRunwayId::getRunwayId(rwName);

        if ( nid == 0 && fid == 0)
        {
            sprintf(NearID, "%02d", index);
            sprintf(FarID,  "%02d", (index + nbRW));
        }
        else
        {
            if (nid != index)
                sprintf(NearID, "%02d", nid);
            else
                sprintf(NearID, "%02d", index);

            if (fid != (index + nbRW))
                sprintf(FarID,  "%02d", fid);
            else
                sprintf(FarID,  "%02d", (index + nbRW));
        }

        updateUICNData(rwName, RWCString("RwNearId"), RWCString(NearID));
        updateUICNData(rwName, RWCString("RwFarId"),  RWCString(FarID));

#       ifdef DEBUGRWID
        cout << "rwName   : " << rwName            << endl;
        cout << "RwNearId : " << RWCString(NearID) << endl;
        cout << "RwFarId  : " << RWCString(FarID)  << endl;
#       endif
    }

    return; 
}

Thanks in advance

Here is the problem:

sprintf(FarID,  "%02d", (index + nbRW));

The FarID array is two character, but you write three characters to the array. Remember that every string also contains a special character that terminates the string.

You do the same on the line just above that, when writing three characters to the two-character array NearID .

Writing beyond the bounds of an array leads to undefined behavior , which means that almost anything can happen.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM