Search code examples
cjsonglib

Json-Glib Invalid jumps on object insertions


I'am working with a C language Json Library, the Json-Glib. The problem is that the library keeps Segfaulting on me and i can't figure out why. I read all the documentation, and understood that, a Root node can hold some primitive values, but more importantly an Object. I'am trying to achieve an object Concatenation, but when i try to put an object inside a Node or even just add string members in it i get Invalid jumps as invalid read of usualy size 8(string). Here is the code trying to achieve a JsonObject retrieval from a JsonNode as little string insertion in the retrieved JsonObject.

int main(){

    const gchar *auth = "authentication";

    //here i initialize my Root JsonNode with an object in it and a JsonObject to be able to get the object from the JsonNode                           
    JsonObject* authObject = json_object_new();
    JsonNode* extNode = json_node_new(JSON_NODE_OBJECT);

    // Here i retrieve the initialized JsonObject that is inside my JsonNode                                                                            
    authObject = json_node_get_object(extNode);

    // And here some few insertion of strings in the object                                                                                             
    SegFault Here -> json_object_set_string_member(authObject, "action", "authenticate");
    json_object_set_string_member(authObject, "type", "authType");
    json_object_set_string_member(authObject, "resource", "resource");
    json_object_set_string_member(authObject, "version", "none");
    json_object_set_string_member(authObject, "data", "loginData");

    //here i try to print my json file but i can't even reach this execution line due to SegFault                                                       
    char* toto = cometd_json_node2str(extNode);
    puts(toto);

    return 0;
}

And here is the Valgrind Report :

==4910== Memcheck, a memory error detector
==4910== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==4910== Using Valgrind-3.11.0.SVN and LibVEX; rerun with -h for copyright info
==4910== Command: ./a.out
==4910== 
==4910== Invalid read of size 8
==4910==    at 0x10013C28D: json_object_set_string_member (in /usr/local/Cellar/json-glib/1.0.2/lib/libjson-glib-1.0.0.dylib)
==4910==    by 0x100000DD1: main (main.c:50)
==4910==  Address 0x0 is not stack'd, malloc'd or (recently) free'd
==4910== 
==4910== 
==4910== Process terminating with default action of signal 11 (SIGSEGV)
==4910==  Access not within mapped region at address 0x0
==4910==    at 0x10013C28D: json_object_set_string_member (in /usr/local/Cellar/json-glib/1.0.2/lib/libjson-glib-1.0.0.dylib)
==4910==    by 0x100000DD1: main (main.c:50)
==4910==  If you believe this happened as a result of a stack
==4910==  overflow in your program's main thread (unlikely but
==4910==  possible), you can try to increase the size of the
==4910==  main thread stack using the --main-stacksize= flag.
==4910==  The main thread stack size used in this run was 8388608.
==4910== 
==4910== HEAP SUMMARY:
==4910==     in use at exit: 836,054 bytes in 2,098 blocks
==4910==   total heap usage: 2,577 allocs, 479 frees, 1,410,814 bytes allocated
==4910== 
==4910== LEAK SUMMARY:
==4910==    definitely lost: 3,167 bytes in 45 blocks
==4910==    indirectly lost: 5,357 bytes in 22 blocks
==4910==      possibly lost: 20,836 bytes in 214 blocks
==4910==    still reachable: 128,596 bytes in 853 blocks
==4910==         suppressed: 678,098 bytes in 964 blocks
==4910== Rerun with --leak-check=full to see details of leaked memory
==4910== 
==4910== For counts of detected and suppressed errors, rerun with: -v
==4910== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 1 from 1)

Any Help would be much much appreciated, in fact if i can debug this i will wrote a tutorial about the Json-Glib to help other programmers not struggling with it.


Solution

  • You're overwriting the authObject pointer:

    JsonObject* authObject = json_object_new(); // ← You create it here…
    JsonNode* extNode = json_node_new(JSON_NODE_OBJECT);
    
    authObject = json_node_get_object(extNode); // ← … but overwrite it here
    

    The call to json_node_get_object() on a newly created JsonNode will return NULL, since you haven't set the object.

    If you want to set a JsonNode with an object, use json_node_init_object() instead:

    JsonObject *authObject = json_object_new ();
    JsonNode *node = json_node_init_object (json_node_alloc (), authObject);
    

    Or use json_node_new() and json_node_take_object():

    JsonObject *authObject = json_object_new ();
    JsonNode *node = json_node_new (JSON_NODE_OBJECT);
    json_node_take_object (node, authObject);
    

    Now you can add members to authObject.