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

[deserialize] Unicode escape "\u0000" is silently ignored #1646

Closed
bupjae opened this issue Aug 31, 2021 · 4 comments
Closed

[deserialize] Unicode escape "\u0000" is silently ignored #1646

bupjae opened this issue Aug 31, 2021 · 4 comments
Labels
bug v6 ArduinoJson 6

Comments

@bupjae
Copy link

bupjae commented Aug 31, 2021

ArduinoJson version: v6.18.3
Target platform: x86-64
Toolchain: gcc (x86_64-posix-seh-rev0, Built by MinGW-W64 project) 8.1.0

Code snippet:

#include <iostream>
#include <string>

#include "ArduinoJson-v6.18.3.hpp"

using namespace std;
using namespace ArduinoJson;

int main() {
  StaticJsonDocument<1024> doc;
  cout << deserializeJson(doc, R"({"data": "wx\u0000yz"})") << endl;
  string value = doc["data"];
  cout << value << endl;
  cout << value.length() << endl;
}

Excepted result (NUL character is replaced to '_' for visibility):

Ok
wx_yz
5

Actual result:

Ok
wxyz
4

I understand that NUL character inside string is very hard to handle for C/C++ code. However, I think this behavior should be documented at least.

@bblanchon
Copy link
Owner

Hi @bupjae,

You're right; there was something wrong: instead of including the null in the string, it dropped it entirely.
I fixed this problem in the main branch.

Now, if you extract a const char*, you'll see that it includes all characters; however, it cannot return an std::string with the correct size because it stores strings as pointers (and naturally expects them to be null-terminated).
I could fix this, but I'm afraid it would increase the size of the library for nothing.
So, unless you have a solid use case, I think this change can wait v7.

I agree to add a word about this in the documentation, but the question is: where?
What do you think, @bupjae? Where would you expect to find such information?

Best regards,
Benoit

@bblanchon bblanchon added the bug label Aug 31, 2021
@bupjae
Copy link
Author

bupjae commented Sep 1, 2021

It seems that unless JsonDocument holds length for each involved strings, NUL character inside document can cause various problems across all places. For example, after applying patch, output of serializeJson(doc, cout) is {"data":"wx"}. If such strings go to key of JsonObject, this issue will become more complicated.

I agree that the priority is low as this is very artificial example and may not have solid use case. Until then, we should be documented this issue; for example, "Strings containing NUL character (ASCII code 0, or \u0000) is not (fully) supported".

Suggested places to document this issue:

@bblanchon
Copy link
Owner

I documented the issue in the four suggested locations.
Thank you very much for reporting this issue, @bupjae.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 2, 2021
bblanchon added a commit that referenced this issue Nov 12, 2021
@bblanchon
Copy link
Owner

The fix was release in ArduinoJson 6.19.0.

@bblanchon bblanchon added the v6 ArduinoJson 6 label Feb 6, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug v6 ArduinoJson 6
Projects
None yet
Development

No branches or pull requests

2 participants