[BACK]Return to xmalloc.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / crypto / external / bsd / openssh / dist

Annotation of src/crypto/external/bsd/openssh/dist/xmalloc.c, Revision 1.13

1.11      christos    1: /*     $NetBSD: xmalloc.c,v 1.10 2017/10/07 19:39:19 christos Exp $    */
1.13    ! christos    2: /* $OpenBSD: xmalloc.c,v 1.37 2022/03/13 23:27:54 cheloha Exp $ */
1.1       christos    3: /*
                      4:  * Author: Tatu Ylonen <ylo@cs.hut.fi>
                      5:  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
                      6:  *                    All rights reserved
                      7:  * Versions of malloc and friends that check their results, and never return
                      8:  * failure (they call fatal if they encounter an error).
                      9:  *
                     10:  * As far as I am concerned, the code I have written for this software
                     11:  * can be used freely for any purpose.  Any derived versions of this
                     12:  * software must be clearly marked as such, and if the derived work is
                     13:  * incompatible with the protocol description in the RFC file, it must be
                     14:  * called by a name other than "ssh" or "Secure Shell".
                     15:  */
                     16:
1.2       christos   17: #include "includes.h"
1.11      christos   18: __RCSID("$NetBSD: xmalloc.c,v 1.10 2017/10/07 19:39:19 christos Exp $");
1.1       christos   19: #include <sys/param.h>
                     20: #include <stdarg.h>
1.5       christos   21: #include <stdint.h>
1.1       christos   22: #include <stdio.h>
                     23: #include <stdlib.h>
                     24: #include <string.h>
                     25:
                     26: #include "xmalloc.h"
                     27: #include "log.h"
                     28:
1.7       christos   29: #ifndef __NetBSD__
1.11      christos   30: char *malloc_options = "S";
1.7       christos   31: #endif
                     32:
1.1       christos   33: void *
                     34: xmalloc(size_t size)
                     35: {
                     36:        void *ptr;
                     37:
                     38:        if (size == 0)
                     39:                fatal("xmalloc: zero size");
                     40:        ptr = malloc(size);
                     41:        if (ptr == NULL)
1.4       christos   42:                fatal("xmalloc: out of memory (allocating %zu bytes)", size);
1.1       christos   43:        return ptr;
                     44: }
                     45:
                     46: void *
                     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.5       christos   53:        if (SIZE_MAX / nmemb < size)
                     54:                fatal("xcalloc: nmemb * size > SIZE_MAX");
1.1       christos   55:        ptr = calloc(nmemb, size);
                     56:        if (ptr == NULL)
1.4       christos   57:                fatal("xcalloc: out of memory (allocating %zu bytes)",
                     58:                    size * nmemb);
1.1       christos   59:        return ptr;
                     60: }
                     61:
                     62: void *
1.6       christos   63: xreallocarray(void *ptr, size_t nmemb, size_t size)
1.1       christos   64: {
                     65:        void *new_ptr;
                     66:
1.6       christos   67:        new_ptr = reallocarray(ptr, nmemb, size);
1.1       christos   68:        if (new_ptr == NULL)
1.6       christos   69:                fatal("xreallocarray: out of memory (%zu elements of %zu bytes)",
                     70:                    nmemb, size);
1.1       christos   71:        return new_ptr;
                     72: }
                     73:
1.10      christos   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)",
                     82:                    nmemb, size);
                     83:        return new_ptr;
                     84: }
                     85:
1.1       christos   86: char *
                     87: xstrdup(const char *str)
                     88: {
                     89:        size_t len;
                     90:        char *cp;
                     91:
                     92:        len = strlen(str) + 1;
                     93:        cp = xmalloc(len);
1.13    ! christos   94:        return memcpy(cp, str, len);
1.1       christos   95: }
                     96:
                     97: int
1.12      christos   98: xvasprintf(char **ret, const char *fmt, va_list ap)
                     99: {
                    100:        int i;
                    101:
                    102:        i = vasprintf(ret, fmt, ap);
                    103:        if (i < 0 || *ret == NULL)
                    104:                fatal("xvasprintf: could not allocate memory");
                    105:        return i;
                    106: }
                    107:
                    108: int
1.1       christos  109: xasprintf(char **ret, const char *fmt, ...)
                    110: {
                    111:        va_list ap;
                    112:        int i;
                    113:
                    114:        va_start(ap, fmt);
1.12      christos  115:        i = xvasprintf(ret, fmt, ap);
1.1       christos  116:        va_end(ap);
1.12      christos  117:        return i;
1.1       christos  118: }

CVSweb <webmaster@jp.NetBSD.org>