0

JSON文字列を読み取って印刷しようとしていますが、セグメンテーション違反(コアダンプ)が発生しています。エラーは入力文字列が原因だと思いますが、よくわかりません。

ここにコードがあります

コード:

 #include <json/json.h>
 #include <stdio.h>

 void json_parse(json_object * jobj);

int main(){

char * string = "{"
              "\"coooooool\": { "
                                    "\"name\" : \"coooooooooool\","
                                    "\"name\" : 1"
                                    "\"}"
               "\"}";

printf ("JSON string: %sn", string);
json_object * jobj = json_tokener_parse(string);
json_parse(jobj);


return 0;
}

void json_parse(json_object * jobj) {
 enum json_type type;
 json_object_object_foreach(jobj, key, val) {
     type = json_object_get_type(val);
     switch (type)
     {
         case json_type_int: printf("type: json_type_int, ");
         printf("value: %dn", json_object_get_int(val));
         break;
     }
  }
}

出力されたバイナリ ファイルを valgrind で実行して、エラーを適切に確認しました

valgrind で実行中にこのエラーが発生します

==14573== Memcheck, a memory error detector
==14573== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==14573== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==14573== Command: ./a.out
==14573== 
==14573== Invalid read of size 4
==14573==    at 0x40491F8: json_object_get_object (in /usr/lib/i386-linux-gnu/libjson.so.0.0.1)
==14573==    by 0x80485E5: main (in /var/www/json/a.out)
==14573==  Address 0xfffffff5 is not stack'd, malloc'd or (recently) free'd
==14573== 
==14573== 
==14573== Process terminating with default action of signal 11 (SIGSEGV)
==14573==  Access not within mapped region at address 0xFFFFFFF5
==14573==    at 0x40491F8: json_object_get_object (in /usr/lib/i386-linux-gnu/libjson.so.0.0.1)
==14573==    by 0x80485E5: main (in /var/www/json/a.out)
==14573==  If you believe this happened as a result of a stack
==14573==  overflow in your program's main thread (unlikely but
==14573==  possible), you can try to increase the size of the
==14573==  main thread stack using the --main-stacksize= flag.
==14573==  The main thread stack size used in this run was 8388608.
JSON string: {"coooooool": { "name" : "coooooooooool","name" : 1"}"}n==14573== 
==14573== HEAP SUMMARY:
==14573==     in use at exit: 0 bytes in 0 blocks
==14573==   total heap usage: 17 allocs, 17 frees, 1,511 bytes allocated
==14573== 
==14573== All heap blocks were freed -- no leaks are possible
==14573== 
==14573== For counts of detected and suppressed errors, rerun with: -v
==14573== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
Segmentation fault (core dumped)
4

2 に答える 2