Browse Source

Fix err(3) calls after allocation failures in examples.

There is long-standing consensus that err(1, NULL) is the best idiom
after failure of malloc(3) and friends.
Quirk in the manual noticed by tb@.
OPENBSD_5_9
schwarze 8 years ago
parent
commit
5595524148
1 changed files with 8 additions and 8 deletions
  1. +8
    -8
      src/lib/libc/stdlib/malloc.3

+ 8
- 8
src/lib/libc/stdlib/malloc.3 View File

@ -30,9 +30,9 @@
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE. .\" SUCH DAMAGE.
.\" .\"
.\" $OpenBSD: malloc.3,v 1.92 2016/01/06 17:57:22 tedu Exp $
.\" $OpenBSD: malloc.3,v 1.93 2016/02/05 15:09:09 schwarze Exp $
.\" .\"
.Dd $Mdocdate: January 6 2016 $
.Dd $Mdocdate: February 5 2016 $
.Dt MALLOC 3 .Dt MALLOC 3
.Os .Os
.Sh NAME .Sh NAME
@ -191,7 +191,7 @@ or
For example, avoid this common idiom as it may lead to integer overflow: For example, avoid this common idiom as it may lead to integer overflow:
.Bd -literal -offset indent .Bd -literal -offset indent
if ((p = malloc(num * size)) == NULL) if ((p = malloc(num * size)) == NULL)
err(1, "malloc");
err(1, NULL);
.Ed .Ed
.Pp .Pp
A drop-in replacement is the A drop-in replacement is the
@ -200,7 +200,7 @@ extension
.Fn reallocarray : .Fn reallocarray :
.Bd -literal -offset indent .Bd -literal -offset indent
if ((p = reallocarray(NULL, num, size)) == NULL) if ((p = reallocarray(NULL, num, size)) == NULL)
err(1, "reallocarray");
err(1, NULL);
.Ed .Ed
.Pp .Pp
Alternatively, Alternatively,
@ -295,7 +295,7 @@ if (size && num > SIZE_MAX / size)
errc(1, EOVERFLOW, "overflow"); errc(1, EOVERFLOW, "overflow");
if ((p = malloc(size * num)) == NULL) if ((p = malloc(size * num)) == NULL)
err(1, "malloc");
err(1, NULL);
.Ed .Ed
.Pp .Pp
The above test is not sufficient in all cases. The above test is not sufficient in all cases.
@ -313,7 +313,7 @@ if (size && num > INT_MAX / size)
errc(1, EOVERFLOW, "overflow"); errc(1, EOVERFLOW, "overflow");
if ((p = malloc(size * num)) == NULL) if ((p = malloc(size * num)) == NULL)
err(1, "malloc");
err(1, NULL);
.Ed .Ed
.Pp .Pp
Assuming the implementation checks for integer overflow as Assuming the implementation checks for integer overflow as
@ -326,13 +326,13 @@ or
The above examples could be simplified to: The above examples could be simplified to:
.Bd -literal -offset indent .Bd -literal -offset indent
if ((p = reallocarray(NULL, num, size)) == NULL) if ((p = reallocarray(NULL, num, size)) == NULL)
err(1, "reallocarray");
err(1, NULL);
.Ed .Ed
.Pp .Pp
or at the cost of initialization: or at the cost of initialization:
.Bd -literal -offset indent .Bd -literal -offset indent
if ((p = calloc(num, size)) == NULL) if ((p = calloc(num, size)) == NULL)
err(1, "calloc");
err(1, NULL);
.Ed .Ed
.Sh DIAGNOSTICS .Sh DIAGNOSTICS
If If


Loading…
Cancel
Save