0

I am getting a seg fault when I try and get the result of val from my json file the other fetch values return fine if I comment out the lines

/*parseText(obj, "val", &tmp);
strcpy (test.val, tmp);
printf("val = %s\n", test.val); */

This is the only one that is not an integer which I presume I am handling incorrectly.

Here is my code and the json file is below.

#include "stdio.h"
#include "string.h"
#include "json-c/json.h"

typedef struct __attribute__ ((packed))
{
    char val[4];
    uint32_t fetch_1;
    uint32_t fetch_2;
    uint32_t fetch_3;
    uint32_t fetch_4;
    uint32_t fetch_5;
}
TEST;

static int parseText(struct json_object *new_obj, const char *field,
                        void *val)
{
    struct json_object *o = NULL;
    json_type type;
    int ret = 0;

    if (!json_object_object_get_ex(new_obj, field, &o))
        printf("Field %s does not exist\n", field);

    type = json_object_get_type(o);

    switch (type) {
    case json_type_null:
        printf("Return type is NULL\n");
        ret = -1;
        break;
    case json_type_boolean:
        *((int *)(val)) = json_object_get_boolean(o);
        break;
    case json_type_double:
        *((double *)(val)) = json_object_get_double(o);
        break;
    case json_type_int:
        *((int *)(val)) = json_object_get_int(o);
        break;
    case json_type_string:
        val = (char *)json_object_get_string(o);
        break;
    case json_type_object:
    case json_type_array:
        ret = -1;;
        break;
    }
    return ret;

}
static inline int parsing(struct json_object *obj) {
    char* tmp = NULL;
    TEST test;
    memset(&test, 0, sizeof(test));

    parseText(obj, "val", &tmp);
    strcpy (test.val, tmp);
    printf("val = %s\n", test.val);
    parseText(obj, "fetch 1", &test.fetch_1);
    printf("fetch_1= %d\n", test.fetch_1);
    parseText(obj, "fetch 2", &test.fetch_2);
    printf("fetch_2= %d\n", test.fetch_2);
    parseText(obj, "fetch 3", &test.fetch_3);
    printf("fetch_3= %d\n", test.fetch_3);
    parseText(obj, "fetch 4", &test.fetch_4);
    printf("fetch_4= %d\n", test.fetch_4);
    parseText(obj, "fetch 5", &test.fetch_5);
    printf("fetch_5= %d\n", test.fetch_5);

    return 0;
}

char* file_read (const char* filename) {
  FILE* fp;
  char* buffer;
  long  fsize;

  /* Open the file */
  fp = fopen (filename, "r");

  if (fp == NULL)
    {
      return NULL;
    }

  /* Get the size of the file */
  fseek (fp, 0, SEEK_END);
  fsize = ftell (fp) + 1;
  fseek (fp, 0, SEEK_SET);

  /* Allocate the buffer */
  buffer = calloc (fsize, sizeof (char));

  if (buffer == NULL)
    {
      fclose (fp);
      return NULL;
    }

  /* Read the file */
  fread (buffer, sizeof (char), fsize, fp);

  /* Close the file */
  fclose (fp);

  /* Return the buffer */
  return buffer;
}

int main (int argc, char *argv[])
{
    struct json_object *jsonObj;
    int i = 0;

    char* file = file_read(argv[1]);

    jsonObj = json_tokener_parse(file);

    for(i = 0; i < json_object_array_length(jsonObj); i++) {
        json_object *obj = json_object_array_get_idx(jsonObj, i);
        parsing(obj);
    }

    return 0;
}

Here is the json file:

[{
    "val": "fpr",
    "fetch 1": 100,
    "fetch 2": 200,
    "fetch 3": 300,
    "fetch 4": 400,
    "fetch 5": 500
}, {
    "val": "mpr",
    "fetch 1": 445,
    "fetch 2": 223,
    "fetch 3": 1089,
    "fetch 4": 432,
    "fetch 5": 34400
}]
1
  • Your debugging approach is kinda crude. Make sure you build the program with debugging symbols included, and run it in a debugger, such as gdb. That will allow you to catch the segfault, to find exactly where it occurs, and to examine the values of the variables involved to determine the proximal cause. Commented May 10, 2017 at 13:03

1 Answer 1

4

The reason for your parseText failing on the field "val" is -

val = (char *)json_object_get_string(o);

This changes the local variable val. Which doesn't affect the tmp from the caller.

What you need is

*(char**) val = (char*) json_object_get_string(o);

This will change tmp to the point to the string returned by json_object_get_string.

In your case tmp is NULL and you are trying to copy from NULL after that which is Undefined Behavior and also is causing the SEG FAULT.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.