xref: /netbsd-src/lib/libc/stdlib/reallocarray.c (revision dd8db24513425586e1b9e2b29dd9f6ffdf19bb31)
1*dd8db245Swiz /*	$NetBSD: reallocarray.c,v 1.12 2022/10/28 09:43:59 wiz Exp $	*/
2a582ce59Schristos /*	$OpenBSD: reallocarray.c,v 1.1 2014/05/08 21:43:49 deraadt Exp $	*/
384a0efdfSchristos 
484a0efdfSchristos /*-
584a0efdfSchristos  * Copyright (c) 2015 The NetBSD Foundation, Inc.
684a0efdfSchristos  * All rights reserved.
7a582ce59Schristos  *
884a0efdfSchristos  * This code is derived from software contributed to The NetBSD Foundation
984a0efdfSchristos  * by Christos Zoulas.
10a582ce59Schristos  *
1184a0efdfSchristos  * Redistribution and use in source and binary forms, with or without
1284a0efdfSchristos  * modification, are permitted provided that the following conditions
1384a0efdfSchristos  * are met:
1484a0efdfSchristos  * 1. Redistributions of source code must retain the above copyright
1584a0efdfSchristos  *    notice, this list of conditions and the following disclaimer.
1684a0efdfSchristos  * 2. Redistributions in binary form must reproduce the above copyright
1784a0efdfSchristos  *    notice, this list of conditions and the following disclaimer in the
1884a0efdfSchristos  *    documentation and/or other materials provided with the distribution.
1984a0efdfSchristos  *
2084a0efdfSchristos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2184a0efdfSchristos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2284a0efdfSchristos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2384a0efdfSchristos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2484a0efdfSchristos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2584a0efdfSchristos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2684a0efdfSchristos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2784a0efdfSchristos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2884a0efdfSchristos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2984a0efdfSchristos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3084a0efdfSchristos  * POSSIBILITY OF SUCH DAMAGE.
31a582ce59Schristos  */
32ec6de4dfSkamil 
3302b62279Schristos #ifdef HAVE_NBTOOL_CONFIG_H
3402b62279Schristos #include "nbtool_config.h"
3502b62279Schristos #endif /* HAVE_NBTOOL_CONFIG_H */
3602b62279Schristos 
37a582ce59Schristos #include <sys/cdefs.h>
38*dd8db245Swiz __RCSID("$NetBSD: reallocarray.c,v 1.12 2022/10/28 09:43:59 wiz Exp $");
39c5b83981Skamil 
40c5b83981Skamil #include "namespace.h"
41a582ce59Schristos 
4284a0efdfSchristos #define _OPENBSD_SOURCE
43a582ce59Schristos #include <errno.h>
44a582ce59Schristos #include <stdlib.h>
45a582ce59Schristos 
46a582ce59Schristos void *
reallocarray(void * optr,size_t nmemb,size_t size)47a582ce59Schristos reallocarray(void *optr, size_t nmemb, size_t size)
48a582ce59Schristos {
49cdda39d7Sroy 	int e;
50f7b1663bSchristos 
51cdda39d7Sroy 	if (nmemb == 0 || size == 0)
52cdda39d7Sroy 		return realloc(optr, 0);
53cdda39d7Sroy 
54cdda39d7Sroy 	e = reallocarr(&optr, nmemb, size);
55cdda39d7Sroy 	if (e == 0)
56cdda39d7Sroy 		return optr;
57*dd8db245Swiz 	if (e == EOVERFLOW)
58*dd8db245Swiz 		errno = ENOMEM;
59*dd8db245Swiz 	else
60cdda39d7Sroy 		errno = e;
61*dd8db245Swiz 
62a582ce59Schristos 	return NULL;
63a582ce59Schristos }
64