mirror of
https://github.com/webmproject/libwebp.git
synced 2025-01-03 17:28:22 +01:00
42f8f9346c
also change lossless encoder logic, which was relying on explicit NULL return from WebPSafeMalloc(0) renamed function to CheckSizeArgumentsOverflow() explicitly addresses issue #138 Change-Id: Ibbd51cc0281e60e86dfd4c5496274399e4c0f7f3
46 lines
1.5 KiB
C
46 lines
1.5 KiB
C
// Copyright 2012 Google Inc. All Rights Reserved.
|
|
//
|
|
// This code is licensed under the same terms as WebM:
|
|
// Software License Agreement: http://www.webmproject.org/license/software/
|
|
// Additional IP Rights Grant: http://www.webmproject.org/license/additional/
|
|
// -----------------------------------------------------------------------------
|
|
//
|
|
// Misc. common utility functions
|
|
//
|
|
// Author: Skal (pascal.massimino@gmail.com)
|
|
|
|
#include <stdlib.h>
|
|
#include "./utils.h"
|
|
|
|
#if defined(__cplusplus) || defined(c_plusplus)
|
|
extern "C" {
|
|
#endif
|
|
|
|
//------------------------------------------------------------------------------
|
|
// Checked memory allocation
|
|
|
|
// Returns 0 in case of overflow of nmemb * size.
|
|
static int CheckSizeArgumentsOverflow(uint64_t nmemb, size_t size) {
|
|
const uint64_t total_size = nmemb * size;
|
|
if (nmemb == 0) return 1;
|
|
if ((uint64_t)size > WEBP_MAX_ALLOCABLE_MEMORY / nmemb) return 0;
|
|
if (total_size != (size_t)total_size) return 0;
|
|
return 1;
|
|
}
|
|
|
|
void* WebPSafeMalloc(uint64_t nmemb, size_t size) {
|
|
if (!CheckSizeArgumentsOverflow(nmemb, size)) return NULL;
|
|
return (nmemb > 0 && size > 0) ? malloc((size_t)(nmemb * size)) : NULL;
|
|
}
|
|
|
|
void* WebPSafeCalloc(uint64_t nmemb, size_t size) {
|
|
if (!CheckSizeArgumentsOverflow(nmemb, size)) return NULL;
|
|
return (nmemb > 0 && size > 0) ? calloc((size_t)nmemb, size) : NULL;
|
|
}
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
#if defined(__cplusplus) || defined(c_plusplus)
|
|
} // extern "C"
|
|
#endif
|