C语言 使用Jansson从管道解析JSON数据时的核心转储

kmpatx3s  于 11个月前  发布在  其他
关注(0)|答案(1)|浏览(108)

编辑-添加了一个最小的可复制代码
我试图从命名管道中读取一些JSON数据。我需要在C中解析JSON,我使用Jansson库来完成。然而,当我试图解析JSON对象时,我得到一个核心转储错误。
JSON数据示例:

{
    "object_ids": [0], 
    "object_classes": ["Person"], 
    "confidences": [0.5], 
    "bounding_boxes": [[0.49161258339881897, 0.40708258748054504, 0.10166069865226746, 0.30669090151786804]]
}

字符串
用于解析它的代码片段(注意,这是一个更大的代码的一部分,但这应该可以工作):

#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <jansson.h>

// A pipe has already been created prior to execution of the code
#define PIPE_NAME "/tmp/xyz"

typedef struct {
    int obj_ids[MAX_ENTRIES];
    char obj_names[MAX_ENTRIES][16];
    double conf[MAX_ENTRIES];
    double coords[MAX_ENTRIES][4];
} ConvertedData;

static ConvertedData *converted_data = NULL;

int main() {
    // A buffer to store read from the pipe
    char buffer[4096];
    
    int fd = open(PIPE_NAME, O_RDONLY);
        if (fd == -1) {
            perror("Error opening named pipe");
            exit(EXIT_FAILURE);
        }

        // Read data from the named pipe
        ssize_t bytesRead = read(fd, buffer, sizeof(buffer));
        if (bytesRead == -1) {
            perror("Error reading from named pipe");
            close(fd);
        }
    
    json_error_t error;
        json_t *obj = json_loads(buffer, 0, &error);

        if (!obj) {
            syslog(LOG_ERR, "ERROR: json object on line %d: %s\n", error.line, error.text);
        }
        else {
            json_t *obj_ids = json_object_get(obj, "object_ids");
            json_incref(obj_ids);
            json_t *classes = json_object_get(obj, "object_classes");
            json_incref(classes);
            json_t *confs = json_object_get(obj, "confidences");
            json_incref(confs);
            json_t *bboxes = json_object_get(obj, "bounding_boxes");
            json_incref(bboxes);

            if (json_is_array(obj_ids) && json_is_array(classes) && 
            json_is_array(confs) && json_is_array(bboxes)) {
                syslog(LOG_INFO, "Inside IF");

                size_t arr_size = json_array_size(confs);

                for(size_t i = 0; i < arr_size; i++) {
                    syslog(LOG_INFO, "Inside Loop");
                    // Store the Object IDS
                    converted_data->obj_ids[i] = json_integer_value(json_array_get(obj_ids, i));

                    // Store the Object Names
                    const char *obj_class = json_string_value(json_array_get(classes, i));
                    strncpy(converted_data->obj_names[i], obj_class, sizeof(converted_data->obj_names[i]) -1);
                    converted_data->obj_names[i][sizeof(converted_data->obj_names[i]) -1] = '\0';
                    
                    // Store the Bounding Boxes
                    json_t *bbox = json_array_get(bboxes, i);
                    json_incref(bbox);
                    if(json_is_array(bbox) && json_array_size(bbox) == 4) {
                        for(size_t j = 0; j < 4; j++) {
                            converted_data->coords[i][j] = json_real_value(json_array_get(bbox, j));
                        }
                    }
                    json_decref(bbox);

                    converted_data->conf[i] = json_real_value(json_array_get(confs, i));
                }
            }
            else {
                syslog(LOG_ERR, "ERROR: Incorrect JSON data structure");
            }

            json_decref(obj_ids);
            json_decref(classes);
            json_decref(confs);
            json_decref(bboxes);
            json_decref(obj);
        }
        
        // Further Processing
    
    return 0;
}


请注意,循环一开始,我就得到一个核心转储。我做错了什么?
编辑:
经过一些调试后,当我尝试将数据存储在结构中时发生错误。

fxnxkyjh

fxnxkyjh1#

我修改了从stdin读取JSON的程序,并添加了:

#include <syslog.h>
#define MAX_ENTRIES 42
#define syslog(level, ...) printf(__VA_ARGS__)

字符串
程序段故障发生在:

0x00005555555554a0 in main () at your.c:60
60                          converted_data->obj_ids[i] = json_integer_value(json_array_get(obj_ids, i));


converted_data是一个空指针。分配一个对象,例如,通过将变量移动到main()

ConvertedData *converted_data = &(ConvertedData) {0};


示例会话:

$ ./a.out < your.json
Inside IFInside Loop$

相关问题