Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

successed in simple test while failed in my project #264

Closed
janlely opened this issue Aug 31, 2016 · 2 comments
Closed

successed in simple test while failed in my project #264

janlely opened this issue Aug 31, 2016 · 2 comments

Comments

@janlely
Copy link

janlely commented Aug 31, 2016

the under code works fine in a simple test program, but failed in my project under Ubuntu 14.04,
however success in my mac pro

i debug into my project, it got a "segmentation fault" at line 9

this is the out put:
////
Process 27360 stopped

  • thread Undefined symbol: json_object_from_file #2: tid = 27411, 0x00007ffff6ea3c37 libc.so.6__GI_raise(sig=6) + 55 at raise.c:56, name = 'xxxxx', stop reason = signal SIGABRT frame #0: 0x00007ffff6ea3c37 libc.so.6__GI_raise(sig=6) + 55 at raise.c:56

////

here is my code:

  1. void put_int_value_to_jsonstring(char *dest, const char *key, int value)
  2. {
  3. if(dest == NULL){
    
  4.     return;
    
  5. }
    
  6. json_object *jobj = NULL;
    
  7. if(strlen(dest) == 0 || !is_json_string(dest)){
    
  8.     json_object *new_jobj = json_object_new_object();
    
  9.     json_object_object_add(new_jobj, key, json_object_new_int(value));
    
  10.     const char *jstr = json_object_to_json_string(new_jobj);
    
  11.     strcpy(dest, jstr);
    
  12.     json_object_put(new_jobj);
    
  13. }else{
    
  14.     jobj = json_tokener_parse(dest);
    
  15.     json_object *exist_value = json_object_object_get(jobj, key);
    
  16.     if(exist_value == NULL){
    
  17.         json_object_object_add(jobj, key, json_object_new_int(value));
    
  18.     }else{
    
  19.         json_object_object_del(jobj, key);
    
  20.         json_object_object_add(jobj, key, json_object_new_int(value));
    
  21.     }
    
  22.     const char *jstr = json_object_to_json_string_ext(jobj, JSON_C_TO_STRING_NOSLASHESCAPE);
    
  23.     strcpy(dest, jstr);
    
  24.     json_object_put(jobj);
    
  25. }
    
  26. }
@hawicz
Copy link
Member

hawicz commented Aug 31, 2016

Doing a blind strcpy like you have here is asking for trouble. My guess is that you're overrunning the size of what you have allocated for dest, but without seeing how you're calling this func who knows.
Even if thats not the problem this could benefit from an adjusted API, where you pass in the length of dest to check that you're within range and either return a failure or alloc a new buffer and return that.

You don't show a stack backtrace, and you don't show how that function is called, so there's no way to tell what is going wrong here.
How do you know it's crashing on line 9?
.

@janlely
Copy link
Author

janlely commented Sep 7, 2016

Thank you! I found it, it was the allocate problem

@hawicz hawicz closed this as completed Sep 7, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants