c99 goto past initialization

后端 未结 7 1972
被撕碎了的回忆
被撕碎了的回忆 2020-11-30 07:34

While debugging a crash, I came across this issue in some code:

int func()
{
    char *p1 = malloc(...);
    if (p1 == NULL)
        goto err_exit;

    char         


        
7条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-30 08:05

    This is not a bug in gcc. A jump is just a jump in C. There is no special logic applied. The issue is that you are not initializing your pointers to NULL first. If you were to do that then you free call would be free(NULL) which would not crash. Start the function with char *p1 = NULL, *p2 = NULL; and all will be well.

提交回复
热议问题