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

1.34    ! deraadt     1: /* $OpenBSD: xmalloc.c,v 1.33 2016/02/15 09:47:49 dtucker 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:
                     25: void
                     26: ssh_malloc_init(void)
                     27: {
                     28:        extern char *malloc_options;
                     29:
                     30:        malloc_options = "S";
                     31: }
1.1       deraadt    32:
1.4       markus     33: void *
                     34: xmalloc(size_t size)
1.1       deraadt    35: {
1.12      deraadt    36:        void *ptr;
                     37:
                     38:        if (size == 0)
                     39:                fatal("xmalloc: zero size");
                     40:        ptr = malloc(size);
1.4       markus     41:        if (ptr == NULL)
1.29      tedu       42:                fatal("xmalloc: out of memory (allocating %zu bytes)", size);
1.4       markus     43:        return ptr;
1.1       deraadt    44: }
                     45:
1.4       markus     46: void *
1.18      djm        47: xcalloc(size_t nmemb, size_t size)
                     48: {
                     49:        void *ptr;
                     50:
                     51:        if (size == 0 || nmemb == 0)
                     52:                fatal("xcalloc: zero size");
1.31      millert    53:        if (SIZE_MAX / nmemb < size)
                     54:                fatal("xcalloc: nmemb * size > SIZE_MAX");
1.18      djm        55:        ptr = calloc(nmemb, size);
                     56:        if (ptr == NULL)
1.29      tedu       57:                fatal("xcalloc: out of memory (allocating %zu bytes)",
                     58:                    size * nmemb);
1.18      djm        59:        return ptr;
                     60: }
                     61:
                     62: void *
1.32      deraadt    63: xreallocarray(void *ptr, size_t nmemb, size_t size)
1.1       deraadt    64: {
1.4       markus     65:        void *new_ptr;
1.1       deraadt    66:
1.32      deraadt    67:        new_ptr = reallocarray(ptr, nmemb, size);
1.4       markus     68:        if (new_ptr == NULL)
1.32      deraadt    69:                fatal("xreallocarray: out of memory (%zu elements of %zu bytes)",
1.34    ! deraadt    70:                    nmemb, size);
        !            71:        return new_ptr;
        !            72: }
        !            73:
        !            74: void *
        !            75: xrecallocarray(void *ptr, size_t onmemb, size_t nmemb, size_t size)
        !            76: {
        !            77:        void *new_ptr;
        !            78:
        !            79:        new_ptr = recallocarray(ptr, onmemb, nmemb, size);
        !            80:        if (new_ptr == NULL)
        !            81:                fatal("xrecallocarray: out of memory (%zu elements of %zu bytes)",
1.32      deraadt    82:                    nmemb, size);
1.4       markus     83:        return new_ptr;
1.1       deraadt    84: }
                     85:
1.4       markus     86: char *
                     87: xstrdup(const char *str)
1.1       deraadt    88: {
1.16      stevesk    89:        size_t len;
1.12      deraadt    90:        char *cp;
1.2       deraadt    91:
1.16      stevesk    92:        len = strlen(str) + 1;
1.12      deraadt    93:        cp = xmalloc(len);
1.4       markus     94:        strlcpy(cp, str, len);
                     95:        return cp;
1.18      djm        96: }
                     97:
                     98: int
                     99: xasprintf(char **ret, const char *fmt, ...)
                    100: {
                    101:        va_list ap;
                    102:        int i;
                    103:
                    104:        va_start(ap, fmt);
                    105:        i = vasprintf(ret, fmt, ap);
                    106:        va_end(ap);
                    107:
                    108:        if (i < 0 || *ret == NULL)
                    109:                fatal("xasprintf: could not allocate memory");
                    110:
                    111:        return (i);
1.1       deraadt   112: }