Consequences of this buffer overflow?

后端 未结 11 2006
悲&欢浪女
悲&欢浪女 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 16:06

    Correct statement. Since you are passing address of the second character of the string to strlen(), you are getting the length one character less as a result. Aside from that, the main problem is with sprintf(), that's one of the reasons that it's not safe.

    Even this compiles and executes (may also crash).

        char* x = new char;
        sprintf(x, "This is way longer than one character");
        printf("%s", x);
    

    In order to avoid this dangerous issue, you should use safe versions of this function like snprintf() or asprintf() under GCC or sprintf_s() under MSVC.

    As references, please have a look at The GNU C Library documentation in this regard and also security note of MSDN's sprintf() article.

提交回复
热议问题