我需要在我的C++程序中的C函数,相克编译代码时++,我得到以下警告:克++ + strncat函数:可能导致目标缓冲区溢出
In function ‘char* strncat(char*, const char*, size_t)’,
inlined from ‘int get_usage(pid_t, pstat*)’ at src/getusage.c:24:
/usr/include/bits/string3.h:154: warning: call to
char* __builtin___strncat_chk(char*, const char*, long unsigned int,
long unsigned int) might overflow destination buffer
代码:
int pidof(const char* process_name){
char cmd[50] ="pidof ";
strncat(cmd, process_name, sizeof(cmd) - strlen(cmd) -1);
[..]
如何摆脱此警告?
欢迎来到StackOverflow!请看周围的常见问题:http://stackoverflow.com/faq – 2010-12-12 15:53:32
ouh,愚蠢的错误:)我误读错误消息,错误是在另一个strncat使用,我只通过缓冲区的大小作为大小参数=>固定:) – fho 2010-12-12 16:14:30
你应该使用std :: string。它们更易于使用。已经构建了对连接的支持,并且它们也更安全。 – bruno 2010-12-12 16:20:15