]> git.djapps.eu Git - pkg/ggml/sources/ggml/commitdiff
Fix heap corruption from wmode out-of-bound writes on windows (llama/6272)
authorRick G <redacted>
Sun, 24 Mar 2024 21:45:56 +0000 (14:45 -0700)
committerGeorgi Gerganov <redacted>
Wed, 27 Mar 2024 11:20:00 +0000 (13:20 +0200)
* 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

src/ggml.c

index 18f10a3dc2f75eca7e6b110178c3a93891663a19..203a9e54038d79176ade78850c223db894af5d67 100644 (file)
@@ -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;