diff options
| author | Vicent Marti <tanoku@gmail.com> | 2013-12-13 12:25:48 +0100 |
|---|---|---|
| committer | Vicent Marti <tanoku@gmail.com> | 2013-12-13 12:25:48 +0100 |
| commit | ce33645ff32d68dfd89867468f58fd9c245c26ff (patch) | |
| tree | 139ba94664d1fc1ba38d5d0d3b34b02d316532cd /src | |
| parent | 452c7de668568f75a97b0438daab9f33b68d605a (diff) | |
| download | libgit2-ce33645ff32d68dfd89867468f58fd9c245c26ff.tar.gz | |
pool: Cleanup error handling in pool_strdup
Note that `git_pool_strdup` cannot really return any error codes,
because the pool doesn't set errors on OOM.
The only place where `giterr_set_oom` is called is in
`git_pool_strndup`, in a conditional check that is always optimized
away. `n + 1` cannot be zero if `n` is unsigned because the compiler
doesn't take wraparound into account.
This check has been removed altogether because `size_t` is not
particularly going to overflow.
Diffstat (limited to 'src')
| -rw-r--r-- | src/pool.c | 19 |
1 files changed, 4 insertions, 15 deletions
diff --git a/src/pool.c b/src/pool.c index 4796d0a81..a23641145 100644 --- a/src/pool.c +++ b/src/pool.c @@ -190,19 +190,15 @@ void *git_pool_malloc(git_pool *pool, uint32_t items) char *git_pool_strndup(git_pool *pool, const char *str, size_t n) { - void *ptr = NULL; + char *ptr = NULL; assert(pool && str && pool->item_size == sizeof(char)); - if (n + 1 == 0) { - giterr_set_oom(); - return NULL; - } - if ((ptr = git_pool_malloc(pool, (uint32_t)(n + 1))) != NULL) { memcpy(ptr, str, n); - *(((char *)ptr) + n) = '\0'; + ptr[n] = '\0'; } + pool->has_string_alloc = 1; return ptr; @@ -217,14 +213,7 @@ char *git_pool_strdup(git_pool *pool, const char *str) char *git_pool_strdup_safe(git_pool *pool, const char *str) { - if (!str) - return NULL; - else { - char *result = git_pool_strdup(pool, str); - if (!result) - giterr_clear(); - return result; - } + return str ? git_pool_strdup(pool, str) : NULL; } char *git_pool_strcat(git_pool *pool, const char *a, const char *b) |
