stdcxx-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Martin Sebor <se...@roguewave.com>
Subject Re: Getting incorrect behavior on strstream
Date Mon, 25 Jun 2007 19:55:40 GMT
I agree with Farid that test2() and test3() exhibit undefined
behavior because they both fail to append the required NUL to
the string before formatting it via the "%s" printf directive.

But the test program does demonstrate a real problem, and that
is the formatting of infinity when the stream precision is
greater than 7. It looks as though the num_put facet inserts
the string "inf\0\0ity" into the stream rather than "inf".
A small test case is below.

Please go ahead and open an issue for this in Jira.

Martin

$ cat u.cpp && make u && ./u | od -c
#include <cassert>
#include <iostream>
#include <sstream>

int main () {
     std::ostringstream s;
     s.precision (8);
     s << 1 / 0.0;
     std::cout << s.str () << '\n';
     assert (s.str () == "inf");
}
gcc -c -I/build/sebor/stdcxx/include/ansi -D_RWSTDDEBUG 
-D_RWSTD_USE_CONFIG -I/build/sebor/stdcxx/include 
-I/build/sebor/stdcxx-gcc-4.1.0-11s/include 
-I/build/sebor/stdcxx/examples/include  -pedantic -nostdinc++ -g  -W 
-Wall -Wcast-qual -Winline -Wshadow -Wwrite-strings -Wno-long-long   u.cpp
u.cpp: In function 'int main()':
u.cpp:8: warning: division by zero in '1 / 0.'
gcc u.o -o u  -L/build/sebor/stdcxx-gcc-4.1.0-11s/lib  -lstd11s -lsupc++ 
-lm
Assertion failed: s.str () == "inf", file u.cpp, line 10
0000000   i   n   f  \0  \0   i   t   y  \n
0000011

Jeremy Dean wrote:
> I have a testcase that is showing incorrect behavior ostrstream or
> ostringstream:
>  
> #include <cstdio>
> #include <iostream>
> #include <iomanip>
> #include <sstream>
> #include <strstream>
> #include <cmath>
>  
> void test1() {
>     std::ostringstream S;
>     long double x = std::pow(1e300,2);
>     S << "Something " <<  std::setprecision(8) << x;
>     S << " else";
>     std::printf("Test1: %s\n", S.str().c_str());
> }
> void test2() {
>     std::ostrstream S;
>     long double x = std::pow(1e300,2);
>     S << "Something " << std::setprecision(7) << x;
>     S << " else";
>     std::printf("Test2: %s\n", S.str());
> }
> void test3() {
>     std::ostrstream S;
>     long double x = 0.0/0.0;
>     S << "Something " << std::setprecision(8) << x;
>     S << " else";
>     std::printf("Test3: %s\n", S.str());
> }
> int main(int argc, char* argv[]) {
>     test1();
>     test2();
>     test3();
> }
>  
>  
> On solaris 8 with Sun Studio 8 patch 14 I get the following output
>  
> Test1: Something inf
> Test2: Something inf else else
> Test3: Something nan else
>  
> Test1 is missing else
> Test2 has an extra else
> Test3 looks ok.
>  
> On RedHat 3u6 I get the following output:
>  
> Test1: Something inf else
> Test2: Something inf else
> Test3: Something nan elseing inf else
>  
> Test1 and 2 look ok
> Test3 however has extra output "ing inf else"
>  
> Any thoughts on this problem?
>  
> Thanks,
>  
> Jeremy
>  
> Jeremy Dean 
> Rogue Wave Software,
> A QUOVADX(tm) division 
> Technical Support 
> Phone: 303-545-3205 -- 1-800-404-4767 
> E-mail: support@roguewave.com 
> Web: http://www.roguewave.com/support 
> Knowledge Base entries: 
> http://www.roguewave.com/kbdocs/search.html 
> View issues online at: 
> http://www.roguewave.com/youraccount/login/
> 


Mime
View raw message