Skip to content

Commit 3f1deb1

Browse files
jimbothigpenclaude
andcommitted
fix(imatrix): null mtp_batch.token after free to prevent double-free
llama_batch_free() checks and frees batch.token if non-null. Since we manually allocate mtp_batch.token (llama_batch_init with n_embd>0 only allocates embd, not token), we must null the pointer after our explicit free() in all three cleanup paths before calling llama_batch_free(). Bug present in upstream ggml-org#23476 as well; added here as fork-specific fix. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent b82080b commit 3f1deb1

1 file changed

Lines changed: 3 additions & 0 deletions

File tree

tools/imatrix/imatrix.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,6 +1101,7 @@ static bool compute_imatrix(llama_context * ctx, llama_context * ctx_mtp, const
11011101
llama_batch_free(batch);
11021102
if (mtp_enabled) {
11031103
free(mtp_batch.token);
1104+
mtp_batch.token = nullptr;
11041105
llama_batch_free(mtp_batch);
11051106
}
11061107
return false;
@@ -1115,6 +1116,7 @@ static bool compute_imatrix(llama_context * ctx, llama_context * ctx_mtp, const
11151116
pos_first, n_embd, /*seq_id=*/0, mtp_batch)) {
11161117
llama_batch_free(batch);
11171118
free(mtp_batch.token);
1119+
mtp_batch.token = nullptr;
11181120
llama_batch_free(mtp_batch);
11191121
return false;
11201122
}
@@ -1181,6 +1183,7 @@ static bool compute_imatrix(llama_context * ctx, llama_context * ctx_mtp, const
11811183
llama_batch_free(batch);
11821184
if (mtp_enabled) {
11831185
free(mtp_batch.token);
1186+
mtp_batch.token = nullptr;
11841187
llama_batch_free(mtp_batch);
11851188
}
11861189

0 commit comments

Comments
 (0)