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

Annotation of src/usr.bin/ssh/xmalloc.c, Revision 1.16.20.1

1.16.20.1! brad        1: /* $OpenBSD: xmalloc.c,v 1.27 2006/08/03 03:34:42 deraadt Exp $ */
1.1       deraadt     2: /*
1.5       deraadt     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).
1.11      stevesk     8:  *
1.8       deraadt     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".
1.5       deraadt    14:  */
1.1       deraadt    15:
1.16.20.1! brad       16: #include <sys/param.h>
        !            17: #include <stdarg.h>
        !            18: #include <stdio.h>
        !            19: #include <stdlib.h>
        !            20: #include <string.h>
1.1       deraadt    21:
1.9       markus     22: #include "xmalloc.h"
                     23: #include "log.h"
1.1       deraadt    24:
1.4       markus     25: void *
                     26: xmalloc(size_t size)
1.1       deraadt    27: {
1.12      deraadt    28:        void *ptr;
                     29:
                     30:        if (size == 0)
                     31:                fatal("xmalloc: zero size");
                     32:        ptr = malloc(size);
1.4       markus     33:        if (ptr == NULL)
1.14      itojun     34:                fatal("xmalloc: out of memory (allocating %lu bytes)", (u_long) size);
1.4       markus     35:        return ptr;
1.1       deraadt    36: }
                     37:
1.4       markus     38: void *
1.16.20.1! brad       39: xcalloc(size_t nmemb, size_t size)
        !            40: {
        !            41:        void *ptr;
        !            42:
        !            43:        if (size == 0 || nmemb == 0)
        !            44:                fatal("xcalloc: zero size");
        !            45:        if (SIZE_T_MAX / nmemb < size)
        !            46:                fatal("xcalloc: nmemb * size > SIZE_T_MAX");
        !            47:        ptr = calloc(nmemb, size);
        !            48:        if (ptr == NULL)
        !            49:                fatal("xcalloc: out of memory (allocating %lu bytes)",
        !            50:                    (u_long)(size * nmemb));
        !            51:        return ptr;
        !            52: }
        !            53:
        !            54: void *
        !            55: xrealloc(void *ptr, size_t nmemb, size_t size)
1.1       deraadt    56: {
1.4       markus     57:        void *new_ptr;
1.16.20.1! brad       58:        size_t new_size = nmemb * size;
1.1       deraadt    59:
1.12      deraadt    60:        if (new_size == 0)
1.13      markus     61:                fatal("xrealloc: zero size");
1.16.20.1! brad       62:        if (SIZE_T_MAX / nmemb < size)
        !            63:                fatal("xrealloc: nmemb * size > SIZE_T_MAX");
1.4       markus     64:        if (ptr == NULL)
1.15      deraadt    65:                new_ptr = malloc(new_size);
                     66:        else
                     67:                new_ptr = realloc(ptr, new_size);
1.4       markus     68:        if (new_ptr == NULL)
1.16.20.1! brad       69:                fatal("xrealloc: out of memory (new_size %lu bytes)",
        !            70:                    (u_long) new_size);
1.4       markus     71:        return new_ptr;
1.1       deraadt    72: }
                     73:
1.6       markus     74: void
1.4       markus     75: xfree(void *ptr)
1.1       deraadt    76: {
1.4       markus     77:        if (ptr == NULL)
                     78:                fatal("xfree: NULL pointer given as argument");
                     79:        free(ptr);
1.1       deraadt    80: }
                     81:
1.4       markus     82: char *
                     83: xstrdup(const char *str)
1.1       deraadt    84: {
1.16      stevesk    85:        size_t len;
1.12      deraadt    86:        char *cp;
1.2       deraadt    87:
1.16      stevesk    88:        len = strlen(str) + 1;
1.12      deraadt    89:        cp = xmalloc(len);
1.4       markus     90:        strlcpy(cp, str, len);
                     91:        return cp;
1.16.20.1! brad       92: }
        !            93:
        !            94: int
        !            95: xasprintf(char **ret, const char *fmt, ...)
        !            96: {
        !            97:        va_list ap;
        !            98:        int i;
        !            99:
        !           100:        va_start(ap, fmt);
        !           101:        i = vasprintf(ret, fmt, ap);
        !           102:        va_end(ap);
        !           103:
        !           104:        if (i < 0 || *ret == NULL)
        !           105:                fatal("xasprintf: could not allocate memory");
        !           106:
        !           107:        return (i);
1.1       deraadt   108: }