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

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.8       deraadt     7:  *
                      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.9     ! markus     16: RCSID("$OpenBSD: xmalloc.c,v 1.8 2000/09/07 20:27:55 deraadt 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.4       markus     24:        void *ptr = malloc(size);
                     25:        if (ptr == NULL)
                     26:                fatal("xmalloc: out of memory (allocating %d bytes)", (int) size);
                     27:        return ptr;
1.1       deraadt    28: }
                     29:
1.4       markus     30: void *
                     31: xrealloc(void *ptr, size_t new_size)
1.1       deraadt    32: {
1.4       markus     33:        void *new_ptr;
1.1       deraadt    34:
1.4       markus     35:        if (ptr == NULL)
                     36:                fatal("xrealloc: NULL pointer given as argument");
                     37:        new_ptr = realloc(ptr, new_size);
                     38:        if (new_ptr == NULL)
                     39:                fatal("xrealloc: out of memory (new_size %d bytes)", (int) new_size);
                     40:        return new_ptr;
1.1       deraadt    41: }
                     42:
1.6       markus     43: void
1.4       markus     44: xfree(void *ptr)
1.1       deraadt    45: {
1.4       markus     46:        if (ptr == NULL)
                     47:                fatal("xfree: NULL pointer given as argument");
                     48:        free(ptr);
1.1       deraadt    49: }
                     50:
1.4       markus     51: char *
                     52: xstrdup(const char *str)
1.1       deraadt    53: {
1.4       markus     54:        int len = strlen(str) + 1;
1.2       deraadt    55:
1.4       markus     56:        char *cp = xmalloc(len);
                     57:        strlcpy(cp, str, len);
                     58:        return cp;
1.1       deraadt    59: }