Skip to content

Commit e81d813

Browse files
fix: add overflow checks for calloc operations in tokenize.cpp
Co-Authored-By: Jake Cosme <[email protected]>
1 parent 69b31b1 commit e81d813

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

tools/tokenize/tokenize.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ static std::vector<std::string> ingest_args(int raw_argc, char ** raw_argv) {
9999

100100
for (int i = 0; i < argc; ++i) {
101101
int length_needed = WideCharToMultiByte(CP_UTF8, 0, wargv[i], wcslen(wargv[i]), 0, 0, NULL, NULL);
102+
if (length_needed < 0 || length_needed >= INT_MAX) {
103+
GGML_ABORT("WideCharToMultiByte returned invalid length");
104+
}
102105
char * output_buf = (char *) calloc(length_needed+1, sizeof(char));
103106
GGML_ASSERT(output_buf);
104107

@@ -173,6 +176,9 @@ static void write_utf8_cstr_to_stdout(const char * str, bool & invalid_utf8) {
173176
GGML_ABORT("MultiByteToWideChar() failed in an unexpected way.");
174177
}
175178

179+
if (length_needed < 0 || length_needed >= INT_MAX) {
180+
GGML_ABORT("MultiByteToWideChar returned invalid length");
181+
}
176182
LPWSTR wstr = (LPWSTR) calloc(length_needed+1, sizeof(*wstr));
177183
GGML_ASSERT(wstr);
178184

0 commit comments

Comments
 (0)