From 747a694f7f45a733fcda13ebce810a84188a74a4 Mon Sep 17 00:00:00 2001 From: Sebastian Ramacher Date: Sat, 3 Dec 2011 14:43:41 +0100 Subject: [PATCH] Fix problems with nmemb = 0 in safe_realloc (Closes: #80) We need to handle this one case. It's not safe to use safe_realloc with size or nmemb = 0. --- zathura.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/zathura.c b/zathura.c index 218d7d3..aa736c7 100644 --- a/zathura.c +++ b/zathura.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #include @@ -1546,6 +1547,9 @@ safe_realloc(void** ptr, size_t nmemb, size_t size) static const size_t limit = ~((size_t)0u); void* tmp = NULL; + assert(nmemb != 0); + assert(size != 0); + /* Check for overflow. */ if(nmemb > limit / size) goto failure; @@ -2988,9 +2992,14 @@ isc_completion(Argument* argument) } } - rows = safe_realloc((void**)&rows, n_items, sizeof(CompletionRow)); - if(!rows) - out_of_memory(); + if (n_items == 0) { + free(rows); + rows = NULL; + } else { + rows = safe_realloc((void**)&rows, n_items, sizeof(CompletionRow)); + if(!rows) + out_of_memory(); + } } gtk_box_pack_start(Zathura.UI.box, GTK_WIDGET(results), FALSE, FALSE, 0);