Skip to content

Commit 6867a40

Browse files
author
Aaron
committed
fix(ggml): use safe character conversion in ggml_fopen on Windows
This commit fixes an unsafe character conversion in the `ggml_fopen` function on Windows. The file `mode` string was being converted from `char *` to `wchar_t *` by simple casting, which is not safe for multi-byte character sets. This change replaces the manual conversion with a call to the `ggml_mbstowcs` helper function, which properly handles UTF-8 to wide character conversion. This makes the code more robust and correct, especially for handling file paths on Windows.
1 parent fa882fd commit 6867a40

File tree

1 file changed

+7
-10
lines changed

1 file changed

+7
-10
lines changed

ggml/src/ggml.c

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -562,18 +562,15 @@ FILE * ggml_fopen(const char * fname, const char * mode) {
562562
// convert fname (UTF-8)
563563
wchar_t * wfname = ggml_mbstowcs(fname);
564564
if (wfname) {
565-
// convert mode (ANSI)
566-
wchar_t * wmode = GGML_MALLOC((strlen(mode) + 1) * sizeof(wchar_t));
567-
wchar_t * wmode_p = wmode;
568-
do {
569-
*wmode_p++ = (wchar_t)*mode;
570-
} while (*mode++);
571-
572-
// open file
573-
file = _wfopen(wfname, wmode);
565+
// convert mode (UTF-8)
566+
wchar_t * wmode = ggml_mbstowcs(mode);
567+
if (wmode) {
568+
// open file
569+
file = _wfopen(wfname, wmode);
570+
GGML_FREE(wmode);
571+
}
574572

575573
GGML_FREE(wfname);
576-
GGML_FREE(wmode);
577574
}
578575

579576
return file;

0 commit comments

Comments
 (0)