Path: blob/master/src/java.desktop/share/native/libsplashscreen/giflib/openbsd-reallocarray.c
41153 views
/*1* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.2*3* This code is free software; you can redistribute it and/or modify it4* under the terms of the GNU General Public License version 2 only, as5* published by the Free Software Foundation. Oracle designates this6* particular file as subject to the "Classpath" exception as provided7* by Oracle in the LICENSE file that accompanied this code.8*9* This code is distributed in the hope that it will be useful, but WITHOUT10* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or11* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License12* version 2 for more details (a copy is included in the LICENSE file that13* accompanied this code).14*15* You should have received a copy of the GNU General Public License version16* 2 along with this work; if not, write to the Free Software Foundation,17* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.18*19* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA20* or visit www.oracle.com if you need additional information or have any21* questions.22*/2324/* $OpenBSD: reallocarray.c,v 1.1 2014/05/08 21:43:49 deraadt Exp $ */25/*26* Copyright (c) 2008 Otto Moerbeek <[email protected]>27* SPDX-License-Identifier: MIT28*/2930#include <sys/types.h>31#include <errno.h>32#include <stdint.h>33#include <stdlib.h>3435#ifndef SIZE_MAX36#define SIZE_MAX UINTPTR_MAX37#endif3839/*40* This is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX41* if both s1 < MUL_NO_OVERFLOW and s2 < MUL_NO_OVERFLOW42*/43#define MUL_NO_OVERFLOW ((size_t)1 << (sizeof(size_t) * 4))4445void *46openbsd_reallocarray(void *optr, size_t nmemb, size_t size)47{48if ((nmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&49nmemb > 0 && SIZE_MAX / nmemb < size) {50errno = ENOMEM;51return NULL;52}53/*54* Head off variations in realloc behavior on different55* platforms (reported by MarkR <[email protected]>)56*57* The behaviour of reallocarray is implementation-defined if58* nmemb or size is zero. It can return NULL or non-NULL59* depending on the platform.60* https://www.securecoding.cert.org/confluence/display/c/MEM04-C.Beware+of+zero-lengthallocations61*62* Here are some extracts from realloc man pages on different platforms.63*64* void realloc( void memblock, size_t size );65*66* Windows:67*68* If there is not enough available memory to expand the block69* to the given size, the original block is left unchanged,70* and NULL is returned. If size is zero, then the block71* pointed to by memblock is freed; the return value is NULL,72* and memblock is left pointing at a freed block.73*74* OpenBSD:75*76* If size or nmemb is equal to 0, a unique pointer to an77* access protected, zero sized object is returned. Access via78* this pointer will generate a SIGSEGV exception.79*80* Linux:81*82* If size was equal to 0, either NULL or a pointer suitable83* to be passed to free() is returned.84*85* OS X:86*87* If size is zero and ptr is not NULL, a new, minimum sized88* object is allocated and the original object is freed.89*90* It looks like images with zero width or height can trigger91* this, and fuzzing behaviour will differ by platform, so92* fuzzing on one platform may not detect zero-size allocation93* problems on other platforms.94*/95if (size == 0 || nmemb == 0)96return NULL;97return realloc(optr, size * nmemb);98}99100101