Skip to content
This repository has been archived by the owner on Aug 5, 2022. It is now read-only.

Commit

Permalink
[net] Use ZJS_ASSERT to affirm various invariant conditions
Browse files Browse the repository at this point in the history
Signed-off-by: Geoff Gustafson <geoff@linux.intel.com>
  • Loading branch information
grgustaf committed Sep 7, 2017
1 parent 368b803 commit d8689ac
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/zjs_net.c
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ static void close_server(server_handle_t *server_h)
static void server_free_cb(void *native)
{
server_handle_t *server_h = (server_handle_t *)native;
ZJS_ASSERT(server_h != &no_server, "attempt to free stub server");
net_context_put(server_h->server_ctx);
zjs_free(server_h);
}
Expand All @@ -240,6 +241,7 @@ static void release_close(void *handle, jerry_value_t argv[], u32_t argc)

server_handle_t *server_h = h->server_h;
u8_t removed = ZJS_LIST_REMOVE(sock_handle_t, server_h->connections, h);
ZJS_ASSERT(removed, "connection not found in list");

// check if this is a server connection socket
if (server_h != &no_server) {
Expand Down Expand Up @@ -318,6 +320,8 @@ static void handle_error_arg(void *unused, jerry_value_t argv[], u32_t *argc,
// effects: creates an error object with the corresponding text, clears
// the error flag, and sets this as the first arg; the error
// value must be released later (e.g. zjs_release_args)
ZJS_ASSERT(bytes == sizeof(error_desc_t), "invalid data received");

error_desc_t *desc = (error_desc_t *)buffer;
const char *message = error_messages[desc->error_id];

Expand All @@ -340,6 +344,8 @@ static void receive_packet(const void *buffer, u32_t length)
{
// effects: handle an incoming packet on the main thread that we first
// received on the RX thread
ZJS_ASSERT(length == sizeof(receive_packet_t), "invalid data received");

receive_packet_t *receive = (receive_packet_t *)buffer;
sock_handle_t *handle = receive->handle;
struct net_pkt *pkt = receive->pkt;
Expand All @@ -348,6 +354,8 @@ static void receive_packet(const void *buffer, u32_t length)
if (!handle) {
handle = find_connection(receive->server_h, receive->context);
}
ZJS_ASSERT(handle, "no handle found");
ZJS_ASSERT(handle, "no packet found");

if (handle && pkt) {
start_socket_timeout(handle);
Expand Down Expand Up @@ -401,12 +409,19 @@ static void clear_closed(const void *buffer, u32_t length)
// clean up and will no longer refer to the associated context
// this is particularly because the same context ID might get used
// again right away and we don't want to be confused thinking
ZJS_ASSERT(length == sizeof(clear_closed_t), "invalid data received");

clear_closed_t *clear = (clear_closed_t *)buffer;
ZJS_ASSERT(clear->server_h != &no_server, "called with client socket");
ZJS_ASSERT(clear->server_h->early_closed == clear->context,
"unexpected early closed socket");

DBG_PRINT("cleared early closed for server %p\n", clear->server_h);
clear->server_h->early_closed = NULL;

sock_handle_t *handle = find_connection(clear->server_h,
clear->context);
ZJS_ASSERT(handle, "handle not found");
if (handle) {
// clear context reference out of the handle so it no longer shows
// up as a match with find_connection
Expand Down Expand Up @@ -453,6 +468,8 @@ static void tcp_received(struct net_context *context,
release_close);
}
else {
ZJS_ASSERT(server_h != &no_server,
"client connections shouldn't get here");
if (server_h->early_closed) {
// this is bad because we only allocated space in the server
// handle to remember one early-closed socket; could be
Expand Down Expand Up @@ -748,6 +765,8 @@ typedef struct {
// a zjs_deferred_work callback
static void accept_connection(const void *buffer, u32_t length)
{
ZJS_ASSERT(length == sizeof(accept_connection_t), "invalid data received");

accept_connection_t *accept = (accept_connection_t *)buffer;

sock_handle_t *sock_handle = NULL;
Expand Down

0 comments on commit d8689ac

Please sign in to comment.