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

1.35    ! otto        1: /* $OpenBSD: xmalloc.c,v 1.34 2017/05/31 09:15: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.22      stevesk    16: #include <stdarg.h>
1.31      millert    17: #include <stdint.h>
1.26      stevesk    18: #include <stdio.h>
1.25      stevesk    19: #include <stdlib.h>
1.23      stevesk    20: #include <string.h>
1.1       deraadt    21:
1.9       markus     22: #include "xmalloc.h"
                     23: #include "log.h"
1.33      dtucker    24:
1.35    ! otto       25: char *malloc_options = "S";
1.1       deraadt    26:
1.4       markus     27: void *
                     28: xmalloc(size_t size)
1.1       deraadt    29: {
1.12      deraadt    30:        void *ptr;
                     31:
                     32:        if (size == 0)
                     33:                fatal("xmalloc: zero size");
                     34:        ptr = malloc(size);
1.4       markus     35:        if (ptr == NULL)
1.29      tedu       36:                fatal("xmalloc: out of memory (allocating %zu bytes)", size);
1.4       markus     37:        return ptr;
1.1       deraadt    38: }
                     39:
1.4       markus     40: void *
1.18      djm        41: xcalloc(size_t nmemb, size_t size)
                     42: {
                     43:        void *ptr;
                     44:
                     45:        if (size == 0 || nmemb == 0)
                     46:                fatal("xcalloc: zero size");
1.31      millert    47:        if (SIZE_MAX / nmemb < size)
                     48:                fatal("xcalloc: nmemb * size > SIZE_MAX");
1.18      djm        49:        ptr = calloc(nmemb, size);
                     50:        if (ptr == NULL)
1.29      tedu       51:                fatal("xcalloc: out of memory (allocating %zu bytes)",
                     52:                    size * nmemb);
1.18      djm        53:        return ptr;
                     54: }
                     55:
                     56: void *
1.32      deraadt    57: xreallocarray(void *ptr, size_t nmemb, size_t size)
1.1       deraadt    58: {
1.4       markus     59:        void *new_ptr;
1.1       deraadt    60:
1.32      deraadt    61:        new_ptr = reallocarray(ptr, nmemb, size);
1.4       markus     62:        if (new_ptr == NULL)
1.32      deraadt    63:                fatal("xreallocarray: out of memory (%zu elements of %zu bytes)",
1.34      deraadt    64:                    nmemb, size);
                     65:        return new_ptr;
                     66: }
                     67:
                     68: void *
                     69: xrecallocarray(void *ptr, size_t onmemb, size_t nmemb, size_t size)
                     70: {
                     71:        void *new_ptr;
                     72:
                     73:        new_ptr = recallocarray(ptr, onmemb, nmemb, size);
                     74:        if (new_ptr == NULL)
                     75:                fatal("xrecallocarray: out of memory (%zu elements of %zu bytes)",
1.32      deraadt    76:                    nmemb, size);
1.4       markus     77:        return new_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: }