Segfault when parsing json string

605 views
Skip to first unread message

abedra

unread,
Jan 4, 2012, 7:59:41 PM1/4/12
to Jansson users
It's possible I am being stupid here, but I am getting a segfault
trying to parse a json string. The following code:

char *string = zrecv(receiver);
fflush(stdout);
printf("Got %s\n", string);

root = json_loadb(string, sizeof(string), 0, error);

segfaults during the json_loadb call

(gdb) run
Starting program: /home/abedra/crazy/client
[Thread debugging using libthread_db enabled]
[New Thread 0xb7c8db70 (LWP 19236)]
[New Thread 0xb748db70 (LWP 19237)]
Got { "id": "crazy", "message": "log message" }

Program received signal SIGSEGV, Segmentation fault.
jsonp_error_init (error=0x8048b6b, source=0xb7f949a3 "<buffer>") at
error.c:8
8 error->text[0] = '\0';
(gdb) bt
#0 jsonp_error_init (error=0x8048b6b, source=0xb7f949a3 "<buffer>")
at error.c:8
#1 0xb7f91591 in json_loadb (buffer=0x804c2c0 "{ \"id\": \"crazy\",
\"message\": \"log message\" }", buflen=4, flags=0, error=0x8048b6b)
at load.c:915
#2 0x08048ac0 in main ()

I have tried this with json_loads as well and get the same result. The
json string is coming in from a zeromq socket.

Any ideas?

Aaron

Aaron Bedra

unread,
Jan 4, 2012, 8:16:12 PM1/4/12
to jansso...@googlegroups.com
Just realized I had a typo in the code. I actually used strlen, not sizeof. I still do get the segfault though.

Aaron

> --
> Jansson users mailing list
> jansso...@googlegroups.com
> http://groups.google.com/group/jansson-users

Deron Meranda

unread,
Jan 5, 2012, 12:48:26 AM1/5/12
to jansso...@googlegroups.com
On Wed, Jan 4, 2012 at 7:59 PM, abedra <aaron...@gmail.com> wrote:
> It's possible I am being stupid here, but I am getting a segfault
> trying to parse a json string. The following code:
>
> char *string = zrecv(receiver);
> fflush(stdout);
> printf("Got %s\n", string);
>
> root = json_loadb(string, strlen(string), 0, error); //corrected typo


As you're using zero-byte-terminated strings, it may be easier to call
json_loads() rather than doing both a strlen() and then json_loadb().


> Program received signal SIGSEGV, Segmentation fault.
> jsonp_error_init (error=0x8048b6b, source=0xb7f949a3 "<buffer>") at

I suspect that you're 4th argument, error, is not of the correct type.

Typically you'll allocate an error structure in local (stack) storage
and then pass a pointer to it, as:

json_error_t error;
...
json_loadb( string, strlen(string), 0, &error );


I don't see an & operator in your code. So how are you declaring the variable?
--
Deron Meranda
http://deron.meranda.us/

Reply all
Reply to author
Forward
0 new messages