[LOG] remove ogs_expect_or_return()/return_val()

This commit is contained in:
Sukchan Lee
2023-01-24 00:01:36 +09:00
parent ed5cd4d513
commit c6fd4ae6b8
83 changed files with 2294 additions and 649 deletions

View File

@@ -191,10 +191,16 @@ char *ogs_uint64_to_string(uint64_t x)
char *str, *p, *dup;
str = ogs_uint64_to_0string(x);
ogs_expect_or_return_val(str, NULL);
if (!str) {
ogs_error("ogs_uint64_to_0string[%lld] failed", (long long)x);
return NULL;
}
p = ogs_left_trimcharacter(str, '0');
ogs_expect_or_return_val(p, NULL);
if (!p) {
ogs_error("ogs_left_trimcharacter[%s] failld", str);
return NULL;
}
dup = ogs_strdup(p);
ogs_free(str);

View File

@@ -112,7 +112,10 @@ static int epoll_add(ogs_poll_t *poll)
map = ogs_hash_get(context->map_hash, &poll->fd, sizeof(poll->fd));
if (!map) {
map = ogs_calloc(1, sizeof(*map));
ogs_expect_or_return_val(map, OGS_ERROR);
if (!map) {
ogs_error("ogs_calloc() failed");
return OGS_ERROR;
}
op = EPOLL_CTL_ADD;
ogs_hash_set(context->map_hash, &poll->fd, sizeof(poll->fd), map);

View File

@@ -72,7 +72,10 @@ ogs_hash_t *ogs_hash_make()
ogs_time_t now = ogs_get_monotonic_time();
ht = ogs_malloc(sizeof(ogs_hash_t));
ogs_expect_or_return_val(ht, NULL);
if (!ht) {
ogs_error("ogs_malloc() failed");
return NULL;
}
ht->free = NULL;
ht->count = 0;
@@ -88,7 +91,10 @@ ogs_hash_t *ogs_hash_make()
ogs_hash_t *ogs_hash_make_custom(ogs_hashfunc_t hash_func)
{
ogs_hash_t *ht = ogs_hash_make();
ogs_expect_or_return_val(ht, NULL);
if (!ht) {
ogs_error("ogs_hash_make() failed");
return NULL;
}
ht->hash_func = hash_func;
return ht;
}

View File

@@ -125,24 +125,6 @@ void ogs_log_hexdump_func(ogs_log_level_e level, int domain_id,
} \
} while (0)
#define ogs_expect_or_return(expr) \
do { \
if (ogs_likely(expr)) ; \
else { \
ogs_error("%s: Expectation `%s' failed.", OGS_FUNC, #expr); \
return; \
} \
} while (0)
#define ogs_expect_or_return_val(expr, val) \
do { \
if (ogs_likely(expr)) ; \
else { \
ogs_error("%s: Expectation `%s' failed.", OGS_FUNC, #expr); \
return (val); \
} \
} while (0)
#ifdef __cplusplus
}
#endif

View File

@@ -124,7 +124,11 @@ void *ogs_malloc_debug(size_t size, const char *file_line)
headroom = sizeof(ogs_pkbuf_t *);
pkbuf = ogs_pkbuf_alloc_debug(NULL, headroom + size, file_line);
ogs_expect_or_return_val(pkbuf, NULL);
if (!pkbuf) {
ogs_error("ogs_pkbuf_alloc_debug[headroom:%d, size:%d] failed",
(int)headroom, (int)size);
return NULL;
}
ogs_pkbuf_reserve(pkbuf, headroom);
memcpy(pkbuf->head, &pkbuf, headroom);
@@ -155,7 +159,11 @@ void *ogs_calloc_debug(size_t nmemb, size_t size, const char *file_line)
void *ptr = NULL;
ptr = ogs_malloc_debug(nmemb * size, file_line);
ogs_expect_or_return_val(ptr, NULL);
if (!ptr) {
ogs_error("ogs_malloc_debug[nmemb:%d, size:%d] failed",
(int)nmemb, (int)size);
return NULL;
}
memset(ptr, 0, nmemb * size);
return ptr;
@@ -174,10 +182,17 @@ void *ogs_realloc_debug(void *ptr, size_t size, const char *file_line)
memcpy(&pkbuf, (unsigned char*)ptr - headroom, headroom);
ogs_expect_or_return_val(pkbuf, NULL);
if (!pkbuf) {
ogs_error("Cannot get pkbuf from ptr[%p], headroom[%d]",
ptr, (int)headroom);
return NULL;
}
cluster = pkbuf->cluster;
ogs_expect_or_return_val(cluster, NULL);
if (!cluster) {
ogs_error("No cluster");
return NULL;
}
if (!size) {
ogs_pkbuf_free(pkbuf);
@@ -188,7 +203,10 @@ void *ogs_realloc_debug(void *ptr, size_t size, const char *file_line)
void *new = NULL;
new = ogs_malloc_debug(size, file_line);
ogs_expect_or_return_val(new, NULL);
if (!new) {
ogs_error("ogs_malloc_debug[%d] failed", (int)size);
return NULL;
}
memcpy(new, ptr, pkbuf->len);

View File

@@ -374,31 +374,52 @@ static ogs_cluster_t *cluster_alloc(
if (size <= OGS_CLUSTER_128_SIZE) {
ogs_pool_alloc(&pool->cluster_128, (ogs_cluster_128_t**)&buffer);
ogs_expect_or_return_val(buffer, NULL);
if (!buffer) {
ogs_error("ogs_pool_alloc() failed");
return NULL;
}
cluster->size = OGS_CLUSTER_128_SIZE;
} else if (size <= OGS_CLUSTER_256_SIZE) {
ogs_pool_alloc(&pool->cluster_256, (ogs_cluster_256_t**)&buffer);
ogs_expect_or_return_val(buffer, NULL);
if (!buffer) {
ogs_error("ogs_pool_alloc() failed");
return NULL;
}
cluster->size = OGS_CLUSTER_256_SIZE;
} else if (size <= OGS_CLUSTER_512_SIZE) {
ogs_pool_alloc(&pool->cluster_512, (ogs_cluster_512_t**)&buffer);
ogs_expect_or_return_val(buffer, NULL);
if (!buffer) {
ogs_error("ogs_pool_alloc() failed");
return NULL;
}
cluster->size = OGS_CLUSTER_512_SIZE;
} else if (size <= OGS_CLUSTER_1024_SIZE) {
ogs_pool_alloc(&pool->cluster_1024, (ogs_cluster_1024_t**)&buffer);
ogs_expect_or_return_val(buffer, NULL);
if (!buffer) {
ogs_error("ogs_pool_alloc() failed");
return NULL;
}
cluster->size = OGS_CLUSTER_1024_SIZE;
} else if (size <= OGS_CLUSTER_2048_SIZE) {
ogs_pool_alloc(&pool->cluster_2048, (ogs_cluster_2048_t**)&buffer);
ogs_expect_or_return_val(buffer, NULL);
if (!buffer) {
ogs_error("ogs_pool_alloc() failed");
return NULL;
}
cluster->size = OGS_CLUSTER_2048_SIZE;
} else if (size <= OGS_CLUSTER_16384_SIZE) {
ogs_pool_alloc(&pool->cluster_16384, (ogs_cluster_16384_t**)&buffer);
ogs_expect_or_return_val(buffer, NULL);
if (!buffer) {
ogs_error("ogs_pool_alloc() failed");
return NULL;
}
cluster->size = OGS_CLUSTER_16384_SIZE;
} else if (size <= OGS_CLUSTER_BIG_SIZE) {
ogs_pool_alloc(&pool->cluster_big, (ogs_cluster_big_t**)&buffer);
ogs_expect_or_return_val(buffer, NULL);
if (!buffer) {
ogs_error("ogs_pool_alloc() failed");
return NULL;
}
cluster->size = OGS_CLUSTER_BIG_SIZE;
} else {
ogs_fatal("invalid size = %d", size);

View File

@@ -35,7 +35,10 @@ bool ogs_pollset_actions_initialized = false;
ogs_pollset_t *ogs_pollset_create(unsigned int capacity)
{
ogs_pollset_t *pollset = ogs_calloc(1, sizeof *pollset);
ogs_expect_or_return_val(pollset, NULL);
if (!pollset) {
ogs_error("ogs_calloc() failed");
return NULL;
}
pollset->capacity = capacity;

View File

@@ -70,7 +70,10 @@ typedef struct ogs_queue_s {
ogs_queue_t *ogs_queue_create(unsigned int capacity)
{
ogs_queue_t *queue = ogs_calloc(1, sizeof *queue);
ogs_expect_or_return_val(queue, NULL);
if (!queue) {
ogs_error("ogs_calloc() failed");
return NULL;
}
ogs_assert(queue);
ogs_thread_mutex_init(&queue->one_big_mutex);
@@ -78,7 +81,11 @@ ogs_queue_t *ogs_queue_create(unsigned int capacity)
ogs_thread_cond_init(&queue->not_full);
queue->data = ogs_calloc(1, capacity * sizeof(void*));
ogs_expect_or_return_val(queue->data, NULL);
if (!queue->data) {
ogs_error("ogs_calloc[capacity:%d, sizeof(void*):%d] failed",
(int)capacity, (int)sizeof(void*));
return NULL;
}
queue->bounds = capacity;
queue->nelts = 0;
queue->in = 0;

View File

@@ -130,7 +130,10 @@ int ogs_addaddrinfo(ogs_sockaddr_t **sa_list,
continue;
new = ogs_calloc(1, sizeof(ogs_sockaddr_t));
ogs_expect_or_return_val(new, OGS_ERROR);
if (!new) {
ogs_error("ogs_calloc() failed");
return OGS_ERROR;
}
memcpy(&new->sa, ai->ai_addr, ai->ai_addrlen);
new->ogs_sin_port = htobe16(port);
@@ -204,15 +207,24 @@ int ogs_copyaddrinfo(ogs_sockaddr_t **dst, const ogs_sockaddr_t *src)
for (*dst = d = NULL, s = src; s; s = s->next) {
if (!d) {
*dst = d = ogs_memdup(s, sizeof *s);
ogs_expect_or_return_val(*dst, OGS_ERROR);
if (!(*dst)) {
ogs_error("ogs_memdup() failed");
return OGS_ERROR;
}
} else {
d = d->next = ogs_memdup(s, sizeof *s);
ogs_expect_or_return_val(d, OGS_ERROR);
if (!d) {
ogs_error("ogs_memdup() failed");
return OGS_ERROR;
}
}
if (s->hostname) {
if (s == src || s->hostname != src->hostname) {
d->hostname = ogs_strdup(s->hostname);
ogs_expect_or_return_val(d->hostname, OGS_ERROR);
if (!d->hostname) {
ogs_error("ogs_memdup() failed");
return OGS_ERROR;
}
} else {
d->hostname = (*dst)->hostname;
}
@@ -287,7 +299,10 @@ ogs_sockaddr_t *ogs_link_local_addr(const char *dev, const ogs_sockaddr_t *sa)
continue;
addr = ogs_calloc(1, sizeof(ogs_sockaddr_t));
ogs_expect_or_return_val(addr, NULL);
if (!addr) {
ogs_error("ogs_calloc() failed");
return NULL;
}
ogs_assert(addr);
memcpy(&addr->sa, cur->ifa_addr, ogs_sockaddr_len(cur->ifa_addr));

View File

@@ -55,7 +55,10 @@ ogs_sock_t *ogs_sock_create(void)
ogs_sock_t *sock = NULL;
sock = ogs_calloc(1, sizeof(*sock));
ogs_expect_or_return_val(sock, NULL);
if (!sock) {
ogs_error("ogs_calloc() failed");
return NULL;
}
sock->fd = INVALID_SOCKET;

View File

@@ -39,7 +39,10 @@ ogs_socknode_t *ogs_socknode_new(ogs_sockaddr_t *addr)
ogs_assert(addr);
node = ogs_calloc(1, sizeof(ogs_socknode_t));
ogs_expect_or_return_val(node, NULL);
if (!node) {
ogs_error("ogs_calloc() failed");
return NULL;
}
node->addr = addr;

View File

@@ -237,7 +237,10 @@ char *ogs_strdup_debug(const char *s, const char *file_line)
len = strlen(s) + 1;
res = ogs_memdup_debug(s, len, file_line);
ogs_expect_or_return_val(res, res);
if (!res) {
ogs_error("ogs_memdup_debug[len:%d] failed", (int)len);
return res;
}
return res;
}
@@ -254,7 +257,10 @@ char *ogs_strndup_debug(
if (end != NULL)
n = end - s;
res = ogs_malloc_debug(n + 1, file_line);
ogs_expect_or_return_val(res, res);
if (!res) {
ogs_error("ogs_malloc_debug[n:%d] failed", (int)n);
return res;
}
memcpy(res, s, n);
res[n] = '\0';
return res;
@@ -269,7 +275,10 @@ void *ogs_memdup_debug(
return NULL;
res = ogs_malloc_debug(n, file_line);
ogs_expect_or_return_val(res, res);
if (!res) {
ogs_error("ogs_malloc_debug[n:%d] failed", (int)n);
return res;
}
memcpy(res, m, n);
return res;
}

View File

@@ -77,7 +77,10 @@ static void *thread_worker(void *arg)
ogs_thread_t *ogs_thread_create(void (*func)(void *), void *data)
{
ogs_thread_t *thread = ogs_calloc(1, sizeof *thread);
ogs_expect_or_return_val(thread, NULL);
if (!thread) {
ogs_error("ogs_calloc() failed");
return NULL;
}
ogs_thread_mutex_init(&thread->mutex);
ogs_thread_cond_init(&thread->cond);

View File

@@ -55,7 +55,10 @@ static void add_timer_node(
ogs_timer_mgr_t *ogs_timer_mgr_create(unsigned int capacity)
{
ogs_timer_mgr_t *manager = ogs_calloc(1, sizeof *manager);
ogs_expect_or_return_val(manager, NULL);
if (!manager) {
ogs_error("ogs_calloc() failed");
return NULL;
}
ogs_pool_init(&manager->pool, capacity);
@@ -87,7 +90,6 @@ ogs_timer_t *ogs_timer_add(
ogs_fatal("ogs_pool_alloc() failed");
return NULL;
}
ogs_expect_or_return_val(timer, NULL);
memset(timer, 0, sizeof *timer);
timer->cb = cb;

View File

@@ -107,12 +107,21 @@ static ogs_tlv_t *tlv_add_leaf(
case OGS_TV_INT8:
{
ogs_tlv_uint8_t *v = (ogs_tlv_uint8_t *)msg;
if (parent_tlv)
if (parent_tlv) {
tlv = ogs_tlv_embed(parent_tlv, tlv_mode,
desc->type, 1, desc->instance, &v->u8);
else
tlv = ogs_tlv_add(tlv, tlv_mode, desc->type, 1, desc->instance, &v->u8);
ogs_expect_or_return_val(tlv, NULL);
if (!tlv) {
ogs_error("ogs_tlv_embed()");
return NULL;
}
} else {
tlv = ogs_tlv_add(tlv, tlv_mode,
desc->type, 1, desc->instance, &v->u8);
if (!tlv) {
ogs_error("ogs_tlv_add()");
return NULL;
}
}
break;
}
case OGS_TLV_UINT16:
@@ -124,12 +133,21 @@ static ogs_tlv_t *tlv_add_leaf(
v->u16 = htobe16(v->u16);
if (parent_tlv)
if (parent_tlv) {
tlv = ogs_tlv_embed(parent_tlv, tlv_mode,
desc->type, 2, desc->instance, &v->u16);
else
tlv = ogs_tlv_add(tlv, tlv_mode, desc->type, 2, desc->instance, &v->u16);
ogs_expect_or_return_val(tlv, NULL);
if (!tlv) {
ogs_error("ogs_tlv_embed()");
return NULL;
}
} else {
tlv = ogs_tlv_add(tlv, tlv_mode,
desc->type, 2, desc->instance, &v->u16);
if (!tlv) {
ogs_error("ogs_tlv_add()");
return NULL;
}
}
break;
}
case OGS_TLV_UINT24:
@@ -142,12 +160,21 @@ static ogs_tlv_t *tlv_add_leaf(
v->u24 = v->u24 << 8;
v->u24 = htobe32(v->u24);
if (parent_tlv)
if (parent_tlv) {
tlv = ogs_tlv_embed(parent_tlv, tlv_mode,
desc->type, 3, desc->instance, &v->u24);
else
tlv = ogs_tlv_add(tlv, tlv_mode, desc->type, 3, desc->instance, &v->u24);
ogs_expect_or_return_val(tlv, NULL);
if (!tlv) {
ogs_error("ogs_tlv_embed()");
return NULL;
}
} else {
tlv = ogs_tlv_add(tlv, tlv_mode,
desc->type, 3, desc->instance, &v->u24);
if (!tlv) {
ogs_error("ogs_tlv_add()");
return NULL;
}
}
break;
}
case OGS_TLV_UINT32:
@@ -159,13 +186,21 @@ static ogs_tlv_t *tlv_add_leaf(
v->u32 = htobe32(v->u32);
if (parent_tlv)
if (parent_tlv) {
tlv = ogs_tlv_embed(parent_tlv, tlv_mode,
desc->type, 4, desc->instance, &v->u32);
else
if (!tlv) {
ogs_error("ogs_tlv_embed()");
return NULL;
}
} else {
tlv = ogs_tlv_add(tlv, tlv_mode,
desc->type, 4, desc->instance, &v->u32);
ogs_expect_or_return_val(tlv, NULL);
if (!tlv) {
ogs_error("ogs_tlv_add()");
return NULL;
}
}
break;
}
case OGS_TLV_FIXED_STR:
@@ -173,13 +208,21 @@ static ogs_tlv_t *tlv_add_leaf(
{
ogs_tlv_octet_t *v = (ogs_tlv_octet_t *)msg;
if (parent_tlv)
if (parent_tlv) {
tlv = ogs_tlv_embed(parent_tlv, tlv_mode,
desc->type, desc->length, desc->instance, v->data);
else
if (!tlv) {
ogs_error("ogs_tlv_embed()");
return NULL;
}
} else {
tlv = ogs_tlv_add(tlv, tlv_mode,
desc->type, desc->length, desc->instance, v->data);
ogs_expect_or_return_val(tlv, NULL);
if (!tlv) {
ogs_error("ogs_tlv_add()");
return NULL;
}
}
break;
}
case OGS_TLV_VAR_STR:
@@ -189,33 +232,49 @@ static ogs_tlv_t *tlv_add_leaf(
if (v->len == 0) {
ogs_error("No TLV length - [%s] T:%d I:%d (vsz=%d)",
desc->name, desc->type, desc->instance, desc->vsize);
ogs_expect_or_return_val(0, NULL);
return NULL;
}
if (parent_tlv)
if (parent_tlv) {
tlv = ogs_tlv_embed(parent_tlv, tlv_mode,
desc->type, v->len, desc->instance, v->data);
else
if (!tlv) {
ogs_error("ogs_tlv_embed()");
return NULL;
}
} else {
tlv = ogs_tlv_add(tlv, tlv_mode,
desc->type, v->len, desc->instance, v->data);
ogs_expect_or_return_val(tlv, NULL);
if (!tlv) {
ogs_error("ogs_tlv_add()");
return NULL;
}
}
break;
}
case OGS_TLV_NULL:
case OGS_TV_NULL:
{
if (parent_tlv)
if (parent_tlv) {
tlv = ogs_tlv_embed(parent_tlv, tlv_mode,
desc->type, 0, desc->instance, NULL);
else
if (!tlv) {
ogs_error("ogs_tlv_embed()");
return NULL;
}
} else {
tlv = ogs_tlv_add(tlv, tlv_mode,
desc->type, 0, desc->instance, NULL);
ogs_expect_or_return_val(tlv, NULL);
if (!tlv) {
ogs_error("ogs_tlv_add()");
return NULL;
}
}
break;
}
default:
ogs_expect_or_return_val(0, NULL);
break;
ogs_error("Unknown type [%d]", desc->ctype);
return NULL;
}
return tlv;
@@ -258,16 +317,21 @@ static uint32_t tlv_add_compound(ogs_tlv_t **root, ogs_tlv_t *parent_tlv,
desc->vsize, p + offset2);
if (parent_tlv)
tlv = ogs_tlv_embed(parent_tlv, tlv_ctype2mode(desc->ctype, mode),
tlv = ogs_tlv_embed(parent_tlv,
tlv_ctype2mode(desc->ctype, mode),
desc->type, 0, desc->instance, NULL);
else
tlv = ogs_tlv_add(tlv, tlv_ctype2mode(desc->ctype, mode),
tlv = ogs_tlv_add(tlv,
tlv_ctype2mode(desc->ctype, mode),
desc->type, 0, desc->instance, NULL);
r = tlv_add_compound(&emb_tlv, tlv, desc,
p + offset2 + sizeof(ogs_tlv_presence_t),
depth + 1, mode);
ogs_expect_or_return_val(r > 0 && emb_tlv, 0);
if (r <= 0 || !emb_tlv) {
ogs_error("tlv_add_compound() failed");
return 0;
}
count += 1 + r;
} else {
ogs_trace("BUILD %sL#%d [%s] T:%d L:%d I:%d "
@@ -276,8 +340,12 @@ static uint32_t tlv_add_compound(ogs_tlv_t **root, ogs_tlv_t *parent_tlv,
desc->instance, desc->ctype, desc->vsize,
p + offset2);
tlv = tlv_add_leaf(parent_tlv, tlv, desc, p + offset2, mode);
ogs_expect_or_return_val(tlv, 0);
tlv = tlv_add_leaf(parent_tlv, tlv, desc,
p + offset2, mode);
if (!tlv) {
ogs_error("tlv_add_leaf() failed");
return 0;
}
count++;
}
@@ -298,16 +366,21 @@ static uint32_t tlv_add_compound(ogs_tlv_t **root, ogs_tlv_t *parent_tlv,
desc->vsize, p + offset);
if (parent_tlv)
tlv = ogs_tlv_embed(parent_tlv, tlv_ctype2mode(desc->ctype, mode),
tlv = ogs_tlv_embed(parent_tlv,
tlv_ctype2mode(desc->ctype, mode),
desc->type, 0, desc->instance, NULL);
else
tlv = ogs_tlv_add(tlv, tlv_ctype2mode(desc->ctype, mode),
tlv = ogs_tlv_add(tlv,
tlv_ctype2mode(desc->ctype, mode),
desc->type, 0, desc->instance, NULL);
r = tlv_add_compound(&emb_tlv, tlv, desc,
p + offset + sizeof(ogs_tlv_presence_t),
depth + 1, mode);
ogs_expect_or_return_val(r > 0 && emb_tlv, 0);
if (r <= 0 || !emb_tlv) {
ogs_error("tlv_add_compound() failed");
return 0;
}
count += 1 + r;
} else {
ogs_trace("BUILD %sL#%d [%s] T:%d L:%d I:%d "
@@ -317,7 +390,10 @@ static uint32_t tlv_add_compound(ogs_tlv_t **root, ogs_tlv_t *parent_tlv,
p + offset);
tlv = tlv_add_leaf(parent_tlv, tlv, desc, p + offset, mode);
ogs_expect_or_return_val(tlv, 0);
if (!tlv) {
ogs_error("tlv_add_leaf() failed");
return 0;
}
count++;
}
@@ -344,20 +420,30 @@ ogs_pkbuf_t *ogs_tlv_build_msg(ogs_tlv_desc_t *desc, void *msg, int mode)
if (desc->child_descs[0]) {
r = tlv_add_compound(&root, NULL, desc, msg, 0, mode);
ogs_expect_or_return_val(r > 0 && root, NULL);
if (r <= 0 || !root) {
ogs_error("tlv_add_compound() failed");
return NULL;
}
length = ogs_tlv_calc_length(root);
} else {
length = 0;
}
pkbuf = ogs_pkbuf_alloc(NULL, OGS_TLV_MAX_HEADROOM+length);
ogs_expect_or_return_val(pkbuf, NULL);
if (!pkbuf) {
ogs_error("ogs_pkbuf_alloc() failed");
return NULL;
}
ogs_pkbuf_reserve(pkbuf, OGS_TLV_MAX_HEADROOM);
ogs_pkbuf_put(pkbuf, length);
if (desc->child_descs[0]) {
rendlen = ogs_tlv_render(root, pkbuf->data, length);
ogs_expect_or_return_val(rendlen == length, NULL);
if (rendlen != length) {
ogs_error("ogs_tlv_render[rendlen:%d != length:%d] failed",
rendlen, length);
return NULL;
}
ogs_tlv_free_all(root);
}