Hash :
c47f7155
Author :
Date :
2018-03-14T10:34:59
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
/*
* Copyright (C) the libgit2 contributors. All rights reserved.
*
* This file is part of libgit2, distributed under the GNU GPL v2 with
* a Linking Exception. For full terms see the included COPYING file.
*/
#include "stdalloc.h"
void *git__stdalloc__malloc(size_t len)
{
void *ptr = malloc(len);
if (!ptr) giterr_set_oom();
return ptr;
}
void *git__stdalloc__calloc(size_t nelem, size_t elsize)
{
void *ptr = calloc(nelem, elsize);
if (!ptr) giterr_set_oom();
return ptr;
}
char *git__stdalloc__strdup(const char *str)
{
char *ptr = strdup(str);
if (!ptr) giterr_set_oom();
return ptr;
}
char *git__stdalloc__strndup(const char *str, size_t n)
{
size_t length = 0, alloclength;
char *ptr;
length = p_strnlen(str, n);
if (GIT_ADD_SIZET_OVERFLOW(&alloclength, length, 1) ||
!(ptr = git__stdalloc__malloc(alloclength)))
return NULL;
if (length)
memcpy(ptr, str, length);
ptr[length] = '\0';
return ptr;
}
char *git__stdalloc__substrdup(const char *start, size_t n)
{
char *ptr;
size_t alloclen;
if (GIT_ADD_SIZET_OVERFLOW(&alloclen, n, 1) ||
!(ptr = git__stdalloc__malloc(alloclen)))
return NULL;
memcpy(ptr, start, n);
ptr[n] = '\0';
return ptr;
}
void *git__stdalloc__realloc(void *ptr, size_t size)
{
void *new_ptr = realloc(ptr, size);
if (!new_ptr) giterr_set_oom();
return new_ptr;
}
void *git__stdalloc__reallocarray(void *ptr, size_t nelem, size_t elsize)
{
size_t newsize;
return GIT_MULTIPLY_SIZET_OVERFLOW(&newsize, nelem, elsize) ?
NULL : realloc(ptr, newsize);
}
void *git__stdalloc__mallocarray(size_t nelem, size_t elsize)
{
return git__stdalloc__reallocarray(NULL, nelem, elsize);
}
void git__stdalloc__free(void *ptr)
{
free(ptr);
}