2011-02-17 16:10:53 +08:00
|
|
|
#include <jansson.h>
|
2019-10-17 14:08:51 +08:00
|
|
|
#include <string.h>
|
2011-02-17 16:10:53 +08:00
|
|
|
|
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
static int malloc_called = 0;
|
|
|
|
static int free_called = 0;
|
2016-08-11 22:18:46 +08:00
|
|
|
static size_t malloc_used = 0;
|
2011-02-17 16:10:53 +08:00
|
|
|
|
2016-08-11 22:18:46 +08:00
|
|
|
/* helpers */
|
2019-10-17 14:08:51 +08:00
|
|
|
static void create_and_free_complex_object() {
|
2011-02-17 16:10:53 +08:00
|
|
|
json_t *obj;
|
|
|
|
|
2019-10-20 01:35:28 +08:00
|
|
|
obj = json_pack("{s:i,s:n,s:b,s:b,s:{s:s},s:[i,i,i]}", "foo", 42, "bar", "baz", 1,
|
|
|
|
"qux", 0, "alice", "bar", "baz", "bob", 9, 8, 7);
|
2011-02-17 16:10:53 +08:00
|
|
|
|
|
|
|
json_decref(obj);
|
|
|
|
}
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
static void create_and_free_object_with_oom() {
|
2016-08-11 23:51:29 +08:00
|
|
|
int i;
|
|
|
|
char key[4];
|
2016-08-11 22:18:46 +08:00
|
|
|
json_t *obj = json_object();
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
for (i = 0; i < 10; i++) {
|
2016-08-11 22:18:46 +08:00
|
|
|
snprintf(key, sizeof key, "%d", i);
|
|
|
|
json_object_set_new(obj, key, json_integer(i));
|
|
|
|
}
|
|
|
|
|
|
|
|
json_decref(obj);
|
|
|
|
}
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
static void *my_malloc(size_t size) {
|
2014-01-14 17:16:39 +08:00
|
|
|
malloc_called = 1;
|
2011-02-17 16:10:53 +08:00
|
|
|
return malloc(size);
|
|
|
|
}
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
static void my_free(void *ptr) {
|
2014-01-14 17:16:39 +08:00
|
|
|
free_called = 1;
|
2011-02-17 16:10:53 +08:00
|
|
|
free(ptr);
|
|
|
|
}
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
static void test_simple() {
|
2015-12-22 00:46:32 +08:00
|
|
|
json_malloc_t mfunc = NULL;
|
|
|
|
json_free_t ffunc = NULL;
|
|
|
|
|
2011-02-17 16:10:53 +08:00
|
|
|
json_set_alloc_funcs(my_malloc, my_free);
|
2015-12-22 00:46:32 +08:00
|
|
|
json_get_alloc_funcs(&mfunc, &ffunc);
|
2011-02-17 16:10:53 +08:00
|
|
|
create_and_free_complex_object();
|
|
|
|
|
2019-10-20 01:35:28 +08:00
|
|
|
if (malloc_called != 1 || free_called != 1 || mfunc != my_malloc || ffunc != my_free)
|
2011-02-17 16:10:53 +08:00
|
|
|
fail("Custom allocation failed");
|
|
|
|
}
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
static void *oom_malloc(size_t size) {
|
2016-08-11 22:18:46 +08:00
|
|
|
if (malloc_used + size > 800)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
malloc_used += size;
|
|
|
|
return malloc(size);
|
|
|
|
}
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
static void oom_free(void *ptr) {
|
2016-08-11 22:18:46 +08:00
|
|
|
free_called++;
|
|
|
|
free(ptr);
|
|
|
|
}
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
static void test_oom() {
|
2016-08-11 22:18:46 +08:00
|
|
|
free_called = 0;
|
|
|
|
json_set_alloc_funcs(oom_malloc, oom_free);
|
|
|
|
create_and_free_object_with_oom();
|
|
|
|
|
|
|
|
if (free_called == 0)
|
|
|
|
fail("Allocation with OOM failed");
|
|
|
|
}
|
|
|
|
|
2011-02-17 16:10:53 +08:00
|
|
|
/*
|
|
|
|
Test the secure memory functions code given in the API reference
|
|
|
|
documentation, but by using plain memset instead of
|
|
|
|
guaranteed_memset().
|
|
|
|
*/
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
static void *secure_malloc(size_t size) {
|
2011-02-17 16:10:53 +08:00
|
|
|
/* Store the memory area size in the beginning of the block */
|
|
|
|
void *ptr = malloc(size + 8);
|
|
|
|
*((size_t *)ptr) = size;
|
2012-06-04 02:17:29 +08:00
|
|
|
return (char *)ptr + 8;
|
2011-02-17 16:10:53 +08:00
|
|
|
}
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
static void secure_free(void *ptr) {
|
2011-02-17 16:10:53 +08:00
|
|
|
size_t size;
|
|
|
|
|
2012-06-04 02:17:29 +08:00
|
|
|
ptr = (char *)ptr - 8;
|
2011-02-17 16:10:53 +08:00
|
|
|
size = *((size_t *)ptr);
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
/*guaranteed_*/ memset(ptr, 0, size + 8);
|
2011-02-17 16:10:53 +08:00
|
|
|
free(ptr);
|
|
|
|
}
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
static void test_secure_funcs(void) {
|
2011-02-17 16:10:53 +08:00
|
|
|
json_set_alloc_funcs(secure_malloc, secure_free);
|
|
|
|
create_and_free_complex_object();
|
|
|
|
}
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
static void test_bad_args(void) {
|
2018-02-13 17:35:37 +08:00
|
|
|
/* The result of this test is not crashing. */
|
|
|
|
json_get_alloc_funcs(NULL, NULL);
|
|
|
|
}
|
|
|
|
|
2019-10-17 14:08:51 +08:00
|
|
|
static void run_tests() {
|
2011-02-17 16:10:53 +08:00
|
|
|
test_simple();
|
|
|
|
test_secure_funcs();
|
2016-08-11 22:18:46 +08:00
|
|
|
test_oom();
|
2018-02-13 17:35:37 +08:00
|
|
|
test_bad_args();
|
2011-02-17 16:10:53 +08:00
|
|
|
}
|