www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - std.json segmentation fault

reply "Andrea Fontana" <nospam example.com> writes:
This code was working some months ago:

import std.json;
JSONValue total;
total.type = JSON_TYPE.OBJECT;
total["results"].type = JSON_TYPE.OBJECT;


Now, last line gives me a segmentation fault. What's wrong with 
this code?
Feb 04 2013
next sibling parent "Vladimir Panteleev" <vladimir thecybershadow.net> writes:
On Monday, 4 February 2013 at 10:04:10 UTC, Andrea Fontana wrote:
 This code was working some months ago:

 import std.json;
 JSONValue total;
 total.type = JSON_TYPE.OBJECT;
 total["results"].type = JSON_TYPE.OBJECT;


 Now, last line gives me a segmentation fault. What's wrong with 
 this code?

https://github.com/D-Programming-Language/phobos/pull/561/files Is this code right? Has it ever worked? I don't think returning a ref value of an AA works for adding items to an AA. That pull request didn't come with any unit tests... shouldn't have made it through review, IMO.
Feb 04 2013
prev sibling next sibling parent Walter Bright <newshound2 digitalmars.com> writes:
On 2/4/2013 2:04 AM, Andrea Fontana wrote:
 This code was working some months ago:

 import std.json;
 JSONValue total;
 total.type = JSON_TYPE.OBJECT;
 total["results"].type = JSON_TYPE.OBJECT;


 Now, last line gives me a segmentation fault. What's wrong with this code?

1. That code is not valid D source - it's presumably missing the function it is embedded inside. Please post valid D source when a bug is suspected, otherwise people will have to guess at what the rest is, and often the rest is where the real issue was. 2. Regressions (and other bugs) should be posted to bugzilla, along with a reproducible test case.
Feb 04 2013
prev sibling next sibling parent "Maxim Fomin" <maxim maxim-fomin.ru> writes:
On Monday, 4 February 2013 at 21:16:51 UTC, Vladimir Panteleev 
wrote:
 On Monday, 4 February 2013 at 10:04:10 UTC, Andrea Fontana 
 wrote:
 This code was working some months ago:

 import std.json;
 JSONValue total;
 total.type = JSON_TYPE.OBJECT;
 total["results"].type = JSON_TYPE.OBJECT;


 Now, last line gives me a segmentation fault. What's wrong 
 with this code?

https://github.com/D-Programming-Language/phobos/pull/561/files Is this code right? Has it ever worked?

I think it depends on how std.json is defined. It is up to implementator to define whether such things should be supported or not.
 I don't think returning a ref value of an AA works for adding 
 items to an AA.

He doesn't add item to AA. The code just queries a reference to item from array and since opIndex returns null, any attempt to construct a stack variable from null reference leads to segfault. By no means the code can manipulate AA using returned struct since it is copied, even if opIndex returns by reference.
 That pull request didn't come with any unit tests... shouldn't 
 have made it through review, IMO.

Feb 04 2013
prev sibling next sibling parent "Stephan" <stephan_schiffels mac.com> writes:
On Monday, 4 February 2013 at 10:04:10 UTC, Andrea Fontana wrote:
 This code was working some months ago:

 import std.json;
 JSONValue total;
 total.type = JSON_TYPE.OBJECT;
 total["results"].type = JSON_TYPE.OBJECT;


 Now, last line gives me a segmentation fault. What's wrong with 
 this code?

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. 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(); } This program works fine: import std.json; void main() { JSONValue json; json.type = JSON_TYPE.OBJECT; json.object["hello"] = JSONValue(); }
Feb 06 2013
prev sibling next sibling parent "Maxim Fomin" <maxim maxim-fomin.ru> writes:
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.
Feb 06 2013
prev sibling next sibling parent "Andrea Fontana" <nospam example.com> writes:
Yes it works in that way. I had to change it in a lot of source 
files.
It worked fine for months as online webservice used by thousands 
users and never crashed :)

With last changes to code (a db connection preference - not 
relevant to std.json) it starts failing every time.



On Wednesday, 6 February 2013 at 09:41:28 UTC, Stephan wrote:
 On Monday, 4 February 2013 at 10:04:10 UTC, Andrea Fontana 
 wrote:
 This code was working some months ago:

 import std.json;
 JSONValue total;
 total.type = JSON_TYPE.OBJECT;
 total["results"].type = JSON_TYPE.OBJECT;


 Now, last line gives me a segmentation fault. What's wrong 
 with this code?

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. 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(); } This program works fine: import std.json; void main() { JSONValue json; json.type = JSON_TYPE.OBJECT; json.object["hello"] = JSONValue(); }

Feb 06 2013
prev sibling parent "Maxim Fomin" <maxim maxim-fomin.ru> writes:
On Wednesday, 6 February 2013 at 12:09:47 UTC, Andrea Fontana 
wrote:
 Yes it works in that way. I had to change it in a lot of source 
 files.
 It worked fine for months as online webservice used by 
 thousands users and never crashed :)

 With last changes to code (a db connection preference - not 
 relevant to std.json) it starts failing every time.

If you are interested, you can apply phobos pull request which fixes json's opIndex.
Feb 06 2013