1
0
mirror of https://github.com/libssh2/libssh2.git synced 2025-07-31 00:03:08 +03:00

example, tests: fix/silence -Wformat-truncation=2 gcc warnings

Then sync this warning option with curl.

Seems like a false positive and/or couldn't figure how to fix it, so silence:
```
example/ssh2.c:227:38: error: '%s' directive output may be truncated writing likely 1 or more bytes into a region of size 0 [-Werror=format-truncation=]
  227 |             snprintf(fn1, fn1sz, "%s/%s", h, pubkey);
      |                                      ^~
example/ssh2.c:227:34: note: assuming directive output of 1 byte
  227 |             snprintf(fn1, fn1sz, "%s/%s", h, pubkey);
      |                                  ^~~~~~~
example/ssh2.c:227:13: note: 'snprintf' output 3 or more bytes (assuming 4) into a destination of size 2
  227 |             snprintf(fn1, fn1sz, "%s/%s", h, pubkey);
      |             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
example/ssh2.c:228:38: error: '%s' directive output may be truncated writing likely 1 or more bytes into a region of size 0 [-Werror=format-truncation=]
  228 |             snprintf(fn2, fn2sz, "%s/%s", h, privkey);
      |                                      ^~
example/ssh2.c:228:34: note: assuming directive output of 1 byte
  228 |             snprintf(fn2, fn2sz, "%s/%s", h, privkey);
      |                                  ^~~~~~~
example/ssh2.c:228:13: note: 'snprintf' output 3 or more bytes (assuming 4) into a destination of size 2
  228 |             snprintf(fn2, fn2sz, "%s/%s", h, privkey);
      |             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
```
Ref: https://github.com/libssh2/libssh2/actions/runs/7055480458/job/19205970397#step:10:98

Fix:
```
tests/openssh_fixture.c:116:38: error: ' 2>&1' directive output may be truncated writing 5 bytes into a region of size between 1 and 1024 [-Werror=format-truncation=]
tests/openssh_fixture.c:116:11: note: 'snprintf' output between 6 and 1029 bytes into a destination of size 1024
```
Ref: https://github.com/libssh2/libssh2/actions/runs/7055480458/job/19205969221#step:10:51

Tested via #1257
This commit is contained in:
Viktor Szakats
2023-12-01 03:19:04 +00:00
parent 2e57dcb9d2
commit 744e059f31
4 changed files with 11 additions and 3 deletions

View File

@ -499,7 +499,7 @@ AC_DEFUN([CURL_CC_DEBUG_OPTS],
CURL_ADD_COMPILER_WARNINGS([tmp_CFLAGS], [restrict]) CURL_ADD_COMPILER_WARNINGS([tmp_CFLAGS], [restrict])
CURL_ADD_COMPILER_WARNINGS([tmp_CFLAGS], [alloc-zero]) CURL_ADD_COMPILER_WARNINGS([tmp_CFLAGS], [alloc-zero])
tmp_CFLAGS="$tmp_CFLAGS -Wformat-overflow=2" tmp_CFLAGS="$tmp_CFLAGS -Wformat-overflow=2"
tmp_CFLAGS="$tmp_CFLAGS -Wformat-truncation=1" # =2 causes false positives tmp_CFLAGS="$tmp_CFLAGS -Wformat-truncation=2"
fi fi
# #
dnl Only gcc 10 or later dnl Only gcc 10 or later

View File

@ -193,7 +193,7 @@ elseif(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX OR CMAKE_C_COMPILER_I
-Walloc-zero # gcc 7.0 -Walloc-zero # gcc 7.0
-Wduplicated-branches # gcc 7.0 -Wduplicated-branches # gcc 7.0
-Wformat-overflow=2 # gcc 7.0 -Wformat-overflow=2 # gcc 7.0
-Wformat-truncation=1 # gcc 7.0 (=2 causes false positives) -Wformat-truncation=2 # gcc 7.0
-Wrestrict # gcc 7.0 -Wrestrict # gcc 7.0
) )
endif() endif()

View File

@ -222,10 +222,18 @@ int main(int argc, char *argv[])
fprintf(stderr, "out of memory\n"); fprintf(stderr, "out of memory\n");
goto shutdown; 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, /* Using asprintf() here would be much cleaner,
but less portable */ but less portable */
snprintf(fn1, fn1sz, "%s/%s", h, pubkey); snprintf(fn1, fn1sz, "%s/%s", h, pubkey);
snprintf(fn2, fn2sz, "%s/%s", h, privkey); snprintf(fn2, fn2sz, "%s/%s", h, privkey);
#if defined(__GNUC__) && __GNUC__ >= 7
#pragma GCC diagnostic pop
#endif
if(libssh2_userauth_publickey_fromfile(session, username, if(libssh2_userauth_publickey_fromfile(session, username,
fn1, fn2, fn1, fn2,

View File

@ -85,7 +85,7 @@ static int run_command_varg(char **output, const char *command, va_list args)
FILE *pipe; FILE *pipe;
char command_buf[BUFSIZ]; char command_buf[BUFSIZ];
char buf[BUFSIZ]; char buf[BUFSIZ + sizeof(redirect_stderr)];
int ret; int ret;
size_t buf_len; size_t buf_len;