]> wimlib.net Git - wimlib/commitdiff
util.c: check for calloc multiplication overflow
authorEric Biggers <ebiggers3@gmail.com>
Sat, 17 Dec 2016 03:47:44 +0000 (19:47 -0800)
committerEric Biggers <ebiggers3@gmail.com>
Sat, 17 Dec 2016 03:47:44 +0000 (19:47 -0800)
src/util.c

index e0c5cc1bbc94ec0421bfd6184ee4834495c6f1e0..47aedd428f79e5358e0feb83978a661cd55d5451 100644 (file)
@@ -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
  *
  * 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 "config.h"
 #endif
 
+#include <errno.h>
 #include <limits.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <limits.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -82,7 +83,14 @@ void *
 wimlib_calloc(size_t nmemb, size_t size)
 {
        size_t total_size = nmemb * size;
 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;
        if (p)
                p = memset(p, 0, total_size);
        return p;