[BACK]Return to xmalloc.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / diff

Annotation of src/usr.bin/diff/xmalloc.c, Revision 1.7

1.7     ! nicm        1: /* $OpenBSD: xmalloc.c,v 1.6 2015/04/29 04:00:25 deraadt Exp $ */
1.1       ray         2: /*
                      3:  * Author: Tatu Ylonen <ylo@cs.hut.fi>
                      4:  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
                      5:  *                    All rights reserved
                      6:  * Versions of malloc and friends that check their results, and never return
                      7:  * failure (they call fatal if they encounter an error).
                      8:  *
                      9:  * As far as I am concerned, the code I have written for this software
                     10:  * can be used freely for any purpose.  Any derived versions of this
                     11:  * software must be clearly marked as such, and if the derived work is
                     12:  * incompatible with the protocol description in the RFC file, it must be
                     13:  * called by a name other than "ssh" or "Secure Shell".
                     14:  */
                     15:
                     16: #include <err.h>
                     17: #include <stdarg.h>
1.5       millert    18: #include <stdint.h>
1.1       ray        19: #include <stdio.h>
                     20: #include <stdlib.h>
                     21: #include <string.h>
                     22:
                     23: #include "xmalloc.h"
                     24:
                     25: void *
                     26: xmalloc(size_t size)
                     27: {
                     28:        void *ptr;
                     29:
                     30:        if (size == 0)
1.4       millert    31:                errx(2, "xmalloc: zero size");
1.1       ray        32:        ptr = malloc(size);
                     33:        if (ptr == NULL)
1.4       millert    34:                err(2, NULL);
1.1       ray        35:        return ptr;
                     36: }
                     37:
                     38: void *
1.2       ray        39: xcalloc(size_t nmemb, size_t size)
                     40: {
                     41:        void *ptr;
                     42:
                     43:        if (size == 0 || nmemb == 0)
1.3       ray        44:                errx(2, "xcalloc: zero size");
1.2       ray        45:        if (SIZE_MAX / nmemb < size)
1.3       ray        46:                errx(2, "xcalloc: nmemb * size > SIZE_MAX");
1.2       ray        47:        ptr = calloc(nmemb, size);
                     48:        if (ptr == NULL)
1.3       ray        49:                errx(2, "xcalloc: out of memory (allocating %lu bytes)",
1.2       ray        50:                    (u_long)(size * nmemb));
                     51:        return ptr;
                     52: }
                     53:
                     54: void *
1.6       deraadt    55: xreallocarray(void *ptr, size_t nmemb, size_t size)
1.1       ray        56: {
                     57:        void *new_ptr;
                     58:
1.6       deraadt    59:        new_ptr = reallocarray(ptr, nmemb, size);
1.1       ray        60:        if (new_ptr == NULL)
1.4       millert    61:                err(2, NULL);
1.1       ray        62:        return new_ptr;
                     63: }
                     64:
                     65: void
                     66: xfree(void *ptr)
                     67: {
                     68:        if (ptr == NULL)
1.4       millert    69:                err(2, NULL);
1.1       ray        70:        free(ptr);
                     71: }
                     72:
                     73: char *
                     74: xstrdup(const char *str)
                     75: {
                     76:        char *cp;
                     77:
1.7     ! nicm       78:        if ((cp = strdup(str)) == NULL)
        !            79:                err(1, "xstrdup");
1.1       ray        80:        return cp;
                     81: }
                     82:
                     83: int
                     84: xasprintf(char **ret, const char *fmt, ...)
                     85: {
                     86:        va_list ap;
                     87:        int i;
                     88:
                     89:        va_start(ap, fmt);
                     90:        i = vasprintf(ret, fmt, ap);
                     91:        va_end(ap);
                     92:
                     93:        if (i < 0 || *ret == NULL)
1.4       millert    94:                err(2, NULL);
1.1       ray        95:
                     96:        return (i);
                     97: }