调用我的函数,导致程序崩溃 - C | 3DS 自制软件

Calling my function, causes the program to crash - C | 3DS 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

来自 cplusplus.com 关于 printf() 的 return 值:

On success, the total number of characters written is returned.

If a writing error occurs, the error indicator (ferror) is set and a negative number is returned.

If a multibyte character encoding error occurs while writing wide characters, errno is set to EILSEQ and a negative number is returned.

你不能这样做:return printf("%.*s\n", t[i+1].end-t[i+1].start, file_buffer + t[i+1].start);

因为您的函数需要 return 一个 const char *,而不是一个 int,它是 return 从 printf() 编辑的。

您应该使用 sprintf() 将您想要的字符串打印到缓冲区中(例如 tempString)

static char *tempString;
...
if(tempstring != NULL) {
    free(tempString);
    tempString = NULL;
}
tempString = (char *)malloc(t[i+1].end-t[i+1].start+1);
if(tempString != NULL) {
    sprintf(tempString,"%s", file_buffer + t[i+1].start);
    return (const char *)tempString;
} else {
    return "malloc error!";
}