调用我的函数会导致程序崩溃 - C | 3DS Homebrew

时间:2015-08-26 18:58:49

标签: c crash homebrew

我一直在为Nintendo 3DS编写一个自制软件,用C语言编写。它只是解析一个JSON文件并将其打印到屏幕上。问题是在解析并在屏幕上打印之后,它就会崩溃。

代码:

char JSON_FILE[] = "jsbr-ford-mustang.json";
static int jsoneq(const char *json, jsmntok_t *tok, const char *s) {
    if (tok->type == JSMN_STRING && (int) strlen(s) == tok->end - tok->start &&
        strncmp(json + tok->start, s, tok->end - tok->start) == 0) {
        return 0;
    }
    return -1;
}

const char * parse_json(char* value) {
    u8* file_buffer; FILE *file = fopen(JSON_FILE,"rb"); if (file == NULL) printf("Error.\n");
    fseek(file,0,SEEK_END); off_t size = ftell(file); fseek(file,0,SEEK_SET); file_buffer=malloc(size);
    if(!file_buffer) printf("Error.\n");
    off_t bytesRead = fread(file_buffer,1,size,file); fclose(file);
    if(size!=bytesRead) printf("Error.\n");
    int i; int r;
    jsmn_parser p; jsmntok_t t[128]; jsmn_init(&p);
    r = jsmn_parse(&p, file_buffer, size, t, sizeof(t)/sizeof(t[0]));

    if (r < 0) {
        printf("Failed to parse JSON: %d\n", r);
        return 1;
    }
    if (r < 1 || t[0].type != JSMN_OBJECT) {
        printf("Object expected\n");
        return 1;
    }
    printf("Debug START\n");
    for (i = 1; i < r; i++) {
        if (jsoneq(file_buffer, &t[i], value) == 0) {
            printf("Debug 1\n");
            break;
        }
        printf("Debug 2\n");
    }
    printf("Debug 3\n");
    return printf("%.*s\n", t[i+1].end-t[i+1].start, file_buffer + t[i+1].start);
}

int main() {
    gfxInitDefault();
    consoleInit(GFX_TOP,NULL);
    printf("P1\n");
    printf("Description: %s",parse_json("description"));
    printf("P2\n");
    printf("Sync spacing: %s",parse_json("synchronization_spacing_us"));
    while (aptMainLoop()) {
        hidScanInput(); u32 kDown = hidKeysDown();
        if(kDown & KEY_START) {
            consoleClear();
            break;
        }
        gfxFlushBuffers();
        gfxSwapBuffers();
    }
    gfxExit();
    return 0;
}

调试输出:

P1
Debug START
Debug 2
Debug 2
Debug 1
Debug 3
Ford Mustang, 40MHz, No. 23019

以下是视频:https://www.youtube.com/watch?v=zpt_BEMyIOc

这是我拥有的GitHub回购:https://github.com/lavanoid/3DS_JSON_Parser

0 个答案:

没有答案