Tencent / rapidjson

A fast JSON parser/generator for C++ with both SAX/DOM style API
http://rapidjson.org/
Other
14.16k stars 3.52k forks source link

Insitu parsing #1790

Open alenloncaric opened 3 years ago

alenloncaric commented 3 years ago

Dear,

I saw somewhere that for insitu we need to reserve few bytes at the end ? How much ? Can I do insitu parsing over std::string ? being resized by those few bytes?

Something like: response.resize(response.length() + 16); char* cstr = &response[0]; d.ParseInsitu<0>(cstr);

With kind regards,

miloyip commented 3 years ago

Some tools detect out-of-bound read when SSE optimization is used. This is actually considered safe and would not access out of page boundary. But if you really want to remove this out-of-bound issue, you can append 16 bytes (width of SSE load).

miloyip commented 3 years ago

You can do it with std::string.

LazyRen commented 1 year ago

~~IMHO current behavior of insitu is too dangerous. It simply writes into the given buffer without checking char array boundary. This may results in undefined behavior such as crash or memory corruption.~~

miloyip commented 1 year ago

IMHO current behavior of insitu is too dangerous. It simply writes into the given buffer without checking char array boundary. This may results in undefined behavior such as crash or memory corruption.

That is not related to this issue related to SIMD. Insitu parsing is designed to be safe, with the decoded string always shorter than the source string. It will not write across boundary to the in/out zero-terminated string.

LazyRen commented 1 year ago

Sorry about my previous comment, I have confirmed that insitu parsing does provide decoded string does not exceeds original length of the string.

AFAICT, it switches ending " to \0 if given key or value is not \0 terminated.