From 165a9c3bee773935449786968abbc3bd00a91836 Mon Sep 17 00:00:00 2001 From: Viktor Szakats Date: Sun, 21 Jun 2026 04:28:27 +0200 Subject: [PATCH] example/ssh2: replace GCC silencer pragma with a simpler workaround MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit As of gcc-16, this still causes a false positive: ``` example/ssh2.c: In function 'main': example/ssh2.c:215:34: error: '__builtin_snprintf' output may be truncated before the last format character [-Werror=format-truncation=] 215 | snprintf(fn1, fn1sz, "%s/%s", h, pubkey); | ^~~~~~~ example/ssh2.c:215:40: note: format string is defined here 215 | snprintf(fn1, fn1sz, "%s/%s", h, pubkey); | ^ example/ssh2.c:215:13: note: '__builtin_snprintf' output 17 or more bytes (assuming 18) into a destination of size 17 215 | snprintf(fn1, fn1sz, "%s/%s", h, pubkey); | ^~~~~~~~ example/ssh2.c:216:34: error: '__builtin_snprintf' output may be truncated before the last format character [-Werror=format-truncation=] 216 | snprintf(fn2, fn2sz, "%s/%s", h, privkey); | ^~~~~~~ example/ssh2.c:216:40: note: format string is defined here 216 | snprintf(fn2, fn2sz, "%s/%s", h, privkey); | ^ example/ssh2.c:216:13: note: '__builtin_snprintf' output 13 or more bytes (assuming 14) into a destination of size 13 216 | snprintf(fn2, fn2sz, "%s/%s", h, privkey); | ^~~~~~~~ ``` By bumping the allocated buffers by one byte, the above disappears, but remains in gcc-13. Fix by bumping buffer by one more byte. ``` example/ssh2.c: In function ‘main’: example/ssh2.c:228:40: error: ‘snprintf’ output may be truncated before the last format character [-Werror=format-truncation=] 228 | snprintf(fn1, fn1sz, "%s/%s", h, pubkey); | ^ example/ssh2.c:228:13: note: ‘snprintf’ output 3 or more bytes (assuming 4) into a destination of size 3 228 | snprintf(fn1, fn1sz, "%s/%s", h, pubkey); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ example/ssh2.c:229:40: error: ‘snprintf’ output may be truncated before the last format character [-Werror=format-truncation=] 229 | snprintf(fn2, fn2sz, "%s/%s", h, privkey); | ^ example/ssh2.c:229:13: note: ‘snprintf’ output 3 or more bytes (assuming 4) into a destination of size 3 229 | snprintf(fn2, fn2sz, "%s/%s", h, privkey); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ``` Ref: https://github.com/libssh2/libssh2/actions/runs/27890978881/job/82534253967#step:24:67 Closes #2100 --- example/ssh2.c | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) 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)) {