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

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