This post has been de-listed
It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.
reallocarray(3) is a malloc(3)/realloc(3) extension from OpenBSD, it is very portable and easy to incorporate into existing codebases.
The intention of reallocarray to replace the following idiom:
if ((p = malloc(num * size)) == NULL)
err(1, "malloc");
..with the much safer:
if ((p = reallocarray(NULL, num, size)) == NULL)
err(1, "malloc");
In the first example, num * size may lead to an undetected integer multiplication overflow.
reallocarray(3) performs the same overflow detection that is conventionally done by calloc(3), but without the expensive memory zeroing operation. It returns NULL on overflow, with errno set to ENOMEM, as is permitted by standards.
It is now being used extensively by LibreSSL as within OpenBSD's own userland; and in the kernel, as mallocarray(9).
An ISC licensed reference implementation is available here.
Post Details
- Posted
- 10 years ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/Cprog/comme...