Allocate buffer for support/font/lang files properly (#894)

Merged, thank you!
This commit is contained in:
Danny Tsai 2025-10-18 00:58:29 +08:00 committed by GitHub
parent e83f58e841
commit 320a3fe0ee
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 26 additions and 17 deletions

View File

@ -7,19 +7,25 @@
#define SUPPORT_DIR_PATHS "V:", "0:/gm9", "1:/gm9" #define SUPPORT_DIR_PATHS "V:", "0:/gm9", "1:/gm9"
bool CheckSupportFile(const char* fname) bool CheckSupportFile(const char* fname, size_t* fsize)
{ {
// try VRAM0 first // try VRAM0 first
if (FindVTarFileInfo(fname, NULL)) u64 tar_fsize;
if (FindVTarFileInfo(fname, &tar_fsize)) {
if (fsize) *fsize = tar_fsize; // truncated but should be fine for vtar
return true; return true;
}
// try support file paths // try support file paths
const char* base_paths[] = { SUPPORT_FILE_PATHS }; const char* base_paths[] = { SUPPORT_FILE_PATHS };
for (u32 i = 0; i < countof(base_paths); i++) { for (u32 i = 0; i < countof(base_paths); i++) {
char path[256]; char path[256];
FILINFO fno;
snprintf(path, sizeof(path), "%s/%s", base_paths[i], fname); snprintf(path, sizeof(path), "%s/%s", base_paths[i], fname);
if (fvx_stat(path, NULL) == FR_OK) if (fvx_stat(path, &fno) == FR_OK) {
if (fsize) *fsize = fno.fsize;
return true; return true;
}
} }
return false; return false;

View File

@ -8,7 +8,7 @@
#define LUASCRIPTS_DIR "luascripts" #define LUASCRIPTS_DIR "luascripts"
#define PAYLOADS_DIR "payloads" #define PAYLOADS_DIR "payloads"
bool CheckSupportFile(const char* fname); bool CheckSupportFile(const char* fname, size_t* fsize);
size_t LoadSupportFile(const char* fname, void* buffer, size_t max_len); size_t LoadSupportFile(const char* fname, void* buffer, size_t max_len);
bool SaveSupportFile(const char* fname, void* buffer, size_t len); bool SaveSupportFile(const char* fname, void* buffer, size_t len);
bool SetAsSupportFile(const char* fname, const char* source); bool SetAsSupportFile(const char* fname, const char* source);

View File

@ -2164,18 +2164,20 @@ u32 FileHandlerMenu(char* current_path, u32* cursor, u32* scroll, PaneData** pan
return 0; return 0;
} }
else if (user_select == font) { // set font else if (user_select == font) { // set font
u8* font = (u8*) malloc(0x20000); // arbitrary, should be enough by far size_t fsize = FileGetSize(file_path);
u8* font = (u8*) malloc(fsize);
if (!font) return 1; if (!font) return 1;
u32 font_size = FileGetData(file_path, font, 0x20000, 0); u32 font_size = FileGetData(file_path, font, fsize, 0);
if (font_size) SetFont(font, font_size); if (font_size) SetFont(font, font_size);
ClearScreenF(true, true, COLOR_STD_BG); ClearScreenF(true, true, COLOR_STD_BG);
free(font); free(font);
return 0; return 0;
} }
else if (user_select == translation) { // set translation else if (user_select == translation) { // set translation
u8* translation = (u8*) malloc(0x20000); // arbitrary, should be enough by far size_t fsize = FileGetSize(file_path);
u8* translation = (u8*) malloc(fsize);
if (!translation) return 1; if (!translation) return 1;
u32 translation_size = FileGetData(file_path, translation, 0x20000, 0); u32 translation_size = FileGetData(file_path, translation, fsize, 0);
if (translation_size) SetLanguage(translation, translation_size); if (translation_size) SetLanguage(translation, translation_size);
ClearScreenF(true, true, COLOR_STD_BG); ClearScreenF(true, true, COLOR_STD_BG);
free(translation); free(translation);
@ -2442,17 +2444,18 @@ u32 GodMode(int entrypoint) {
SetScreenBrightness(brightness); SetScreenBrightness(brightness);
// custom font handling // custom font handling
if (CheckSupportFile("font.frf")) { size_t support_size;
u8* riff = (u8*) malloc(0x20000); // arbitrary, should be enough by far if (CheckSupportFile("font.frf", &support_size)) {
u8* riff = (u8*) malloc(support_size);
if (riff) { if (riff) {
u32 riff_size = LoadSupportFile("font.frf", riff, 0x20000); u32 riff_size = LoadSupportFile("font.frf", riff, support_size);
if (riff_size) SetFont(riff, riff_size); if (riff_size) SetFont(riff, riff_size);
free(riff); free(riff);
} }
} else if (CheckSupportFile("font.pbm")) { } else if (CheckSupportFile("font.pbm", &support_size)) {
u8* pbm = (u8*) malloc(0x10000); // arbitrary, should be enough by far u8* pbm = (u8*) malloc(support_size);
if (pbm) { if (pbm) {
u32 pbm_size = LoadSupportFile("font.pbm", pbm, 0x10000); u32 pbm_size = LoadSupportFile("font.pbm", pbm, support_size);
if (pbm_size) SetFont(pbm, pbm_size); if (pbm_size) SetFont(pbm, pbm_size);
free(pbm); free(pbm);
} }
@ -2460,10 +2463,10 @@ u32 GodMode(int entrypoint) {
// language handling // language handling
bool language_loaded = false; bool language_loaded = false;
if (CheckSupportFile("language.trf")) { if (CheckSupportFile("language.trf", &support_size)) {
char* translation = (char*) malloc(0x20000); // arbitrary, should be enough by far char* translation = (char*) malloc(support_size);
if (translation) { if (translation) {
u32 translation_size = LoadSupportFile("language.trf", translation, 0x20000); u32 translation_size = LoadSupportFile("language.trf", translation, support_size);
if (translation_size) language_loaded = SetLanguage(translation, translation_size); if (translation_size) language_loaded = SetLanguage(translation, translation_size);
free(translation); free(translation);
} }