Why are strlcpy and strlcat considered insecure?

前端 未结 7 1039
一整个雨季
一整个雨季 2020-11-22 05:49

I understand that strlcpy and strlcat were designed as secure replacements for strncpy and strncat. However, some people

7条回答
  •  情歌与酒
    2020-11-22 06:25

    There are two "problems" related to using strl functions:

    1. You have to check return values to avoid truncation.

    The c1x standard draft writers and Drepper, argue that programmers won't check the return value. Drepper says we should somehow know the length and use memcpy and avoid string functions altogether, The standards committee argues that the secure strcpy should return nonzero on truncation unless otherwise stated by the _TRUNCATE flag. The idea is that people are more likely to use if(strncpy_s(...)).

    1. Cannot be used on non-strings.

    Some people think that string functions should never crash even when fed bogus data. This affects standard functions such as strlen which in normal conditions will segfault. The new standard will include many such functions. The checks of course have a performance penalty.

    The upside over the proposed standard functions is that you can know how much data you missed with strl functions.

提交回复
热议问题