Skip to content

Commit

Permalink
jwt-memory: Last user of realloc is gone
Browse files Browse the repository at this point in the history
Signed-off-by: Ben Collins <[email protected]>
  • Loading branch information
benmcollins committed Feb 13, 2025
1 parent 2ea11b1 commit 70b92e3
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 42 deletions.
16 changes: 3 additions & 13 deletions include/jwt.h
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,6 @@ typedef struct jwk_item jwk_item_t;
*/
typedef void *(*jwt_malloc_t)(size_t);

/** @ingroup jwt_memory_grp
* @brief Prototype for realloc(3)
*/
typedef void *(*jwt_realloc_t)(void *, size_t);

/** @ingroup jwt_memory_grp
* @brief Prototype for free(3)
*/
Expand Down Expand Up @@ -1568,7 +1563,7 @@ size_t jwks_item_count(const jwk_set_t *jwk_set);
/**
* @brief Set functions to be used for memory management
*
* By default, LibJWT uses malloc, realloc, and free for memory
* By default, LibJWT uses malloc and free for memory
* management. This function allows the user of the library to
* specify its own memory management functions. This is especially
* useful on Windows where mismatches in runtimes across DLLs can
Expand All @@ -1583,26 +1578,21 @@ size_t jwks_item_count(const jwk_set_t *jwk_set);
*
* @param pmalloc The function to use for allocating memory or
* NULL to use malloc
* @param prealloc The function to use for reallocating memory or
* NULL to use realloc
* @param pfree The function to use for freeing memory or
* NULL to use free
* @returns 0 on success or errno otherwise.
*/
JWT_EXPORT
int jwt_set_alloc(jwt_malloc_t pmalloc, jwt_realloc_t prealloc,
jwt_free_t pfree);
int jwt_set_alloc(jwt_malloc_t pmalloc, jwt_free_t pfree);

/**
* Get functions used for allocating and freeing memory.
*
* @param pmalloc Pointer to malloc function output variable, or NULL
* @param prealloc Pointer to realloc function output variable, or NULL
* @param pfree Pointer to free function output variable, or NULL
*/
JWT_EXPORT
void jwt_get_alloc(jwt_malloc_t *pmalloc, jwt_realloc_t *prealloc,
jwt_free_t *pfree);
void jwt_get_alloc(jwt_malloc_t *pmalloc, jwt_free_t *pfree);

/**
* @}
Expand Down
18 changes: 2 additions & 16 deletions libjwt/jwt-memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#include "jwt-private.h"

static jwt_malloc_t pfn_malloc;
static jwt_realloc_t pfn_realloc;
static jwt_free_t pfn_free;

void *jwt_malloc(size_t size)
Expand All @@ -26,19 +25,10 @@ void *jwt_malloc(size_t size)
return malloc(size);
}

void *jwt_realloc(void *ptr, size_t size)
{
if (pfn_realloc)
return pfn_realloc(ptr, size);

return realloc(ptr, size);
}

int jwt_set_alloc(jwt_malloc_t pmalloc, jwt_realloc_t prealloc, jwt_free_t pfree)
int jwt_set_alloc(jwt_malloc_t pmalloc, jwt_free_t pfree)
{
/* Set allocator functions for LibJWT. */
pfn_malloc = pmalloc;
pfn_realloc = prealloc;
pfn_free = pfree;

/* Set same allocator functions for Jansson. */
Expand All @@ -47,15 +37,11 @@ int jwt_set_alloc(jwt_malloc_t pmalloc, jwt_realloc_t prealloc, jwt_free_t pfree
return 0;
}

void jwt_get_alloc(jwt_malloc_t *pmalloc, jwt_realloc_t *prealloc,
jwt_free_t *pfree)
void jwt_get_alloc(jwt_malloc_t *pmalloc, jwt_free_t *pfree)
{
if (pmalloc)
*pmalloc = pfn_malloc;

if (prealloc)
*prealloc = pfn_realloc;

if (pfree)
*pfree = pfn_free;
}
Expand Down
2 changes: 0 additions & 2 deletions libjwt/jwt-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,6 @@ JWT_NO_EXPORT
void *jwt_malloc(size_t size);
JWT_NO_EXPORT
void __jwt_freemem(void *ptr);
JWT_NO_EXPORT
void *jwt_realloc(void *ptr, size_t size);

JWT_NO_EXPORT
jwt_t *jwt_new(void);
Expand Down
14 changes: 3 additions & 11 deletions tests/jwt_flipflop.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,9 @@ static void test_free(void *ptr)
free(ptr);
}

static void *test_realloc(void *ptr, size_t size)
{
return realloc(ptr, size);
}

static int test_set_alloc(void)
{
return jwt_set_alloc(test_malloc, test_realloc, test_free);
return jwt_set_alloc(test_malloc, test_free);
}

#ifdef JWT_CONSTRUCTOR
Expand All @@ -42,23 +37,20 @@ END_TEST
START_TEST(test_alloc_funcs)
{
jwt_malloc_t m = NULL;
jwt_realloc_t r = NULL;
jwt_free_t f = NULL;
int ret;

SET_OPS();

jwt_get_alloc(&m, &r, &f);
jwt_get_alloc(&m, &f);
ck_assert_ptr_null(m);
ck_assert_ptr_null(r);
ck_assert_ptr_null(f);

ret = test_set_alloc();
ck_assert_int_eq(ret, 0);

jwt_get_alloc(&m, &r, &f);
jwt_get_alloc(&m, &f);
ck_assert(m == test_malloc);
ck_assert(r == test_realloc);
ck_assert(f == test_free);

/* XXX Need to do a build/verify to exercise the functions */
Expand Down

0 comments on commit 70b92e3

Please sign in to comment.