https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78977

            Bug ID: 78977
           Summary: g++7 snprintf() of double produces wrong code with -O3
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: h2+bugs at fsfe dot org
  Target Milestone: ---

We have current snapshots of g++ in our nightly build matrix and are seeing two
tests fail at run-time for strange reasons.

% g++7 --version
g++7 (FreeBSD Ports Collection) 7.0.0 20161225 (experimental)

% uname -a
FreeBSD celegans.imp.fu-berlin.de 11.0-RELEASE-p1 FreeBSD 11.0-RELEASE-p1 #0
r306420: Thu Sep 29 01:43:23 UTC 2016    
r...@releng2.nyi.freebsd.org:/usr/obj/usr/src/sys/GENERIC  amd64


Both tests involve the following function (although other passing tests use
this code, too):

template <typename TTarget>
inline typename Size<TTarget>::Type
appendNumber(TTarget & target, double source)
{
    char buffer[32];
    size_t len = snprintf(buffer, sizeof(buffer), "%g", source);
    write(target, (char *)buffer, len);
    return len;
}


An example of test_random in our build matrix is here:
http://cdash.seqan.de/testSummary.php?project=1&name=test_test_random&date=2017-01-03

test_random_beta_write fails with:

/home/mi/h4nn3s/nightly-builds/testroot/checkout-develop/FreeBSD-11.0-RELEASE-p1_g++7_64/tests/random/test_random_beta.h:130
Assertion failed : CharString("~Beta(0.5,0.3)") == os.str() was: ~Beta(0.5,0.3)
!= ~Beta(0.5,0.3
[NON-XML-CHAR-0x8]


)

Or sometimes just:
CharString("~Beta(0.5,0.3)") == os.str() was: ~Beta(0.5,0.3) != ~Beta(0.5,0.3

From the debugger the string looks like:
1 = "~Beta(0.5,0.3\000\377\177\000\000)"

So apparently some bogus content from behind the double is included in the
string.

The issue only appears with -O3 , not with any lower optimization levels. This
makes it really hard for me to debug, because all the relevant code is
optimized out.

Can you help me with this? What do you need for further analysis? Unfortunately
I have not yet been able to extract a minimal working example outside our test
matrix, a regular call to snprintf seems to work. And I know that more than the
two failing tests use the code in question so it's definitely not something
always fails (although it is reproducible on exactly these two tests). 

Thank you for your help!

Reply via email to