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 module - SIGSEGV if key not exists #3107

Closed
scriptum opened this issue Jul 11, 2015 · 4 comments
Closed

Json module - SIGSEGV if key not exists #3107

scriptum opened this issue Jul 11, 2015 · 4 comments

Comments

@scriptum
Copy link
Contributor

This results in SIGSEGV:

import json
var data = parseJson("{\"a\":1,\"b\":2}")
echo data["a"]
echo data["c"]

Is that ok? Why not exception?

@Perelandric
Copy link
Contributor

Traceback (most recent call last)
test.nim(38)             test
json.nim(990)            $
json.nim(759)            len
SIGSEGV: Illegal storage access. (Attempt to read from nil?)

Seems that it's from the len proc missing a nil check. The default when not given a jArray or jObject is to return 0, so I'd think it would be the same for nil.

@dom96
Copy link
Contributor

dom96 commented Jul 12, 2015

I would rather be told that what I am passing to it is nil.

@scriptum
Copy link
Contributor Author

Another strange behavior:

import tables
var a = newTable[string,string]()
echo a["b"] #nil
var b = newTable[string,int]()
echo b["c"] #0
b["c"] = 0
echo b["c"] #0 - same result: is key exist with value 0 or it doesn't exist?

Python throws exception in both cases.

@Perelandric
Copy link
Contributor

I guess some len() procs handle nil and others don't. Weird.

@scriptum According to the docs, you get the default value for the type if no key exists, so this by design.

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

No branches or pull requests

4 participants