diff --git a/example/ssh2.c b/example/ssh2.c index 0a77b326..ad03f3f1 100644 --- a/example/ssh2.c +++ b/example/ssh2.c @@ -199,8 +199,10 @@ int main(int argc, char *argv[]) char const *h = getenv("HOME"); if(!h || !*h) h = "."; - fn1sz = strlen(h) + strlen(pubkey) + 2; - fn2sz = strlen(h) + strlen(privkey) + 2; + /* Silence GCC -Wformat-truncation false positives by allocating + 2 extra bytes for each buffer. */ + fn1sz = strlen(h) + strlen(pubkey) + 2 + 2; + fn2sz = strlen(h) + strlen(privkey) + 2 + 2; fn1 = malloc(fn1sz); fn2 = malloc(fn2sz); if(!fn1 || !fn2) { @@ -210,18 +212,10 @@ int main(int argc, char *argv[]) goto shutdown; } /* Avoid false positives */ -#if defined(__GNUC__) && __GNUC__ >= 7 -#pragma GCC diagnostic push -#pragma GCC diagnostic warning "-Wformat-truncation=1" -#endif /* Using asprintf() here would be much cleaner, but less portable */ snprintf(fn1, fn1sz, "%s/%s", h, pubkey); snprintf(fn2, fn2sz, "%s/%s", h, privkey); -#if defined(__GNUC__) && __GNUC__ >= 7 -#pragma GCC diagnostic pop -#endif - if(libssh2_userauth_publickey_fromfile(session, username, fn1, fn2, password)) {