警告:调用__builtin___strncat_chk可能会溢出目标缓冲区[默认启用]在“ strncat”函数中

时间:2019-01-04 11:00:22

标签: c buffer overflow warnings

在编译“ C”语言文件时收到此警告。

In function ‘strncat’,inlined from ‘O281DC3F563F92003x’ at util.c:817:13:
/usr/arm-linux-gnueabihf/include/bits/string3.h:152:3: warning: call to 
__builtin___strncat_chk might overflow destination buffer [enabled by 
default]

In function ‘strncat’,inlined from ‘UFE191C0002FB606Eb’ at util.c:3231:25:
/usr/arm-linux-gnueabihf/include/bits/string3.h:152:3: warning: call to 
__builtin___strncat_chk might overflow destination buffer [enabled by 
default]

In function ‘strncat’, 

如何删除这些警告?

1 个答案:

答案 0 :(得分:0)

this answer中所述,您可以轻松地将googlelogin() { var user = firebase.auth().currentUser; var name, email, photoUrl, uid, emailVerified; alert('hello'); return this.afAuth.auth.signInWithRedirect(new firebase.auth.GoogleAuthProvider()).then( newUser => { if (newUser != null) { name = newUser.displayName; email = newUser.email; photoUrl = newUser.photoURL; emailVerified = newUser.emailVerified; uid = newUser.uid; console.log(email); } }).catch( err => { console.log("Error signing in with Google!"); console.log(err); }); } 的用法替换为strncat,这样可以将缓冲区的位置作为参数并使其可以安全使用。

snprintf示例:

strncat

更安全的#define BUF_SIZE 32 char buf[BUF_SIZE]; strncpy(buf, "foo", sizeof(buf) -1); strncat(buf, "bar", sizeof(buf) - strlen(buf) -1); 示例:

snprintf

该方法的缺点是,如果您实际上想连接为:

#define BUF_SIZE 32
char buf[BUF_SIZE];

snprintf(buf, sizeof(buf), "%s%s", "foo", "bar");

导致未定义的行为。