std.json segmentation fault

Maxim Fomin maxim at maxim-fomin.ru
Wed Feb 6 03:23:52 PST 2013


On Wednesday, 6 February 2013 at 09:41:28 UTC, Stephan wrote:
> Here is a full test case. I don't want to submit it to Bugzilla 
> yet, since I don't know whether it's intended behaviour or a 
> bug. I have a slight feeling that this is NOT a bug. Somehow, 
> in the first case a reference to an invalid struct is returned 
> by json["hello"] and attempted to be overridden by a new 
> JSONValue. In the second case, no reference is generated, 
> instead a simple struct copy (a blit) is invoked, which works.

This depends on whether such code is supposed to be supported or 
not. Currently it is not supported and should no be used. But you 
can try write enhancement pull.

> Can someone explain this more correctly?
>
> This program crashes (tested on dmd 2.061)
> import std.json;
> void main() {
>   JSONValue json;
>   json.type = JSON_TYPE.OBJECT;
>   json["hello"] = JSONValue();
> }

opIndex returns by ref an element of associative array as an 
rvalue. This means that if absent, a new element is not added. In 
your example array is empty. Json library returns null (because 
it returns by ref) and the first instruction which attempts to 
copy object from returned pointer to stack segfaults.

> This program works fine:
> import std.json;
> void main() {
>   JSONValue json;
>   json.type = JSON_TYPE.OBJECT;
>   json.object["hello"] = JSONValue();
> }

In this case a new element of AA is created.


More information about the Digitalmars-d mailing list