Bug 90010 - [8 Regression] valgrind error with snprintf and -Wall
Summary: [8 Regression] valgrind error with snprintf and -Wall
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 9.0
: P2 normal
Target Milestone: 8.4
Assignee: Jakub Jelinek
URL:
Keywords: diagnostic
Depends on:
Blocks:
 
Reported: 2019-04-08 16:47 UTC by David Binderman
Modified: 2019-05-01 07:18 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Known to work: 7.3.0, 8.3.1, 9.0
Known to fail: 8.3.0
Last reconfirmed: 2019-04-09 00:00:00


Attachments
gcc9-pr90010.patch (1017 bytes, patch)
2019-04-09 12:32 UTC, Jakub Jelinek
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description David Binderman 2019-04-08 16:47:22 UTC
For this C++ source code:

extern "C" __inline __attribute__((__gnu_inline__)) int snprintf(...) {}
class a {
  char b[4096];
  void c();
};
void a::c() {
  char d[4096];
  snprintf(d, sizeof(d), "%s/power/runtime_suspended_time", b);
}

on a valgrind version of recent gcc trunk:

$ ~/gcc/results.270150.valgrind/bin/g++ -v
Using built-in specs.
COLLECT_GCC=/home/dcb/gcc/results.270150.valgrind/bin/g++
COLLECT_LTO_WRAPPER=/home/dcb/gcc/results.270150.valgrind/libexec/gcc/x86_64-pc-linux-gnu/9.0.1/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: ../trunk/configure --prefix=/home/dcb/gcc/results.270150.valgrind --disable-bootstrap --disable-multilib --disable-werror --enable-checking=valgrind --enable-languages=c,c++,fortran
Thread model: posix
gcc version 9.0.1 20190404 (experimental) (GCC) 
[dcb@localhost dcbTest]$ 

with compile flag -Wall, makes this:

$ ~/gcc/results.270150.valgrind/bin/g++ -c -Wall bug514.cc
bug514.cc:1:57: warning: declaration of ‘int snprintf(...)’ conflicts with built-in declaration ‘int snprintf(char*, long unsigned int, const char*, ...)’ [-Wbuiltin-declaration-mismatch]
    1 | extern "C" __inline __attribute__((__gnu_inline__)) int snprintf(...) {}
      |                                                         ^~~~~~~~
bug514.cc: In function ‘int snprintf(...)’:
bug514.cc:1:72: warning: no return statement in function returning non-void [-Wreturn-type]
    1 | extern "C" __inline __attribute__((__gnu_inline__)) int snprintf(...) {}
      |                                                                        ^
==30913== Conditional jump or move depends on uninitialised value(s)
==30913==    at 0x483BB9D: strnlen (vg_replace_strmem.c:428)
==30913==    by 0x137D3F3: pp_format(pretty_printer*, text_info*) (pretty-print.c:1374)
==30913==    by 0x1373D62: diagnostic_report_diagnostic(diagnostic_context*, diagnostic_info*) (diagnostic.c:1015)
==30913==    by 0xB5F828: format_string_diagnostic_t::emit_warning_n_va(int, unsigned long, char const*, char const*, __va_list_tag (*) [1]) const (substring-locations.c:216)
Comment 1 David Binderman 2019-04-08 16:50:49 UTC
I forgot to mention that I have also set a valgrind option:

$ set | fgrep VAL
VALGRIND_OPTS=--expensive-definedness-checks=yes
$

Might be significant.
Comment 2 Martin Liška 2019-04-09 08:38:56 UTC
Confirmed, started with r247401.
Comment 3 Jakub Jelinek 2019-04-09 12:32:08 UTC
Created attachment 46113 [details]
gcc9-pr90010.patch

Untested fix.
Comment 4 Jakub Jelinek 2019-04-09 12:39:41 UTC
Not an ICE actually, just printing random bytes (1/2/3 at most) at the end of the %qs string instead of the bytes that should be there.
Comment 5 Jakub Jelinek 2019-04-10 07:27:52 UTC
Author: jakub
Date: Wed Apr 10 07:27:20 2019
New Revision: 270246

URL: https://gcc.gnu.org/viewcvs?rev=270246&root=gcc&view=rev
Log:
	PR c++/90010
	* gimple-ssa-sprintf.c (target_to_host): Fix handling of targstr
	with strlen in between hostsz-3 and hostsz-1 inclusive when no
	translation is needed, and when translation is needed, only append
	... if the string length is hostsz or more bytes long.  Avoid using
	strncpy or strcat.

	* gcc.dg/pr90010.c: New test.

Added:
    trunk/gcc/testsuite/gcc.dg/pr90010.c
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/gimple-ssa-sprintf.c
    trunk/gcc/testsuite/ChangeLog
Comment 6 Jakub Jelinek 2019-04-10 07:33:34 UTC
Fixed on the trunk so far.
Comment 7 Jakub Jelinek 2019-04-30 21:01:14 UTC
Author: jakub
Date: Tue Apr 30 21:00:42 2019
New Revision: 270746

URL: https://gcc.gnu.org/viewcvs?rev=270746&root=gcc&view=rev
Log:
	2019-04-10  Jakub Jelinek  <jakub@redhat.com>

	PR c++/90010
	* gimple-ssa-sprintf.c (target_to_host): Fix handling of targstr
	with strlen in between hostsz-3 and hostsz-1 inclusive when no
	translation is needed, and when translation is needed, only append
	... if the string length is hostsz or more bytes long.  Avoid using
	strncpy or strcat.

	* gcc.dg/pr90010.c: New test.

Added:
    branches/gcc-8-branch/gcc/testsuite/gcc.dg/pr90010.c
Modified:
    branches/gcc-8-branch/gcc/ChangeLog
    branches/gcc-8-branch/gcc/gimple-ssa-sprintf.c
    branches/gcc-8-branch/gcc/testsuite/ChangeLog
Comment 8 Jakub Jelinek 2019-05-01 07:18:02 UTC
Fixed for 8.4+ too.