|
1 | | -/* $OpenBSD: reallocarray.c,v 1.1 2014/05/08 21:43:49 deraadt Exp $ */ |
| 1 | +/* From: https://git.musl-libc.org/cgit/musl/commit/?id=821083ac7b54eaa040d5a8ddc67c6206a175e0ca */ |
2 | 2 | /* |
3 | | - * Copyright (c) 2008 Otto Moerbeek <otto@drijf.net> |
| 3 | + * Copyright (c) 2005-2020 Rich Felker, et al. |
4 | 4 | * |
5 | | - * Permission to use, copy, modify, and distribute this software for any |
6 | | - * purpose with or without fee is hereby granted, provided that the above |
7 | | - * copyright notice and this permission notice appear in all copies. |
8 | | - * |
9 | | - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
10 | | - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
11 | | - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
12 | | - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
13 | | - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
14 | | - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
15 | | - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 5 | + * Permission is hereby granted, free of charge, to any person obtaining |
| 6 | + * a copy of this software and associated documentation files (the |
| 7 | + * "Software"), to deal in the Software without restriction, including |
| 8 | + * without limitation the rights to use, copy, modify, merge, publish, |
| 9 | + * distribute, sublicense, and/or sell copies of the Software, and to |
| 10 | + * permit persons to whom the Software is furnished to do so, subject to |
| 11 | + * the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be |
| 14 | + * included in all copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 17 | + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 18 | + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 19 | + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 20 | + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 21 | + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 22 | + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
16 | 23 | */ |
17 | | - |
18 | | -#include <sys/types.h> |
| 24 | + |
| 25 | +#ifdef HAVE_CONFIG_H |
| 26 | +#include <config.h> |
| 27 | +#endif /*HAVE_CONFIG_H*/ |
19 | 28 |
|
20 | 29 | #include <errno.h> |
21 | | -#include <stdint.h> |
22 | 30 | #include <stdlib.h> |
23 | 31 |
|
24 | | -/* |
25 | | - * This is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX |
26 | | - * if both s1 < MUL_NO_OVERFLOW and s2 < MUL_NO_OVERFLOW |
27 | | - */ |
28 | | -#define MUL_NO_OVERFLOW (1UL << (sizeof(size_t) * 4)) |
| 32 | +#ifndef HAVE_REALLOCARRAY |
29 | 33 |
|
30 | 34 | void * |
31 | 35 | reallocarray(void *optr, size_t nmemb, size_t size) |
32 | 36 | { |
33 | | - if ((nmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) && |
34 | | - nmemb > 0 && SIZE_MAX / nmemb < size) { |
| 37 | + if (size && nmemb > -1 / size) { |
35 | 38 | errno = ENOMEM; |
36 | 39 | return NULL; |
37 | 40 | } |
38 | 41 |
|
39 | 42 | return realloc(optr, size * nmemb); |
40 | 43 | } |
| 44 | + |
| 45 | +#endif /* !HAVE_REALLOCARRAY */ |
0 commit comments