分段错误读取json字符串
我正在尝试阅读json字符串并打印,它正在生成段错误(核心转储)。我认为错误是输入字符串,但不是很确定。分段错误读取json字符串
这里是代码
CODE:
#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字符串是以下(不逃逸引号):
{"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指针,引起分段错误。
你的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;
}
}
}
您可以lint your 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应该会导致段错误... –
是的。 segfault是因为无效的json。但代码无法在json_parse()中打印值。我的意思是json_type_int。请检查我的代码。 – sandesh
为什么右括号之前的引号? –
分配给'json_object * jobj'的内存在哪里? json lib是做这个还是你负责?看起来你是,但我之前没有使用过这个库,所以仔细检查。 '地址0xfffffff5不堆栈,malloc'd或(最近)free'd' ?? –