Consequences of this buffer overflow?

后端 未结 11 2056
悲&欢浪女
悲&欢浪女 2021-01-01 15:44

So here I believe I have a small buffer overflow problem I found when reviewing someone else\'s code. It immediately struck me as incorrect, and potentially dangerous, but a

11条回答
  •  情歌与酒
    2021-01-01 15:53

    Your real problem is that you're writing

    char* buffer = new char[strlen("This string is 27 char long" + 1)];
    

    instead of

    char* buffer = new char[strlen("This string is 27 char long") + 1];
    

    Meaning that on the first one you're giving strlen() an address which isn't the beginning of your string.

    Try this code:

    const char szText[] = "This string is 27 char long";
    char* buffer = new char[strlen(szText) + 1];
    sprintf(buffer, szText);
    

提交回复
热议问题