Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[clang-3.6.2] string/sstream with number to json issue #107

Closed
vpetrigo opened this issue Jul 29, 2015 · 17 comments
Closed

[clang-3.6.2] string/sstream with number to json issue #107

vpetrigo opened this issue Jul 29, 2015 · 17 comments

Comments

@vpetrigo
Copy link
Contributor

Dear Niels,

I have a library for working with some translate API. By using curl I form request to the server and obtain a string in the format {"code":200,"lang":"ru-en","text":[hello]}. After a while I met the issue that my library which had been build with clang dropped with Segmentation fault error.
Below my test code which works well with g++ (4.9.2 and 5.2.0), but not with clang++ (3.6.2):

#include <iostream>
#include <sstream>
#include "json.hpp"

using json = nlohmann::json;

int main() {
    std::istringstream is {R"({"code":200,"lang":"ru-en","text":["hello"]})"};
    json d;

    std::cout << is.str() << std::endl;

    is >> d;

    std::cout << d.dump() << std::endl;

    return 0;
}

And also tested such case:

    std::string s{"{ \"code\" : 200 }"};
    json d;

    d = json::parse(s);

My current clang is version 3.6.2 (tags/RELEASE_362/final). I will test it with other version and will tell you if something changes.

@nlohmann nlohmann self-assigned this Jul 29, 2015
@nlohmann
Copy link
Owner

Hi Vladimir,

unfortunately, I cannot reproduce this. I tried with Clang 3.6.2 from Homebrew (package llvm36) and could compile and run your code successfully. Which OS are you using?

Cheers,
Niels

@vpetrigo
Copy link
Contributor Author

Niels,

That is a little bit strange. I logged in Linux and this bug with did not take place there.
OS where I was able to do this bug was Windows with MSYS2 package.
And this bug takes place only when I try to serialize json data via dump() method.

@vpetrigo
Copy link
Contributor Author

I made some drill and found that clang build in MSYS2 failed when it tried to call strtold() function.
gdb
It is quite strange because gcc and clang use the same libraries.

@maksis
Copy link

maksis commented Aug 4, 2015

I'm having issues with parsing numbers as well, but with MSVC 2015.

Seems like strtold considers the trailing comma to be part of the number, causing endptr to go past m_cursor, so the function get_number() returns NAN.

@nlohmann
Copy link
Owner

nlohmann commented Aug 4, 2015

Can you provide me with a concrete example?

@maksis
Copy link

maksis commented Aug 4, 2015

Not yet, because parsing the same string seems to work ok in a clean project...

@maksis
Copy link

maksis commented Aug 4, 2015

This seems to depend on the current system locale. If I change the number format from Finnish to US and run the program, the comma won't be included in the number. Parsing works correctly in a clean Win32 console project regardless of the locale though.

nlohmann added a commit that referenced this issue Aug 4, 2015
@nlohmann
Copy link
Owner

nlohmann commented Aug 4, 2015

I made a change in the string-to-float conversion to make it locale-independent. Could you please check if the issue still exists, @maksis?

@maksis
Copy link

maksis commented Aug 4, 2015

It seems to work correctly now

@vpetrigo
Copy link
Contributor Author

vpetrigo commented Aug 4, 2015

@nlohmann thank you! Now it works fine.

@nlohmann
Copy link
Owner

nlohmann commented Aug 4, 2015

However, the build on ApoVeyor fails. :-(

@vpetrigo
Copy link
Contributor Author

vpetrigo commented Aug 4, 2015

As I can see it is because of the quite big precision of returned value. Is it the case to use something like this:

long double foo(long double v) {
    std::stringstream ss;

    ss << v;

    ss.precision(6);

    ss >> v;

    return v;
}

int main() {
    long double a = 10.1238127638712638;

    std::cout << foo(a) << std::endl;

    return 0;
}

@nlohmann
Copy link
Owner

nlohmann commented Aug 6, 2015

Unfortunately, this does not help. The issue is the following

C:\projects\json\test\unit.cpp(9340): FAILED:
  CHECK( json::parse(json_string)[0].get<double>() == Approx(expected) )
due to unexpected exception with message:
  json_string := [4.9406564584124654e-324]
  expected := 4.94066e-324
  parse error - 4.9406564584124654e-324 is not a number

===============================================================================

The Windows build fails to parse the number 4.9406564584124654e-324. See https://ci.appveyor.com/project/nlohmann/json/build/554.

nlohmann added a commit that referenced this issue Aug 6, 2015
@nlohmann
Copy link
Owner

nlohmann commented Aug 6, 2015

I reverted the fix. It has some unpredictable behavior, and compatibility to MSVC would break.

@nlohmann
Copy link
Owner

nlohmann commented Aug 6, 2015

:-(

@vpetrigo
Copy link
Contributor Author

vpetrigo commented Nov 6, 2015

Hello Niels. That issue could be closed, because Segmentation fault in MinGW environment with clang compiler caused by that issue https://github.com/Alexpux/MINGW-packages/issues/722. Now it is fixed and all works fine.

@vpetrigo vpetrigo closed this as completed Nov 6, 2015
@nlohmann
Copy link
Owner

nlohmann commented Nov 6, 2015

Thanks for getting back and telling me! :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants