[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.2

1.2     ! ray         1: /* $OpenBSD: xmalloc.c,v 1.1 2007/05/29 18:24:56 ray 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 <limits.h>
                     18: #include <stdarg.h>
                     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)
                     31:                errx(2, NULL);
                     32:        ptr = malloc(size);
                     33:        if (ptr == NULL)
                     34:                errx(2, NULL);
                     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)
        !            44:                errx(1, "xcalloc: zero size");
        !            45:        if (SIZE_MAX / nmemb < size)
        !            46:                errx(1, "xcalloc: nmemb * size > SIZE_MAX");
        !            47:        ptr = calloc(nmemb, size);
        !            48:        if (ptr == NULL)
        !            49:                errx(1, "xcalloc: out of memory (allocating %lu bytes)",
        !            50:                    (u_long)(size * nmemb));
        !            51:        return ptr;
        !            52: }
        !            53:
        !            54: void *
1.1       ray        55: xrealloc(void *ptr, size_t nmemb, size_t size)
                     56: {
                     57:        void *new_ptr;
                     58:        size_t new_size = nmemb * size;
                     59:
                     60:        if (new_size == 0)
                     61:                errx(2, NULL);
1.2     ! ray        62:        if (SIZE_MAX / nmemb < size)
1.1       ray        63:                errx(2, NULL);
                     64:        if (ptr == NULL)
                     65:                new_ptr = malloc(new_size);
                     66:        else
                     67:                new_ptr = realloc(ptr, new_size);
                     68:        if (new_ptr == NULL)
                     69:                errx(2, NULL);
                     70:        return new_ptr;
                     71: }
                     72:
                     73: void
                     74: xfree(void *ptr)
                     75: {
                     76:        if (ptr == NULL)
                     77:                errx(2, NULL);
                     78:        free(ptr);
                     79: }
                     80:
                     81: char *
                     82: xstrdup(const char *str)
                     83: {
                     84:        size_t len;
                     85:        char *cp;
                     86:
                     87:        len = strlen(str) + 1;
                     88:        cp = xmalloc(len);
                     89:        strlcpy(cp, str, len);
                     90:        return cp;
                     91: }
                     92:
                     93: int
                     94: xasprintf(char **ret, const char *fmt, ...)
                     95: {
                     96:        va_list ap;
                     97:        int i;
                     98:
                     99:        va_start(ap, fmt);
                    100:        i = vasprintf(ret, fmt, ap);
                    101:        va_end(ap);
                    102:
                    103:        if (i < 0 || *ret == NULL)
                    104:                errx(2, NULL);
                    105:
                    106:        return (i);
                    107: }