Search code examples
cjsonlinuxubuntujsmn

Getting Segmentation fault when trying to read a json file


I'm trying to parse a json file using jsmn but I get segmentation fault when I run my application. I'm using C and compiling on Ubuntu machine.

Please find the code snippet below:


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "jsmn.h"

#define JSON_FILE_PATH "/home/admin/Desktop/test/server/dataFile.json"
#define BUFFER_SIZE 5000
#define MAX_TOKEN_COUNT 128

// Read files
void readfile(char* filepath, char* fileContent)
{
    FILE *f;
    char c;
    int index;
    f = fopen(filepath, "r");
    ***while((c = fgetc(f)) != EOF){***     ------> seg fault
        fileContent[index] = c;
        index++;
    }
    fileContent[index] = '\0';
}

// This is where the magic happens
int parseJSON(char *filepath, void callback(char *, char*)){

    char JSON_STRING[BUFFER_SIZE];

    char value[1024];
    char key[1024];

    readfile(filepath, JSON_STRING);

   int i;
   int r;

   jsmn_parser p;
   jsmntok_t t[MAX_TOKEN_COUNT];

   jsmn_init(&p);

   r = jsmn_parse(&p, JSON_STRING, strlen(JSON_STRING), t, sizeof(t)/(sizeof(t[0])));

   if (r < 0) {
       printf("Failed to parse JSON: %d\n", r);
       return 1;
   }

   /* Assume the top-level element is an object */
   if (r < 1 || t[0].type != JSMN_OBJECT) {
       printf("Object expected\n");
       return 1;
   }

   for (i = 1; i < r; i++){

       jsmntok_t json_value = t[i+1];
       jsmntok_t json_key = t[i];


       int string_length = json_value.end - json_value.start;
       int key_length = json_key.end - json_key.start;

       int idx;

       for (idx = 0; idx < string_length; idx++){
           value[idx] = JSON_STRING[json_value.start + idx ];
       }

       for (idx = 0; idx < key_length; idx++){
           key[idx] = JSON_STRING[json_key.start + idx];
       }

       value[string_length] = '\0';
       key[key_length] = '\0';

       callback(key, value);

       i++;
   }

   return 0;
}

// Only prints the key and value
void mycallback(char *key, char* value){
    printf("%s : %s\n", key, value);
}

int main()
{
    parseJSON(JSON_FILE_PATH, mycallback);
    return 0;
}

I get segmentation fault after the line as indicated when the file operation of reading is done.

On debugging, at this point f contains 0x00 value which means it is not able to identify the file.

Why this might be happening when file is present at that location?

I tried changing paths but still same issue.


Solution

  • Found the solution:

    index is not initialized before use char c should be int c - fgetc returns an int