0

I am trying to create a temporary database using json-c. For that i have created a simple function which return a json object with default value as key:{"value":data} pair and then i am trying to update value of existing signals in database. But here i am getting segmentation fault when i have tried to print database after value update. I have no idea what is wrong here.

#include <stdio.h>
#include <string.h>
#include <json-c/json.h>
#include <stdlib.h>

static int db_created =0;
struct json_object* create_db(){
       struct json_object* obj;
       struct json_object* val;

       obj = json_object_new_object();
       val = json_object_new_object();

       json_object_object_add(val,"val",json_object_new_int(1001));
       json_object_object_add(obj,"key1",val);

       json_object_object_add(val,"val",json_object_new_int(1002));
       json_object_object_add(obj,"key2",val);

       json_object_object_add(val,"val",json_object_new_int(1003));
       json_object_object_add(obj,"key3",val);

       json_object_object_add(val,"val",json_object_new_int(1004));
       json_object_object_add(obj,"key4",val);

       json_object_object_add(val,"val",json_object_new_int(1005));
       json_object_object_add(obj,"key5",val);

       return obj;
}
void run(){
      struct json_object* db;
      struct json_object* val;
      struct json_object* temp;
      struct json_object *db2;

      val = json_object_new_object();
      if(db_created == 1){
            printf("database exist\n");
      }else{
           db = create_db();
           printf("database = %s\n",json_object_to_json_string(db));
           db_created = 1;
      }

      //Get a value for key1
      json_object_object_get_ex(db,"key1",&val);
      printf("value = %s\n",json_object_to_json_string(val));
    
      //update the value for key1
      json_object_object_add(val,"val",json_object_new_int(100));
      json_object_object_add(db,"ke1",val);

      //print database after updating key
      printf("database = %s\n",json_object_to_json_string(db));
}
int main(){
     run();
     return 0;
}
Evan Teran
  • 87,561
  • 32
  • 179
  • 238
  • Why not delete the global `static int db_created =0;`, and then use `static struct json_object *db = NULL;` in `run()` (delete unused `db2` and `temp`) and change your test to `if(db){..} else {..}`. Outputs without segfault. Issue was `db` was uninitialized when called if `db_created == 1` in your case. – David C. Rankin Aug 08 '22 at 06:43
  • Your use of only one `obj` seems to be causing issues in `create_db()` as all values will be that of the last `obj` set. – David C. Rankin Aug 08 '22 at 06:55

1 Answers1

0

Could not reproduce the segmentation fault. Please provide more details, such as the compiler version, build command line, OS version, json-c version.

By the way, your database building has at least one problem: You reused the val in the create_db function. So the value of the previous key will always be overwritten by next writing. So your first database print will have output like this:

database = {
    "key1": {
        "val": 1005
    },
    "key2": {
        "val": 1005
    },
    "key3": {
        "val": 1005
    },
    "key4": {
        "val": 1005
    },
    "key5": {
        "val": 1005
    }
}

The simple mitigation is creating a new object for each of your database item.

struct json_object *create_db() {
  struct json_object *obj;
  struct json_object *val;

  obj = json_object_new_object();

  val = json_object_new_object();
  json_object_object_add(val, "val", json_object_new_int(1001));
  json_object_object_add(obj, "key1", val);

  val = json_object_new_object();
  json_object_object_add(val, "val", json_object_new_int(1002));
  json_object_object_add(obj, "key2", val);

  val = json_object_new_object();
  json_object_object_add(val, "val", json_object_new_int(1003));
  json_object_object_add(obj, "key3", val);

  val = json_object_new_object();
  json_object_object_add(val, "val", json_object_new_int(1004));
  json_object_object_add(obj, "key4", val);

  val = json_object_new_object();
  json_object_object_add(val, "val", json_object_new_int(1005));
  json_object_object_add(obj, "key5", val);

  return obj;
}
Zongru Zhan
  • 546
  • 2
  • 8