繁体   English   中英

分段错误读取json字符串

[英]segmentation fault reading json string

我正在尝试读取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)

您的json无效。 尝试使用格式正确的json字符串,它可以正常工作:

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

void json_parse(json_object * jobj);

int main(){

    char * string2 = "{"
            "\"coooooool\": { "
            "\"name\" : \"coooooooooool\","
            "\"name\" : 1"
            "\"}"
            "\"}";
    char * string = "{\"name\" : \"joys of programming\"}";
    printf ("JSON string: %sn", string);
   // json_object * jobj = malloc(sizeof(json_object));
    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;
        }
    }
}

您可以对json进行整理,以检查其需要方式。

输出量

./test1 
JSON string: {"name" : "joys of programming"}

我这样编译

gcc -g -v -Wall -std=gnu99 -static -L/path/to/json-c-0.9/lib main.c -o test1 -ljson

您的问题很简单:您的json字符串中有错误。
您的json字符串如下(不带引号):

{"coooooool": { "name" : "coooooooooool","name" : 1"}"}

两个键具有相同的名称,并且字符串的最后两个双引号字符完全不合适。
该字符串不是有效的json,因此json_tokener_parse返回NULL。
您应该执行错误检查以捕获解析字符串的失败,即添加如下检查:

if(jobj == NULL) {
    // recover from the error or quit the program
}

使用您的代码,json_object_object_foreach接收到NULL指针,从而导致分段错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM