Is it secure to use malloc?

前端 未结 12 892
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-05 04:18

Somebody told me that allocating with malloc is not secure anymore, I\'m not a C/C++ guru but I\'ve made some stuff with malloc and C/C++. Does anyone know about what risks

12条回答
  •  清歌不尽
    2021-01-05 05:06

    Your friend could be talking about:

    • The safety of using pointers in general. For example in C++ if you're allocating an array of char with malloc, question why you aren't using a string or vector. Pointers aren't insecure, but code that's buggy due to incorrect use of pointers is.

    • Something about malloc in particular. Most OSes clear memory before first handing it to a process, for security reasons. Otherwise, sensitive data from one app, could be leaked to another app. On OSes that don't do that, you could argue that there's an insecurity related to malloc. It's really more related to free.

    It's also possible your friend doesn't know what he's talking about. When someone says "X is insecure", my response is, "in what way?".

提交回复
热议问题