1*49e10846Suwe.\" $NetBSD: reallocarray.3,v 1.7 2022/12/04 11:25:08 uwe Exp $ 2dc83c154Skamil.\" 3dc83c154Skamil.\" Copyright (c) 2015 The NetBSD Foundation, Inc. 4dc83c154Skamil.\" All rights reserved. 5dc83c154Skamil.\" 6dc83c154Skamil.\" Redistribution and use in source and binary forms, with or without 7dc83c154Skamil.\" modification, are permitted provided that the following conditions 8dc83c154Skamil.\" are met: 9dc83c154Skamil.\" 10dc83c154Skamil.\" 1. Redistributions of source code must retain the above copyright 11dc83c154Skamil.\" notice, this list of conditions and the following disclaimer. 12dc83c154Skamil.\" 2. Redistributions in binary form must reproduce the above copyright 13dc83c154Skamil.\" notice, this list of conditions and the following disclaimer in 14dc83c154Skamil.\" the documentation and/or other materials provided with the 15dc83c154Skamil.\" distribution. 16dc83c154Skamil.\" 17dc83c154Skamil.\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18dc83c154Skamil.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19dc83c154Skamil.\" LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 20dc83c154Skamil.\" FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 21dc83c154Skamil.\" COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 22dc83c154Skamil.\" INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 23dc83c154Skamil.\" BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 24dc83c154Skamil.\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 25dc83c154Skamil.\" AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 26dc83c154Skamil.\" OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 27dc83c154Skamil.\" OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28dc83c154Skamil.\" SUCH DAMAGE. 297347edd3Schristos.\" 30dd8db245Swiz.Dd September 9, 2022 317347edd3Schristos.Dt REALLOCARRAY 3 327347edd3Schristos.Os 337347edd3Schristos.Sh NAME 347347edd3Schristos.Nm reallocarray 357347edd3Schristos.Nd reallocate memory for an array of elements checking for overflow 367347edd3Schristos.Sh SYNOPSIS 377347edd3Schristos.In stdlib.h 387347edd3Schristos.Ft void * 397347edd3Schristos.Fo reallocarray 407347edd3Schristos.Fa "void *ptr" 417347edd3Schristos.Fa "size_t nmemb" 427347edd3Schristos.Fa "size_t size" 437347edd3Schristos.Fc 447347edd3Schristos.Sh DESCRIPTION 457347edd3SchristosThe 467347edd3Schristos.Fn reallocarray 477347edd3Schristosfunction reallocates the pointer 487347edd3Schristos.Fa ptr 497347edd3Schristosto a size appropriate to handle an allocation of 507347edd3Schristos.Fa nmemb 517347edd3Schristoselements in an array where each of the array elements is 527347edd3Schristos.Fa size 537347edd3Schristosbytes using 547347edd3Schristos.Xr realloc 3 557347edd3Schristosand making sure that overflow does not happen in the multiplication of 567347edd3Schristos.Dq "nmemb * size" . 57dd8db245SwizOtherwise it behaves like 58dd8db245Swiz.Xr realloc 3 . 597347edd3Schristos.Sh RETURN VALUES 607347edd3SchristosThe 617347edd3Schristos.Fn reallocarray 627347edd3Schristosfunction will return 637347edd3Schristos.Dv NULL 647347edd3Schristosif there was overflow or if 657347edd3Schristos.Xr realloc 3 667347edd3Schristosfailed setting 677347edd3Schristos.Va errno 687347edd3Schristosto 69*49e10846Suwe.Er ENOMEM 707347edd3Schristosor preserving the value from 717347edd3Schristos.Xr realloc 3 . 727347edd3Schristos.Sh SEE ALSO 737347edd3Schristos.Xr malloc 3 , 747347edd3Schristos.Xr realloc 3 , 757347edd3Schristos.Xr reallocarr 3 76dd8db245Swiz.\" .Sh STANDARDS 77dd8db245Swiz.\" Will be part of POSIX, but isn't yet. 787347edd3Schristos.Sh HISTORY 797347edd3SchristosThe 807347edd3Schristos.Fn reallocarray 817347edd3Schristosfunction first appeared in 827347edd3Schristos.Ox 5.6 . 837347edd3Schristos.Fn reallocarray 847347edd3Schristoswas redesigned in 857347edd3Schristos.Nx 8 867347edd3Schristosas 877347edd3Schristos.Fn reallocarr 3 . 88dd8db245SwizUntil 89dd8db245Swiz.Nx 10 , 90dd8db245Swiz.Nm 91dd8db245Swizwas available in the 927347edd3Schristos.Vt _OPENBSD_SOURCE 937347edd3Schristosnamespace. 947347edd3Schristos.Sh CAVEATS 957347edd3SchristosThe 967347edd3Schristos.Fn reallocarray 977347edd3Schristosfunction was designed to facilitate safe, 987347edd3Schristosrobust programming and overcome the shortcomings of the 997347edd3Schristos.Xr malloc 3 1007347edd3Schristosand 1017347edd3Schristos.Xr realloc 3 1027347edd3Schristosfunctions by centralizing the overflow check in the multiplication of 1037347edd3Schristos.Fa nmemb 1047347edd3Schristosand 1057347edd3Schristos.Fa size . 1067347edd3Schristos.Pp 107d671eab6SgutteridgeThere are still portability issues. 108d671eab6Sgutteridge(It does not solve the 1097347edd3Schristos.Dv 0 1100a0e590dSchristossized allocation return ambiguity in the C standard: does 1117347edd3Schristos.Fn reallocarray 1127347edd3Schristosreturn 1137347edd3Schristos.Dv NULL 114d671eab6Sgutteridgeor a unique pointer to memory that cannot be accessed? 115d671eab6SgutteridgeDoes a 1167347edd3Schristos.Dv NULL 1177347edd3Schristosmean that an error occurred, and can someone check 1187347edd3Schristos.Dv errno 119d671eab6Sgutteridgein that case to find out what happened?) 1200a0e590dSchristosFor this reason 1217347edd3Schristos.Nx 1227347edd3Schristosdecided to go with an alternative implementation, and created 1237347edd3Schristos.Xr reallocarr 3 . 124