|
| 1 | +/* |
| 2 | + +----------------------------------------------------------------------+ |
| 3 | + | Copyright (c) The PHP Group | |
| 4 | + +----------------------------------------------------------------------+ |
| 5 | + | This source file is subject to version 3.01 of the PHP license, | |
| 6 | + | that is bundled with this package in the file LICENSE, and is | |
| 7 | + | available through the world-wide-web at the following url: | |
| 8 | + | https://www.php.net/license/3_01.txt | |
| 9 | + | If you did not receive a copy of the PHP license and are unable to | |
| 10 | + | obtain it through the world-wide-web, please send a note to | |
| 11 | + | license@php.net so we can mail you a copy immediately. | |
| 12 | + +----------------------------------------------------------------------+ |
| 13 | + | Author: | |
| 14 | + +----------------------------------------------------------------------+ |
| 15 | +*/ |
| 16 | + |
| 17 | +#include "php.h" |
| 18 | + |
| 19 | +#ifdef USE_REALLOCARRAY_PHP_IMPL |
| 20 | +/* $OpenBSD: reallocarray.c,v 1.3 2015/09/13 08:31:47 guenther Exp $ */ |
| 21 | +/* |
| 22 | + * Copyright (c) 2008 Otto Moerbeek <otto@drijf.net> |
| 23 | + * |
| 24 | + * Permission to use, copy, modify, and distribute this software for any |
| 25 | + * purpose with or without fee is hereby granted, provided that the above |
| 26 | + * copyright notice and this permission notice appear in all copies. |
| 27 | + * |
| 28 | + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 29 | + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 30 | + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 31 | + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 32 | + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 33 | + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 34 | + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 35 | + */ |
| 36 | + |
| 37 | +#include <sys/types.h> |
| 38 | +#include <errno.h> |
| 39 | +#include <stdint.h> |
| 40 | +#include <stdlib.h> |
| 41 | + |
| 42 | +/* |
| 43 | + * This is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX |
| 44 | + * if both s1 < MUL_NO_OVERFLOW and s2 < MUL_NO_OVERFLOW |
| 45 | + */ |
| 46 | +#define MUL_NO_OVERFLOW ((size_t)1 << (sizeof(size_t) * 4)) |
| 47 | + |
| 48 | +PHPAPI void * |
| 49 | +php_reallocarray(void *optr, size_t nmemb, size_t size) |
| 50 | +{ |
| 51 | + if ((nmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) && |
| 52 | + nmemb > 0 && SIZE_MAX / nmemb < size) { |
| 53 | + errno = ENOMEM; |
| 54 | + return NULL; |
| 55 | + } |
| 56 | + return realloc(optr, size * nmemb); |
| 57 | +} |
| 58 | + |
| 59 | +#endif /* !HAVE_REALLOCARRAY */ |
0 commit comments