Peculiar bug when converting int's to std::strings using MSVC 2015

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Peculiar bug when converting int's to std::strings using MSVC 2015

Arash Partow-2
Hello all,

I've got the following bit of code, it compiles under all compilers
I've tried (gcc, clang, msvc (10,15,17), intel).

https://gist.github.com/anonymous/e9a7eafbbddf3cc8289f7346329126ab

The problem seems to only occur under msvc2105 Update3 release build.
All other compilers/versions and debug/release builds all give a
correct result.

Here are the first few outputs:

cmd> spirit.exe
ERROR s1:-4254967296    s2:-40000000
ERROR s1:-4254967297    s2:-39999999
ERROR s1:-4254967298    s2:-39999998
ERROR s1:-4254967299    s2:-39999997
ERROR s1:-4254967300    s2:-39999996
ERROR s1:-4254967301    s2:-39999995

I was wondering if anyone else can replicate this on an MSVC 2015 u3
release build? Or is there something wrong in the code - I've been
using that particular conversion routine for several years now.



Regards,
Arash


.

------------------------------------------------------------------------------
Announcing the Oxford Dictionaries API! The API offers world-renowned
dictionary content that is easy and intuitive to access. Sign up for an
account today to start using our lexical data to power your apps and
projects. Get started today and enter our developer competition.
http://sdm.link/oxford
_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general
Reply | Threaded
Open this post in threaded view
|

Re: Peculiar bug when converting int's to std::strings using MSVC 2015

sehe
On 08-03-17 09:21, Arash Partow wrote:
> ERROR s1:-4254967296    s2:-40000000
> ERROR s1:-4254967297    s2:-39999999
> ERROR s1:-4254967298    s2:-39999998
> ERROR s1:-4254967299    s2:-39999997
> ERROR s1:-4254967300    s2:-39999996
> ERROR s1:-4254967301    s2:-39999995

Can you specify what the values for i are? Perhaps just replace:

 printf("ERROR
s1:%s\ts2:%s\n",s1.c_str(),s2.c_str());                                                                                                                


With

 std::cout << "Mismatch: i=" << i << " s1=" << s1 << " s2=" << s2 <<
"\n";                                                                                            



------------------------------------------------------------------------------
Announcing the Oxford Dictionaries API! The API offers world-renowned
dictionary content that is easy and intuitive to access. Sign up for an
account today to start using our lexical data to power your apps and
projects. Get started today and enter our developer competition.
http://sdm.link/oxford
_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general
Reply | Threaded
Open this post in threaded view
|

Re: Peculiar bug when converting int's to std::strings using MSVC 2015

Arash Partow-2
On Thu, Mar 9, 2017, Seth  wrote:

>
> Can you specify what the values for i are? Perhaps just replace:
>
>  printf("ERROR
> s1:%s\ts2:%s\n",s1.c_str(),s2.c_str());
>
>
> With
>
>  std::cout << "Mismatch: i=" << i << " s1=" << s1 << " s2=" << s2 <<
> "\n";
>
>

I've modified the example code as suggested.

https://gist.github.com/anonymous/b3987d1514fe64dc9a4cd13c7cfd9f09


Here is the output of the first several lines:

Mismatch: i=-40000000 s1=-4254967296 s2=-40000000
Mismatch: i=-39999999 s1=-4254967297 s2=-39999999
Mismatch: i=-39999998 s1=-4254967298 s2=-39999998
Mismatch: i=-39999997 s1=-4254967299 s2=-39999997
Mismatch: i=-39999996 s1=-4254967300 s2=-39999996
Mismatch: i=-39999995 s1=-4254967301 s2=-39999995
Mismatch: i=-39999994 s1=-4254967302 s2=-39999994
Mismatch: i=-39999993 s1=-4254967303 s2=-39999993
Mismatch: i=-39999992 s1=-4254967304 s2=-39999992
Mismatch: i=-39999991 s1=-4254967305 s2=-39999991
Mismatch: i=-39999990 s1=-4254967306 s2=-39999990
Mismatch: i=-39999989 s1=-4254967307 s2=-39999989
Mismatch: i=-39999988 s1=-4254967308 s2=-39999988
Mismatch: i=-39999987 s1=-4254967309 s2=-39999987
Mismatch: i=-39999986 s1=-4254967310 s2=-39999986
Mismatch: i=-39999985 s1=-4254967311 s2=-39999985
Mismatch: i=-39999984 s1=-4254967312 s2=-39999984
Mismatch: i=-39999983 s1=-4254967313 s2=-39999983
Mismatch: i=-39999982 s1=-4254967314 s2=-39999982
Mismatch: i=-39999981 s1=-4254967315 s2=-39999981


As before all the values are a 'mismatch'.



Arash

------------------------------------------------------------------------------
Announcing the Oxford Dictionaries API! The API offers world-renowned
dictionary content that is easy and intuitive to access. Sign up for an
account today to start using our lexical data to power your apps and
projects. Get started today and enter our developer competition.
http://sdm.link/oxford
_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general
Reply | Threaded
Open this post in threaded view
|

Re: Peculiar bug when converting int's to std::strings using MSVC 2015

sehe
On 08-03-17 19:10, Arash Partow wrote:
As before all the values are a 'mismatch'.

Oh. You mean not just the ones shown. I had assumed you listed only the failing cases. Because that's the output of the program. So, to clarify even more, do you mean, the following program fails too? http://coliru.stacked-crooked.com/a/2715e11d1592fd07

#include <boost/spirit/include/karma.hpp>

inline std::string karma_int_to_string(int value) {
    namespace k = boost::spirit::karma;
    std::string str;
    k::generate(back_inserter(str), k::int_, value);
    return str;
}

int main() {
    assert(std::to_string(42) == karma_int_to_string(42));
}

If so, there's obviously some kind of UB involved. But it's not very clear to me what could cause it (barring MSVC bugs)


------------------------------------------------------------------------------
Announcing the Oxford Dictionaries API! The API offers world-renowned
dictionary content that is easy and intuitive to access. Sign up for an
account today to start using our lexical data to power your apps and
projects. Get started today and enter our developer competition.
http://sdm.link/oxford
_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general
Reply | Threaded
Open this post in threaded view
|

Re: Peculiar bug when converting int's to std::strings using MSVC 2015

Arash Partow-2
On Thu, Mar 9, 2017, Seth wrote:
>
> Oh. You mean not just the ones shown. I had assumed you listed only the
> failing cases.

Yes every value conversion attempt in that loop fails.



> Because that's the output of the program. So, to clarify even
> more, do you mean, the following program fails too?
> http://coliru.stacked-crooked.com/a/2715e11d1592fd07
>
> #include <boost/spirit/include/karma.hpp>
>
> inline std::string karma_int_to_string(int value) {
>     namespace k = boost::spirit::karma;
>     std::string str;
>     k::generate(back_inserter(str), k::int_, value);
>     return str;
> }
>
> int main() {
>     assert(std::to_string(42) == karma_int_to_string(42));
> }
>

Your example works fine on all the compilers I tested - including
MSVC2015 U3 which was the only compiler giving the erroneous result.




>
> If so, there's obviously some kind of UB involved. But it's not very clear
> to me what could cause it (barring MSVC bugs)
>


I've been using the originally described routine, as it was the one
recommended by Joel and Hartmut back in 2009 for inclusion in a string
processing benchmark.

Now given that this 'new' version functions correctly even using the
'buggy'  compiler - do you recommend it be used?

I ask because I reran the benchmark (using MSVC2017) and the new
routine is considerably slower than the original routine.

https://gist.github.com/anonymous/c1d91acd1bf8ac7f4fdfbeecbd102639

Similar results are obtained when using GCC/Clang.


The benchmark code:

https://github.com/ArashPartow/strtk/blob/master/strtk_tokenizer_cmp.cpp#L901




Arash

------------------------------------------------------------------------------
Announcing the Oxford Dictionaries API! The API offers world-renowned
dictionary content that is easy and intuitive to access. Sign up for an
account today to start using our lexical data to power your apps and
projects. Get started today and enter our developer competition.
http://sdm.link/oxford
_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general
Reply | Threaded
Open this post in threaded view
|

Re: Peculiar bug when converting int's to std::strings using MSVC 2015

sehe
On 09-03-17 00:33, Arash Partow wrote:
> Your example works fine on all the compilers I tested - including
> MSVC2015 U3 which was the only compiler giving the erroneous result.

can you narrow it down, then, to see what minimal change introduces the
problem? I suspect it is related to a specialization when the output
iterator is `char*`, perhaps


------------------------------------------------------------------------------
Announcing the Oxford Dictionaries API! The API offers world-renowned
dictionary content that is easy and intuitive to access. Sign up for an
account today to start using our lexical data to power your apps and
projects. Get started today and enter our developer competition.
http://sdm.link/oxford
_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general