Skip to content

Commit 04532b4

Browse files
committed
Fix reallocarray() on Win32
``` ../nip4-9.0.1-2/src/kplot/reallocarray.c:33:16: warning: shift count >= width of type [-Wshift-count-overflow] 33 | if ((nmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) && | ^~~~~~~~~~~~~~~ ../nip4-9.0.1-2/src/kplot/reallocarray.c:28:30: note: expanded from macro 'MUL_NO_OVERFLOW' 28 | #define MUL_NO_OVERFLOW (1UL << (sizeof(size_t) * 4)) | ^ ~~~~~~~~~~~~~~~~~~~~ ../nip4-9.0.1-2/src/kplot/reallocarray.c:33:43: warning: shift count >= width of type [-Wshift-count-overflow] 33 | if ((nmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) && | ^~~~~~~~~~~~~~~ ../nip4-9.0.1-2/src/kplot/reallocarray.c:28:30: note: expanded from macro 'MUL_NO_OVERFLOW' 28 | #define MUL_NO_OVERFLOW (1UL << (sizeof(size_t) * 4)) | ^ ~~~~~~~~~~~~~~~~~~~~ 2 warnings generated. ```
1 parent 499da60 commit 04532b4

File tree

2 files changed

+31
-30
lines changed

2 files changed

+31
-30
lines changed

meson.build

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,9 @@ else
7171
endif
7272
config_h.set('HAVE_BIND_TEXTDOMAIN_CODESET', have_bind_textdomain_codeset)
7373

74-
if cc.has_header('sys/resource.h')
75-
config_h.set('HAVE_SYS_RESOURCE_H', 1)
76-
endif
77-
78-
if cc.has_function('getrlimit')
79-
config_h.set('HAVE_GETRLIMIT', 1)
80-
endif
74+
config_h.set('HAVE_SYS_RESOURCE_H', cc.has_header('sys/resource.h'))
75+
config_h.set('HAVE_GETRLIMIT', cc.has_function('getrlimit'))
76+
config_h.set('HAVE_REALLOCARRAY', cc.has_function('reallocarray', prefix: '#include <stdlib.h>'))
8177

8278
configure_file(
8379
output: 'config.h',

src/kplot/reallocarray.c

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,45 @@
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 */
22
/*
3-
* Copyright (c) 2008 Otto Moerbeek <otto@drijf.net>
3+
* Copyright (c) 2005-2020 Rich Felker, et al.
44
*
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.
1623
*/
17-
18-
#include <sys/types.h>
24+
25+
#ifdef HAVE_CONFIG_H
26+
#include <config.h>
27+
#endif /*HAVE_CONFIG_H*/
1928

2029
#include <errno.h>
21-
#include <stdint.h>
2230
#include <stdlib.h>
2331

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
2933

3034
void *
3135
reallocarray(void *optr, size_t nmemb, size_t size)
3236
{
33-
if ((nmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
34-
nmemb > 0 && SIZE_MAX / nmemb < size) {
37+
if (size && nmemb > -1 / size) {
3538
errno = ENOMEM;
3639
return NULL;
3740
}
3841

3942
return realloc(optr, size * nmemb);
4043
}
44+
45+
#endif /* !HAVE_REALLOCARRAY */

0 commit comments

Comments
 (0)