wangxiaowei0303 / rapidjson

Automatically exported from code.google.com/p/rapidjson
MIT License
0 stars 0 forks source link

SetStringRaw doesn't work with wchar_t #44

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. use UTF16 document
2. use Parse, not ParseInsitu
3. == string corruption on simple JSON sample

    std::wstring test2(L"[{\"created_at\":\"Wed Oct 30 17:13:20 +0000 2012\"}]");

    GenericDocument< UTF16LE<> > json;
    json.Parse<kParseValidateEncodingFlag>(test2.c_str());

This is due to a bug in SetStringRaw() - it does not take account of the size 
of multi-byte strings when allocating and copying, thus when used with wchar_t, 
the string data gets chopped in half.

The following two changes seem to fix things:

void SetStringRaw(const Ch* s, SizeType length, Allocator& allocator) {
    RAPIDJSON_ASSERT(s != NULL);
    flags_ = kCopyStringFlag;
>>  data_.s.str = (Ch *)allocator.Malloc((length + 1) * sizeof(Ch));
    data_.s.length = length;
>>  memcpy((void*)data_.s.str, s, (length * sizeof(Ch)));
    ((Ch*)data_.s.str)[length] = '\0';
}

Original issue reported on code.google.com by harry.de...@gmail.com on 7 Nov 2012 at 11:40

GoogleCodeExporter commented 9 years ago

Original comment by milo...@gmail.com on 12 Nov 2012 at 3:46

GoogleCodeExporter commented 9 years ago
This issue was closed by revision r65.

Original comment by milo...@gmail.com on 13 Nov 2012 at 2:59

GoogleCodeExporter commented 9 years ago
This issue was closed by revision r66.

Original comment by milo...@gmail.com on 13 Nov 2012 at 2:59

GoogleCodeExporter commented 9 years ago
This issue was closed by revision 821c6ab73cf3.

Original comment by milo...@gmail.com on 6 Jun 2014 at 6:04