From 01b8acbc9071569a4ca24205d636d433ef5eed41 Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Tue, 22 Apr 2014 17:49:49 +0200 Subject: [PATCH] Fix calloc not erroring on multiplication overflow. --- libc/stdlib/calloc.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/libc/stdlib/calloc.cpp b/libc/stdlib/calloc.cpp index ff7daf89..34b2edb5 100644 --- a/libc/stdlib/calloc.cpp +++ b/libc/stdlib/calloc.cpp @@ -1,6 +1,6 @@ /******************************************************************************* - Copyright(C) Jonas 'Sortie' Termansen 2011, 2012. + Copyright(C) Jonas 'Sortie' Termansen 2011, 2012, 2014. This file is part of the Sortix C Library. @@ -22,11 +22,15 @@ *******************************************************************************/ +#include +#include #include #include extern "C" void* calloc(size_t nmemb, size_t size) { + if ( size && nmemb && SIZE_MAX / size < nmemb ) + return errno = ENOMEM, (void*) NULL; size_t total = nmemb * size; void* result = malloc(total); if ( !result )