From e9dcc37498764149c139cb891f16d0c6ce248304 Mon Sep 17 00:00:00 2001 From: Paul Tsochantaris Date: Mon, 29 Jan 2024 20:59:43 +0000 Subject: [PATCH] Fix for a null pointer dereference if a metal GGML buffer fails to be allocated --- ggml-alloc.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/ggml-alloc.c b/ggml-alloc.c index dfe5ba2e5..9d5b88567 100644 --- a/ggml-alloc.c +++ b/ggml-alloc.c @@ -788,10 +788,12 @@ static bool alloc_tensor_range(struct ggml_context * ctx, #ifndef NDEBUG fprintf(stderr, "%s: failed to allocate %s buffer of size %zu\n", __func__, ggml_backend_buft_name(buft), size); #endif - for (size_t i = 0; i < *n_buffers; i++) { - ggml_backend_buffer_free(*buffers[i]); + if (buffers && *buffers) { + for (size_t i = 0; i < *n_buffers; i++) { + ggml_backend_buffer_free(*buffers[i]); + } + free(buffers); } - free(buffers); return false; } @@ -843,10 +845,12 @@ ggml_backend_buffer_t ggml_backend_alloc_ctx_tensors_from_buft(struct ggml_conte __func__, t->name, ggml_backend_buft_name(buft), this_size, max_size); - for (size_t i = 0; i < n_buffers; i++) { - ggml_backend_buffer_free(buffers[i]); + if(buffers && *buffers) { + for (size_t i = 0; i < n_buffers; i++) { + ggml_backend_buffer_free(buffers[i]); + } + free(buffers); } - free(buffers); return NULL; }