From: Eric Biggers Date: Sat, 17 Dec 2016 03:47:44 +0000 (-0800) Subject: util.c: check for calloc multiplication overflow X-Git-Tag: v1.11.0~37 X-Git-Url: https://wimlib.net/git/?p=wimlib;a=commitdiff_plain;h=1d5cae645b413239294cb98bf6b16b281ae10df7 util.c: check for calloc multiplication overflow --- diff --git a/src/util.c b/src/util.c index e0c5cc1b..47aedd42 100644 --- a/src/util.c +++ b/src/util.c @@ -3,7 +3,7 @@ */ /* - * Copyright (C) 2012, 2013, 2014 Eric Biggers + * Copyright (C) 2012-2016 Eric Biggers * * This file is free software; you can redistribute it and/or modify it under * the terms of the GNU Lesser General Public License as published by the Free @@ -23,6 +23,7 @@ # include "config.h" #endif +#include #include #include #include @@ -82,7 +83,14 @@ void * wimlib_calloc(size_t nmemb, size_t size) { size_t total_size = nmemb * size; - void *p = MALLOC(total_size); + void *p; + + if (size != 0 && nmemb > SIZE_MAX / size) { + errno = ENOMEM; + return NULL; + } + + p = MALLOC(total_size); if (p) p = memset(p, 0, total_size); return p;