xref: /freebsd-14.2/lib/libc/stdlib/reallocf.c (revision 4025b5b5)
19a5722f2SWarner Losh /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3d915a14eSPedro F. Giffuni  *
4f86e6000SWarner Losh  * Copyright (c) 1998 M. Warner Losh <[email protected]>
59a5722f2SWarner Losh  *
69a5722f2SWarner Losh  * Redistribution and use in source and binary forms, with or without
79a5722f2SWarner Losh  * modification, are permitted provided that the following conditions
89a5722f2SWarner Losh  * are met:
99a5722f2SWarner Losh  * 1. Redistributions of source code must retain the above copyright
109a5722f2SWarner Losh  *    notice, this list of conditions and the following disclaimer.
119a5722f2SWarner Losh  * 2. Redistributions in binary form must reproduce the above copyright
129a5722f2SWarner Losh  *    notice, this list of conditions and the following disclaimer in the
139a5722f2SWarner Losh  *    documentation and/or other materials provided with the distribution.
149a5722f2SWarner Losh  *
159a5722f2SWarner Losh  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
169a5722f2SWarner Losh  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
179a5722f2SWarner Losh  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
189a5722f2SWarner Losh  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
199a5722f2SWarner Losh  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
209a5722f2SWarner Losh  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
219a5722f2SWarner Losh  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
229a5722f2SWarner Losh  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
239a5722f2SWarner Losh  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
249a5722f2SWarner Losh  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
259a5722f2SWarner Losh  * SUCH DAMAGE.
269a5722f2SWarner Losh  */
27333fc21eSDavid E. O'Brien 
2894ad719cSWarner Losh #include <stdlib.h>
2994ad719cSWarner Losh 
3094ad719cSWarner Losh void *
reallocf(void * ptr,size_t size)3194ad719cSWarner Losh reallocf(void *ptr, size_t size)
3294ad719cSWarner Losh {
3394ad719cSWarner Losh 	void *nptr;
3494ad719cSWarner Losh 
3594ad719cSWarner Losh 	nptr = realloc(ptr, size);
36967e82caSJaakko Heinonen 
37967e82caSJaakko Heinonen 	/*
38967e82caSJaakko Heinonen 	 * When the System V compatibility option (malloc "V" flag) is
39967e82caSJaakko Heinonen 	 * in effect, realloc(ptr, 0) frees the memory and returns NULL.
40967e82caSJaakko Heinonen 	 * So, to avoid double free, call free() only when size != 0.
41967e82caSJaakko Heinonen 	 * realloc(ptr, 0) can't fail when ptr != NULL.
42967e82caSJaakko Heinonen 	 */
43967e82caSJaakko Heinonen 	if (!nptr && ptr && size != 0)
4494ad719cSWarner Losh 		free(ptr);
4594ad719cSWarner Losh 	return (nptr);
4694ad719cSWarner Losh }
47