net/mqtt: fix race condition in mqtt_init()

We need to set the mqtt context recv function prior to calling
net_context_recv which installs the mqtt_recv callback.
If not, we risk a race condition where an unprotected reference
to mqtt->rcv(mqtt, buf) is made in mqtt_recv().

Change-Id: If90ee58f4ea6f7879ef7c12b969ba27647426acc
Signed-off-by: Marti Bolivar <marti.bolivar@linaro.org>
This commit is contained in:
Marti Bolivar 2017-02-27 20:57:51 -08:00 committed by Jukka Rissanen
commit 5fab97c9d0

View file

@ -837,11 +837,6 @@ int mqtt_init(struct mqtt_ctx *ctx, enum mqtt_app app_type)
ctx->clean_session = 1; ctx->clean_session = 1;
ctx->connected = 0; ctx->connected = 0;
/* Install the receiver callback, timeout is set to K_NO_WAIT.
* In this case, no return code is evaluated.
*/
(void)net_context_recv(ctx->net_ctx, mqtt_recv, K_NO_WAIT, ctx);
ctx->app_type = app_type; ctx->app_type = app_type;
switch (ctx->app_type) { switch (ctx->app_type) {
@ -855,5 +850,10 @@ int mqtt_init(struct mqtt_ctx *ctx, enum mqtt_app app_type)
return -EINVAL; return -EINVAL;
} }
/* Install the receiver callback, timeout is set to K_NO_WAIT.
* In this case, no return code is evaluated.
*/
(void)net_context_recv(ctx->net_ctx, mqtt_recv, K_NO_WAIT, ctx);
return 0; return 0;
} }