Home | History | Annotate | Line # | Download | only in stdlib
      1  1.12       wiz /*	$NetBSD: reallocarray.c,v 1.12 2022/10/28 09:43:59 wiz Exp $	*/
      2   1.1  christos /*	$OpenBSD: reallocarray.c,v 1.1 2014/05/08 21:43:49 deraadt Exp $	*/
      3   1.3  christos 
      4   1.3  christos /*-
      5   1.3  christos  * Copyright (c) 2015 The NetBSD Foundation, Inc.
      6   1.3  christos  * All rights reserved.
      7   1.3  christos  *
      8   1.3  christos  * This code is derived from software contributed to The NetBSD Foundation
      9   1.3  christos  * by Christos Zoulas.
     10   1.1  christos  *
     11   1.3  christos  * Redistribution and use in source and binary forms, with or without
     12   1.3  christos  * modification, are permitted provided that the following conditions
     13   1.3  christos  * are met:
     14   1.3  christos  * 1. Redistributions of source code must retain the above copyright
     15   1.3  christos  *    notice, this list of conditions and the following disclaimer.
     16   1.3  christos  * 2. Redistributions in binary form must reproduce the above copyright
     17   1.3  christos  *    notice, this list of conditions and the following disclaimer in the
     18   1.3  christos  *    documentation and/or other materials provided with the distribution.
     19   1.1  christos  *
     20   1.3  christos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21   1.3  christos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22   1.3  christos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23   1.3  christos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24   1.3  christos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25   1.3  christos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26   1.3  christos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27   1.3  christos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28   1.3  christos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29   1.3  christos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30   1.3  christos  * POSSIBILITY OF SUCH DAMAGE.
     31   1.1  christos  */
     32   1.5     kamil 
     33  1.11  christos #ifdef HAVE_NBTOOL_CONFIG_H
     34  1.11  christos #include "nbtool_config.h"
     35  1.11  christos #endif /* HAVE_NBTOOL_CONFIG_H */
     36  1.11  christos 
     37   1.1  christos #include <sys/cdefs.h>
     38  1.12       wiz __RCSID("$NetBSD: reallocarray.c,v 1.12 2022/10/28 09:43:59 wiz Exp $");
     39  1.10     kamil 
     40  1.10     kamil #include "namespace.h"
     41   1.1  christos 
     42   1.3  christos #define _OPENBSD_SOURCE
     43   1.1  christos #include <errno.h>
     44   1.1  christos #include <stdlib.h>
     45   1.1  christos 
     46   1.1  christos void *
     47   1.1  christos reallocarray(void *optr, size_t nmemb, size_t size)
     48   1.1  christos {
     49   1.7       roy 	int e;
     50   1.4  christos 
     51   1.7       roy 	if (nmemb == 0 || size == 0)
     52   1.7       roy 		return realloc(optr, 0);
     53   1.7       roy 
     54   1.7       roy 	e = reallocarr(&optr, nmemb, size);
     55   1.7       roy 	if (e == 0)
     56   1.7       roy 		return optr;
     57  1.12       wiz 	if (e == EOVERFLOW)
     58  1.12       wiz 		errno = ENOMEM;
     59  1.12       wiz 	else
     60  1.12       wiz 		errno = e;
     61  1.12       wiz 
     62   1.7       roy 	return NULL;
     63   1.1  christos }
     64