Menu

doubleFree bug?

2023-08-15
2023-08-15
  • huanghaifeng

    huanghaifeng - 2023-08-15

    Below code report doubleFree error:

    #define S__SUCCESS 0
    #define S__FAILURE 1
    
    #define m_err \
     if (ctrl->ists != S__SUCCESS) { return ctrl->ists; }
    
     void testFunc(CTRL *ctrl) {
         ctrl->ists =  S__FAILURE;
     }
    
     int main() {
         char lpMsg = NULL;
         CTRL ctrlw, *ctrl=&ctrlw;
    
         if ((lpMsg = malloc(256 * sizeof(char))) == NULL) return 1;
         memset(lpMsg, 0x00, sizeof(256 * sizeof(char));
    
         testFunc(ctrl);
         if (ctrl->ists != S__SUCCESS) {
             free(lpMsg);
             //lpMsg = NULL;
       }
       m_err;
    
       strcpy(lpMsg, "Hello");
       printf("lpMsg=%s\n", lpMsg);
    
       if (lpMsg != NULL) free(lpMsg);
       return 0;
     }
    

    if I uncomment the code lpMsg = NULL; then no doubleFree error will be reported!

     
  • CHR

    CHR - 2023-08-15

    Thanks for reporting, ticket is here: https://trac.cppcheck.net/ticket/11874

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.