The following code copies too many data items to a local array:void function_1 ( void )
{
char array[10];
strcpy ( array, "This will overwrite");
}
An overrun like this corrupts data on the stack, including return addresses, thus causing odd program behavior. Possible causes of this error include: passing a string that is not NULL terminated or passing a source string too long for the destination. When repairing this problem, remember that literal strings are followed by an implied NULL character. The fix to the C++ sample code is:void function_1(void )
{
char array[256];
strcpy ( array, "This will overwrite ");
}

解决方案 »

  1.   

    int wc::lSumDir(char *pszDirName)
    {
        WIN32_FIND_DATA Ffile; /*store file infomation */
    int  lCurLines = g_lLines;/* lines before current dirctory */
        /* file number before current dirctory */
        int  lCurNumFile = g_lNumFile;
        /* skip lines before current dirctory */
        int  lCurSkip = g_lSkipLines;
        /* file size before current dirctory */
        int  lCurSize = g_lSize;
        /* single dirctory lines but space and note */
        int  lSingleLines;
        /* single dirctory lines */
        int  lSingleAll;
        /* single dirctory file number */
        int  lSingleNumFile;
        /* single dirctory size */
        int  lSingleSize;
        HANDLE  hFlag;  /* find successed flag*/
        char cNewName[2048];   /* store complate filename */
        char *pPosif;           /* store charachter position */
    char *pPosie;
        char cDirName[2048];   /* store dirctory name */
        char cCPath[2048];     /* store path for c */
        const char cChar[2] = {92, '\0'};  /* character "\" */
        char cDriv;            /* drive name */

    /* out put dirctory name */
        pPosie = strrchr(pszDirName, '\\'); 
        if(!pPosie)    /* no \ */
        {
    if(!strcmp(pszDirName, "..")) /* father dirctory */
      
    {
        cDirName[0] = '.';
            cDirName[1] = '.';
        cDirName[2] = 92;
    cDirName[3] = '\0';
    strcpy(pszDirName, "..\\*.*");
    }
    else           /* Current dirctory */
    {
        cDirName[0] = '.';
            cDirName[1] = 92;
        cDirName[2] = '\0';
            if(!strcmp(pszDirName, "."))
    {
            strcpy(pszDirName, "*");
    } /* end of if(!strcmp(pszDirName, ".")) */

    } /* end of if(!strcmp(pszDirName ... */ }
        else
        {
        /* find * and ? in parameter */
    pPosif = strchr(pszDirName, '*');
    if(!pPosif)
    {
    pPosif = strchr(pszDirName, '?');
    } /* end of if(!pPosif) */
    if(pPosif)
    {
        if((pPosif - pszDirName) < (pPosie - pszDirName))
    {
        sprintf(pRus,"Parameter wrong!\r\n");
                 strcat(pRusend, pRus);
        return -1;
    } /* end of if((pPosif- pszDirName) ... */
    } /* end of if(!pPosif) */ /* get dirctory of parameter */
    strncpy(cDirName, pszDirName, pPosie - pszDirName + 1);
        cDirName[pPosie - pszDirName + 1] = '\0';
            /* if only dirctory, add *.* for search */
    if((strlen(pszDirName)) == (unsigned)(pPosie-pszDirName) + 1)
    {
    sprintf(pszDirName, "*.*");
            strcat(pRusend, pRus);
    } /* end of if((strlen(pszDirName) */
    pPosif = strchr(pszDirName, '\\');
    } /*end of if(!pPosie) */
        
    sprintf(pRus,"Path:%s\r\n", cDirName);
    strcat(pRusend, pRus);
        /* seek subdirctory */
        hFlag = FindFirstFile(pszDirName, &Ffile);
        if(hFlag == INVALID_HANDLE_VALUE)
    {
    return 0;
    }
        do
        {
        if(strcmp(Ffile.cFileName, ".") && 
       strcmp(Ffile.cFileName, "..") && 
       Ffile.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY)
    {
            strcpy(cNewName, cDirName);/* This Wrong !!!!!!!*/
            strcat(cNewName, Ffile.cFileName);         strcat(cNewName, cChar);
            if(lSumDir(cNewName) < 0)
    {
            return -1;
    } /* end of if(lSumDir(cNewName) < 0) */
    } /* end of if(strcmp... */
        }while(FindNextFile(hFlag, &Ffile));
    FindClose(hFlag);
     
    /* find file */
    hFlag = FindFirstFile(pszDirName, &Ffile);
        if(hFlag == INVALID_HANDLE_VALUE)
    {
    return 0;
    }
      
    do
        {
        if(Ffile.dwFileAttributes == 32)
    {
              cNewName[0] = '\0';
    pPosif = strchr(pszDirName, '\\');
            /* get dirve name */
    if(pPosif &&
      ((pPosif - pszDirName) == 0))
    {
            cDriv = _getdrive() + 'A' - 1;
                cNewName[0] = cDriv;
            cNewName[1] = ':';
    cNewName[2] = '\0';
    } /* end of if(pPosif && ... */ /* made file name */
    strcat(cNewName, cDirName);
                strcat(cNewName, Ffile.cFileName);
             pMakeNewName(cCPath, cNewName); if(lSumFileLines(cCPath, Ffile.cFileName, 
           Ffile.nFileSizeLow) < 0)
    {
            return -1;
    } /* end of if(lSumDir(cNewName) < 0) */
    } /* end of if(strcmp... */
        }while(FindNextFile(hFlag, &Ffile));
    FindClose(hFlag);
        
    /* output data */
    lSingleLines = g_lLines - lCurLines;
        lSingleAll = lSingleLines + g_lSkipLines - lCurSkip;
        lSingleNumFile = g_lNumFile - lCurNumFile;
        lSingleSize = g_lSize - lCurSize;    sprintf(pRus,"total: %6d  %6d  %8d     %4d files\r\n",
           lSingleLines, lSingleAll,
           lSingleSize, lSingleNumFile);
    strcat(pRusend, pRus);
        sprintf(pRus,"(%s)\r\n\r\n", cDirName);
    strcat(pRusend, pRus);    return lSingleLines;
    } /*end of lSumDir()*/
      

  2.   

    我觉得是不是可以这样:在strcpy之前先求一下strlen看长度到底是多少。
    另外再sizeof一下接收串的数组
      

  3.   

    if((strlen(pszDirName)) == (unsigned)(pPosie-pszDirName) + 1)
    {
    sprintf(pszDirName, "*.*"); //wrong!!!!!
    //       strcat(pRusend, pRus);
    } /* end of if((strlen(pszDirName) */for example:fun(char* pchar)
    {
    sprintf(pchar, "*.*");
    }int main(int argc, char *argv[])
    { fun("hehe");

    hehe,check it and good luck