From a32b77c4b2c1808654d0b952f26c37d73d2e746b Mon Sep 17 00:00:00 2001 From: Rick G <26732651+TheFlipbook@users.noreply.github.com> Date: Sun, 24 Mar 2024 14:45:56 -0700 Subject: [PATCH] Fix heap corruption from wmode out-of-bound writes on windows (#6272) * would throw error on VS2022 on GGML_FREE(wmode) * wchar_t is usually 2 bytes, but malloc wants bytes * therefore `*wmode_p++ = (wchar_t)*mode;` could write off the end of the allocation * Fixes error possibly introduced by https://github.com/ggerganov/llama.cpp/pull/6248 --- ggml.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ggml.c b/ggml.c index 18f10a3dc..203a9e540 100644 --- a/ggml.c +++ b/ggml.c @@ -465,7 +465,7 @@ FILE * ggml_fopen(const char * fname, const char * mode) { wchar_t * wfname = ggml_mbstowcs(fname); if (wfname) { // convert mode (ANSI) - wchar_t * wmode = GGML_MALLOC(strlen(mode) + 1); + wchar_t * wmode = GGML_MALLOC((strlen(mode) + 1) * sizeof(wchar_t)); wchar_t * wmode_p = wmode; do { *wmode_p++ = (wchar_t)*mode;