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

1.1       deraadt     1: /*
1.5       deraadt     2:  * Author: Tatu Ylonen <ylo@cs.hut.fi>
                      3:  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
                      4:  *                    All rights reserved
                      5:  * Versions of malloc and friends that check their results, and never return
                      6:  * failure (they call fatal if they encounter an error).
1.11      stevesk     7:  *
1.8       deraadt     8:  * As far as I am concerned, the code I have written for this software
                      9:  * can be used freely for any purpose.  Any derived versions of this
                     10:  * software must be clearly marked as such, and if the derived work is
                     11:  * incompatible with the protocol description in the RFC file, it must be
                     12:  * called by a name other than "ssh" or "Secure Shell".
1.5       deraadt    13:  */
1.1       deraadt    14:
                     15: #include "includes.h"
1.12    ! deraadt    16: RCSID("$OpenBSD: xmalloc.c,v 1.11 2001/02/04 15:32:27 stevesk Exp $");
1.1       deraadt    17:
1.9       markus     18: #include "xmalloc.h"
                     19: #include "log.h"
1.1       deraadt    20:
1.4       markus     21: void *
                     22: xmalloc(size_t size)
1.1       deraadt    23: {
1.12    ! deraadt    24:        void *ptr;
        !            25:
        !            26:        if (size == 0)
        !            27:                fatal("xmalloc: zero size");
        !            28:        ptr = malloc(size);
1.4       markus     29:        if (ptr == NULL)
                     30:                fatal("xmalloc: out of memory (allocating %d bytes)", (int) size);
                     31:        return ptr;
1.1       deraadt    32: }
                     33:
1.4       markus     34: void *
                     35: xrealloc(void *ptr, size_t new_size)
1.1       deraadt    36: {
1.4       markus     37:        void *new_ptr;
1.1       deraadt    38:
1.12    ! deraadt    39:        if (new_size == 0)
        !            40:                fatal("xmalloc: zero size");
1.4       markus     41:        if (ptr == NULL)
                     42:                fatal("xrealloc: NULL pointer given as argument");
                     43:        new_ptr = realloc(ptr, new_size);
                     44:        if (new_ptr == NULL)
                     45:                fatal("xrealloc: out of memory (new_size %d bytes)", (int) new_size);
                     46:        return new_ptr;
1.1       deraadt    47: }
                     48:
1.6       markus     49: void
1.4       markus     50: xfree(void *ptr)
1.1       deraadt    51: {
1.4       markus     52:        if (ptr == NULL)
                     53:                fatal("xfree: NULL pointer given as argument");
                     54:        free(ptr);
1.1       deraadt    55: }
                     56:
1.4       markus     57: char *
                     58: xstrdup(const char *str)
1.1       deraadt    59: {
1.10      stevesk    60:        size_t len = strlen(str) + 1;
1.12    ! deraadt    61:        char *cp;
1.2       deraadt    62:
1.12    ! deraadt    63:        if (len == 0)
        !            64:                fatal("xmalloc: zero size");
        !            65:        cp = xmalloc(len);
1.4       markus     66:        strlcpy(cp, str, len);
                     67:        return cp;
1.1       deraadt    68: }