Module Name: src
Committed By: kamil
Date: Tue Jul 28 17:13:34 UTC 2015
Modified Files:
src/lib/libc/stdlib: reallocarr.3 reallocarr.c
Log Message:
Compatibility fixes in reallocarr(3)
Make this work on !NetBSD platforms:
- replace __CTASSERT() with platform agnostic solution SQRT_SIZE_MAX
- include nbtool_config.h for cross builds to get definition of __RCSID()
- restore errno in the last rare code path for platforms affecting errno(2)
in memcpy(2)
While there: rename parameter name 'num' to 'number' to be in sync with
the calloc(3) parameter naming.
Reported by scole_mail at the current-users ml.
To generate a diff of this commit:
cvs rdiff -u -r1.3 -r1.4 src/lib/libc/stdlib/reallocarr.3
cvs rdiff -u -r1.2 -r1.3 src/lib/libc/stdlib/reallocarr.c
Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.
Modified files:
Index: src/lib/libc/stdlib/reallocarr.3
diff -u src/lib/libc/stdlib/reallocarr.3:1.3 src/lib/libc/stdlib/reallocarr.3:1.4
--- src/lib/libc/stdlib/reallocarr.3:1.3 Thu Feb 19 23:08:21 2015
+++ src/lib/libc/stdlib/reallocarr.3 Tue Jul 28 17:13:34 2015
@@ -1,4 +1,4 @@
-.\" $NetBSD: reallocarr.3,v 1.3 2015/02/19 23:08:21 wiz Exp $
+.\" $NetBSD: reallocarr.3,v 1.4 2015/07/28 17:13:34 kamil Exp $
.\"
.\" Copyright (c) 2015 The NetBSD Foundation, Inc.
.\" All rights reserved.
@@ -37,7 +37,7 @@
.Ft int
.Fo reallocarr
.Fa "void *ptr"
-.Fa "size_t num"
+.Fa "size_t number"
.Fa "size_t size"
.Fc
.Sh DESCRIPTION
Index: src/lib/libc/stdlib/reallocarr.c
diff -u src/lib/libc/stdlib/reallocarr.c:1.2 src/lib/libc/stdlib/reallocarr.c:1.3
--- src/lib/libc/stdlib/reallocarr.c:1.2 Thu Jul 16 00:03:59 2015
+++ src/lib/libc/stdlib/reallocarr.c Tue Jul 28 17:13:34 2015
@@ -1,4 +1,4 @@
-/* $NetBSD: reallocarr.c,v 1.2 2015/07/16 00:03:59 kamil Exp $ */
+/* $NetBSD: reallocarr.c,v 1.3 2015/07/28 17:13:34 kamil Exp $ */
/*-
* Copyright (c) 2015 Joerg Sonnenberger <[email protected]>.
@@ -29,8 +29,12 @@
* SUCH DAMAGE.
*/
+#if HAVE_NBTOOL_CONFIG_H
+#include "nbtool_config.h"
+#endif
+
#include <sys/cdefs.h>
-__RCSID("$NetBSD: reallocarr.c,v 1.2 2015/07/16 00:03:59 kamil Exp $");
+__RCSID("$NetBSD: reallocarr.c,v 1.3 2015/07/28 17:13:34 kamil Exp $");
#include "namespace.h"
#include <errno.h>
@@ -40,16 +44,17 @@ __RCSID("$NetBSD: reallocarr.c,v 1.2 201
#include <stdlib.h>
#include <string.h>
-__CTASSERT(65535 < SIZE_MAX / 65535);
-
#ifdef _LIBC
#ifdef __weak_alias
__weak_alias(reallocarr, _reallocarr)
#endif
#endif
+#define SQRT_SIZE_MAX (1UL << (sizeof(size_t) << 2))
+
+#if !HAVE_REALLOCARR
int
-reallocarr(void *ptr, size_t num, size_t size)
+reallocarr(void *ptr, size_t number, size_t size)
{
int saved_errno, result;
void *optr;
@@ -57,16 +62,21 @@ reallocarr(void *ptr, size_t num, size_t
saved_errno = errno;
memcpy(&optr, ptr, sizeof(ptr));
- if (num == 0 || size == 0) {
+ if (number == 0 || size == 0) {
free(optr);
nptr = NULL;
memcpy(ptr, &nptr, sizeof(ptr));
errno = saved_errno;
return 0;
}
- if ((num >= 65535 || size >= 65535) && num > SIZE_MAX / size)
+
+ if ((number >= SQRT_SIZE_MAX || size >= SQRT_SIZE_MAX) &&
+ number > SIZE_MAX / size) {
+ errno = saved_errno;
return EOVERFLOW;
- nptr = realloc(optr, num * size);
+ }
+
+ nptr = realloc(optr, number * size);
if (nptr == NULL) {
result = errno;
} else {
@@ -76,3 +86,4 @@ reallocarr(void *ptr, size_t num, size_t
errno = saved_errno;
return result;
}
+#endif