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

Null bytes in files are treated like EOF #1095

Closed
nlohmann opened this issue May 17, 2018 · 7 comments
Closed

Null bytes in files are treated like EOF #1095

nlohmann opened this issue May 17, 2018 · 7 comments
Labels
state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated

Comments

@nlohmann
Copy link
Owner

  • What is the issue you have?

When reading parsing JSON from a file, null bytes are treated as EOF. The reason for this is explicit code in the JSON lexer to treat \0 as EOF to properly parse string literals.

  • Please describe the steps to reproduce the issue. Can you provide a small but working code example?

Parse file https://github.com/nst/JSONTestSuite/blob/master/test_parsing/n_multidigit_number_then_00.json, e.g. by

std::ifstream f("n_multidigit_number_then_00.json");
json::parse(f);
  • What is the expected behavior?

Parse error: unexpected null byte after reading 123.

  • And what is the actual behavior instead?

The file is successfully parsed as number 123.

Apple LLVM version 9.1.0.

  • Did you use a released version of the library or the version from the develop branch?

develop.

N/A

@stale
Copy link

stale bot commented Jun 16, 2018

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated label Jun 16, 2018
@stale stale bot closed this as completed Jun 23, 2018
@nlohmann nlohmann reopened this Jan 13, 2019
@stale stale bot removed the state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated label Jan 13, 2019
@FrancoisChabot
Copy link
Contributor

FrancoisChabot commented Jan 22, 2019

I have a working fix for this, however, it causes a knock-on that I believe is desirable.

The following test(and a few other almost identical ones) now fail:

    SECTION("from std::array")
    {
        std::array<uint8_t, 5> v { {'t', 'r', 'u', 'e'} };
        json j;
        json::parser(nlohmann::detail::input_adapter(std::begin(v), std::end(v))).parse(true, j);
        CHECK(j == json(true));
    }

This makes sense as the explicit 0 at the end of the array is now considered to be part of the parsed string.

I would just change the test to be a std::array<uint8_t, 4> instead.

Is that fine?

Edit: I've attached my work-in-progress as a PR for reference.

@nlohmann
Copy link
Owner Author

Thanks for the PR! I added some comments, and I need to think whether there is another use case that needs to be tested.

@stale
Copy link

stale bot commented Feb 21, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated label Feb 21, 2019
@nlohmann nlohmann removed the state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated label Feb 22, 2019
@stale
Copy link

stale bot commented Mar 24, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated label Mar 24, 2019
@nlohmann
Copy link
Owner Author

@FrancoisChabot It would be great if you could have another look at #1452.

@stale stale bot removed the state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated label Mar 26, 2019
@stale
Copy link

stale bot commented Apr 25, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated label Apr 25, 2019
@stale stale bot closed this as completed May 2, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants