Occasional SIGSEGV while trying to parse JSON in an embedded V8 Application.

50 views
Skip to first unread message

Abhishek Kona

unread,
May 14, 2014, 7:36:27 PM5/14/14
to v8-u...@googlegroups.com
I have some v8 Code which tries to Parse JSON I get a SIGSEGV once in a while when I try to access the value. I am very confused about what is happening. 
Any help would be greatly appreciated.

The code to Parse JSON is

v8::Handle<v8::Value> FromJSONString(
    v8::Handle<v8::Value> json_string) {
  v8::HandleScope scope;
  v8::Handle<v8::Context> context = v8::Context::GetCurrent();
  v8::Handle<v8::Object> global = context->Global();

  v8::Handle<v8::Value> JSON_value = global->Get(v8::String::New("JSON"));
  if (!IsObject(JSON_value)) {
    return scope.Close(v8::Undefined());
  }
  v8::Handle<v8::Object> JSON  = JSON_value->ToObject();

  v8::Handle<v8::Value> JSON_parse_value = JSON->Get(v8::String::New("parse"));

  if (JSON_parse_value.IsEmpty() || JSON_parse_value->IsNull() ||
      JSON_parse_value->IsUndefined() ||!JSON_parse_value->IsFunction()) {
    return scope.Close(v8::Undefined());
  }


  v8::Handle<v8::Function> JSON_parse =
      v8::Handle<v8::Function>::Cast(JSON_parse_value);

  return scope.Close(JSON_parse->Call(JSON, 1, &json_string));
}

And the code which calls this is at => For some reason accessing Value results in a SIGSEGV.

bool extractSource(std::string* source, std::string& body) {
    v8::HandleScope scope; // this is needed and clears the memory
    if (body.empty()) {
        return false;
    }
    v8::Handle<v8::Value> value = v8_server_utils::FromJSONString(body);
    if (value->IsEmpty()) { // CRASHES HERE.
        return false;
    }
    if (value->IsNull()) {
        return false;
    }
    if (value->IsUndefined()) {
        return false;
    }
    if (!value->IsObject()) {
        return false;
    }
    auto object = value->ToObject();
    auto source_key = v8::String::New("source");
    if (object.IsEmpty() || object->IsNull() || object->IsUndefined() ||
        !object->Has(source_key)) {
        return false;
    }
    auto source_obj = object->Get(source_key);
    *source = v8_server_utils::JSStringToCString(source_obj->ToString());
    return true;
}



Jakob Kummerow

unread,
May 15, 2014, 11:17:09 AM5/15/14
to v8-u...@googlegroups.com
There's a subtle but important difference between "value->IsEmpty()" and "value.IsEmpty()" ;-)


--
--
v8-users mailing list
v8-u...@googlegroups.com
http://groups.google.com/group/v8-users
---
You received this message because you are subscribed to the Google Groups "v8-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to v8-users+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Abhishek K

unread,
May 16, 2014, 2:50:43 AM5/16/14
to v8-u...@googlegroups.com
Thank you :)

I did figure that out. 

Sometimes the JSON parsing function returns an empty value even if the JSON is valid, I cannot reproduce the bug with the same JSON object, twice, I am not sure what is happening.

Any light on that would be appreciated. 

-Abhishel

-Abhishek Kona



You received this message because you are subscribed to a topic in the Google Groups "v8-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/v8-users/DEDJOGTWgc0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to v8-users+u...@googlegroups.com.

Ben Noordhuis

unread,
May 18, 2014, 1:58:55 PM5/18/14
to v8-u...@googlegroups.com
On Fri, May 16, 2014 at 8:50 AM, Abhishek K <abhish...@gmail.com> wrote:
> Sometimes the JSON parsing function returns an empty value even if the JSON
> is valid, I cannot reproduce the bug with the same JSON object, twice, I am
> not sure what is happening.

It sounds like V8 may be running out of memory. When that happens, it
returns an empty handle.
Reply all
Reply to author
Forward
0 new messages