You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@stdcxx.apache.org by Jeremy Dean <de...@roguewave.com> on 2007/06/22 22:44:46 UTC

Getting incorrect behavior on strstream

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/

Re: Getting incorrect behavior on strstream

Posted by Martin Sebor <se...@roguewave.com>.
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/
> 


RE: Getting incorrect behavior on strstream

Posted by Jeremy Dean <de...@roguewave.com>.
Ok, so I added the S << std::ends; to each of the stream statements.
This did resolve it on Red hat, however on Solaris for the first
testfunction it did not:

void test1() {
    std::ostringstream S;
    long double x = std::pow(1e300,2);
    S << "Something " <<  std::setprecision(8) << x;
    S << " else";
    S << std::ends;
    std::printf("Test1: %s\n", S.str().c_str()); 
}

On solaris it is printing up until the double and then nothing else is
printed.  However if I change the precision to less then 8 then it works
fine.

Jeremy


-----Original Message-----
From: Martin Sebor [mailto:sebor@roguewave.com] 
Sent: Monday, June 25, 2007 11:36 AM
To: stdcxx-dev@incubator.apache.org
Subject: Re: Getting incorrect behavior on strstream

Farid Zaripov wrote:
>> -----Original Message-----
>> From: Jeremy Dean [mailto:dean@roguewave.com]
>> Sent: Friday, June 22, 2007 11:45 PM
>> To: stdcxx-dev@incubator.apache.org
>> Subject: Getting incorrect behavior on strstream
>>
>> I have a testcase that is showing incorrect behavior ostrstream or
>> ostringstream:
> 
> [...]
> 
>> Any thoughts on this problem?
> 
>   ostrstream::str() returns just the beginning pointer to the
sequence.
> The sequence don't
> have the null character unless you especially inserted it. You should 
> print no more that
> otrstream::pcount() characters.
> 
>   See Remarks on the page

Better yet, refer to the stdcxx Class Reference :)

http://incubator.apache.org/stdcxx/doc/stdlibref/strstreambuf.html#idx12
24

Martin


Re: Getting incorrect behavior on strstream

Posted by Martin Sebor <se...@roguewave.com>.
Farid Zaripov wrote:
>> -----Original Message-----
>> From: Jeremy Dean [mailto:dean@roguewave.com] 
>> Sent: Friday, June 22, 2007 11:45 PM
>> To: stdcxx-dev@incubator.apache.org
>> Subject: Getting incorrect behavior on strstream
>>
>> I have a testcase that is showing incorrect behavior ostrstream or
>> ostringstream:
> 
> [...]
> 
>> Any thoughts on this problem?
> 
>   ostrstream::str() returns just the beginning pointer to the sequence.
> The sequence don't
> have the null character unless you especially inserted it. You should
> print no more that
> otrstream::pcount() characters.
> 
>   See Remarks on the page

Better yet, refer to the stdcxx Class Reference :)

http://incubator.apache.org/stdcxx/doc/stdlibref/strstreambuf.html#idx1224

Martin


RE: Getting incorrect behavior on strstream

Posted by Farid Zaripov <Fa...@epam.com>.
> -----Original Message-----
> From: Jeremy Dean [mailto:dean@roguewave.com] 
> Sent: Friday, June 22, 2007 11:45 PM
> To: stdcxx-dev@incubator.apache.org
> Subject: Getting incorrect behavior on strstream
> 
> I have a testcase that is showing incorrect behavior ostrstream or
> ostringstream:

[...]

> Any thoughts on this problem?

  ostrstream::str() returns just the beginning pointer to the sequence.
The sequence don't
have the null character unless you especially inserted it. You should
print no more that
otrstream::pcount() characters.

  See Remarks on the page
https://msdn2.microsoft.com/en-us/library/k1z9z9cf(VS.80).aspx

Farid.