free() on stack memory

后端 未结 5 1191
余生分开走
余生分开走 2020-12-06 18:07

I\'m supporting some c code on Solaris, and I\'ve seen something weird at least I think it is:

char new_login[64];
...
strcpy(new_login, (char *)login);
...
         


        
5条回答
  •  不知归路
    2020-12-06 18:52

    No. This is a bug.

    According to free(3)....

    free() frees the memory space pointed to by ptr, which must have been returned by a previous call to malloc(), calloc() or realloc(). Otherwise, or if free(ptr) has already been called before, undefined behaviour occurs. If ptr is NULL, no operation is performed.

    So you have undefined behavior happening in your program.

提交回复
热议问题