strncpy doesn't always null-terminate

后端 未结 5 967
伪装坚强ぢ
伪装坚强ぢ 2020-12-18 08:23

I am using the code below:

char filename[ 255 ];
strncpy( filename, getenv( \"HOME\" ), 235 );
strncat( filename, \"/.config/stationlist.xml\", 255 );
         


        
5条回答
  •  没有蜡笔的小新
    2020-12-18 08:55

    1) Your strncpy does not necessarily null-terminate filename. In fact, if getenv("HOME") is longer than 235 characters and getenv("HOME")[234] is not a 0, it won't. 2) Your strncat() may attempt to extend filename beyond 255 characters, because, as it says,

    3rd parameter is the maximum number of characters to append. 
    

    (not the total allowed length of dst)

提交回复
热议问题