Skip to content

Commit

Permalink
async.[ch]: Fix flexible array member 'token' warning
Browse files Browse the repository at this point in the history
Make token[] token[8] as the maximum size of a token is 8 bytes

Check that we do not overflow coap_async_state_t when copying in
the token.
  • Loading branch information
mrdeep1 committed May 22, 2018
1 parent 856236d commit a17757e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
2 changes: 1 addition & 1 deletion include/coap/async.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ typedef struct coap_async_state_t {
coap_tid_t id; /**< transaction id */
struct coap_async_state_t *next; /**< internally used for linking */
size_t tokenlen; /**< length of the token */
unsigned char token[]; /**< the token to use in a response */
uint8_t token[8]; /**< the token to use in a response */
} coap_async_state_t;

/* Definitions for Async Status Flags These flags can be used to control the
Expand Down
10 changes: 5 additions & 5 deletions src/async.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,13 @@ coap_register_async(coap_context_t *context, coap_session_t *session,
}

/* store information for handling the asynchronous task */
s = (coap_async_state_t *)coap_malloc(sizeof(coap_async_state_t) +
request->token_length);
s = (coap_async_state_t *)coap_malloc(sizeof(coap_async_state_t));
if (!s) {
coap_log(LOG_CRIT, "coap_register_async: insufficient memory\n");
return NULL;
}

memset(s, 0, sizeof(coap_async_state_t) + request->token_length);
memset(s, 0, sizeof(coap_async_state_t));

/* set COAP_ASYNC_CONFIRM according to request's type */
s->flags = flags & ~COAP_ASYNC_CONFIRM;
Expand All @@ -66,8 +65,9 @@ coap_register_async(coap_context_t *context, coap_session_t *session,
s->id = id;

if (request->token_length) {
s->tokenlen = request->token_length;
memcpy(s->token, request->token, request->token_length);
/* A token can be up to 8 bytes */
s->tokenlen = (request->token_length > 8) ? 8 : request->token_length;
memcpy(s->token, request->token, s->tokenlen);
}

coap_touch_async(s);
Expand Down

0 comments on commit a17757e

Please sign in to comment.