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

json: json_obj_parse will modify the input string #21967

Closed
codingspirit opened this issue Jan 16, 2020 · 4 comments
Closed

json: json_obj_parse will modify the input string #21967

codingspirit opened this issue Jan 16, 2020 · 4 comments

Comments

@codingspirit
Copy link
Contributor

codingspirit commented Jan 16, 2020

Describe the bug
The end " of string items will be changed to \0 after json_obj_parse, i.e. we have a char array like this:

{
    "method": "hello",
    "id": 1
}

if using json_obj_parse to proceed it, then it will be changed to

{
    "method": "hello�, //here " symbol changed to "\0"
    "id": 1
}

To Reproduce
Steps to reproduce the behavior:

  1. create a json char array with string items
  2. decode it with json_obj_parse
  3. print out the json char array
  4. see modifications

Expected behavior
From my point of view, json_obj_parse should be

int json_obj_parse(const char *payload, size_t len,
		   const struct json_obj_descr *descr, size_t descr_len,
		   void *val)

which means the input json string should not be modified.

Impact
Sometimes we'd like to proceed a json string twice:

  1. the first time only proceed the first filed to know the info of this json
  2. according the info we parse again with specific json_obj_descr
    If json_obj_parse will modify string, user have to create a copy of string .

Environment (please complete the following information):

  • OS: Linux
  • Toolchain: Zephyr SDK
  • Commit SHA or Version used: f05cbb4
@codingspirit codingspirit added the bug The issue is a bug, or the PR is fixing a bug label Jan 16, 2020
@jhedberg jhedberg added the priority: low Low impact/importance bug label Jan 21, 2020
@mbolivar
Copy link
Contributor

Is this really a bug? I think this is the library working as designed. Otherwise it would have to duplicate the string.

@dcpleung dcpleung removed their assignment Feb 12, 2020
@dcpleung
Copy link
Member

(Not sure why I was assigned)

However, I agree with @mbolivar that this is not exactly a bug. Parsing JSON and returning an hierarchy of JSON objects is memory intensive (not to mention the manual memory allocation handling). Since Zephyr is targeting embedded devices with limited memory, parsing in-place with modifications is an acceptable compromise.

If you need to parse the string twice, could you try duplicating the string first to extract information for your first step? You can release the memory for the duplicated string, and then proceed to parse the original string.

@mbolivar
Copy link
Contributor

@jhedberg I think the labels need a -bug +question and this issue can be closed.

@jhedberg jhedberg added question and removed bug The issue is a bug, or the PR is fixing a bug labels Feb 12, 2020
@jhedberg
Copy link
Member

(Not sure why I was assigned)

@dcpleung unfortunately I can't remember, but seems like a mistake - sorry about that.

@jhedberg I think the labels need a -bug +question and this issue can be closed.

@mbolivar agreed.

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

No branches or pull requests

4 participants