Skip to content

Commit

Permalink
Add k5calloc internal helper function
Browse files Browse the repository at this point in the history
Letting calloc() do multiplication helps avoid overflow bugs, so
provide an internal k5calloc() helper which accepts both calloc
arguments, and reimplement k5alloc() in terms of it.
  • Loading branch information
greghudson committed Jul 11, 2013
1 parent f6cb089 commit 90f9f6f
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/include/k5-int.h
Original file line number Diff line number Diff line change
Expand Up @@ -2182,16 +2182,23 @@ authdata_eq(krb5_authdata a1, krb5_authdata a2)

/* Allocate zeroed memory; set *code to 0 on success or ENOMEM on failure. */
static inline void *
k5alloc(size_t len, krb5_error_code *code)
k5calloc(size_t nmemb, size_t size, krb5_error_code *code)
{
void *ptr;

/* Allocate at least one byte since zero-byte allocs may return NULL. */
ptr = calloc((len > 0) ? len : 1, 1);
ptr = calloc(nmemb ? nmemb : 1, size ? size : 1);
*code = (ptr == NULL) ? ENOMEM : 0;
return ptr;
}

/* Allocate zeroed memory; set *code to 0 on success or ENOMEM on failure. */
static inline void *
k5alloc(size_t size, krb5_error_code *code)
{
return k5calloc(1, size, code);
}

/* Return a copy of the len bytes of memory at in; set *code to 0 or ENOMEM. */
static inline void *
k5memdup(const void *in, size_t len, krb5_error_code *code)
Expand Down

0 comments on commit 90f9f6f

Please sign in to comment.