首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >json-c:在json_tokener_parse中出现分段错误

json-c:在json_tokener_parse中出现分段错误
EN

Stack Overflow用户
提问于 2018-05-25 01:19:55
回答 1查看 541关注 0票数 0

我正在使用json-c来解析我项目中的json文件。我尝试创建json_tokener_parse,但这导致了seg-fault。请检查一下并告诉我segfault的原因。

代码语言:javascript
复制
#include <sys/mman.h> 
#include <sys/stat.h>
#include <fcntl.h> // O_RDONLY
#include<stdlib.h>
#include<stdio.h>
#include<unistd.h>
#include<json-c/json.h>

int main() {
    int oflag = O_RDONLY;
    const char *path = "file.json";
    const int fd = open(path, oflag);

    // use stat to find the file size
    struct stat stat;
    int ret = fstat(fd, &stat);

    int mflags = MAP_SHARED; // information about handling the mapped data
    int mprot = PROT_READ|PROT_WRITE; // access permissions to the data being mapped 
    size_t size = stat.st_size;
    void *addr = mmap(NULL, size, mprot, mflags, fd, 0);
    const char *file = (char *)addr;

    json_object * jobj = json_tokener_parse(addr);     
    //json_parse(jobj);
}
EN

回答 1

Stack Overflow用户

发布于 2018-05-25 23:46:06

json_tokener_parse()接受以null结尾的字符串。文本文件不是以null结尾的。您必须使用json_tokener_parse_ex()并指定长度。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50514893

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档