fixed bug #3524612 (Double free error when coap_send returns COAP_INVALID_TID)

From jsucaet's error description (Credits to Marc Haesen for revealing this bug):
The intent of the current code of coap_send is to release the allocated
memory upon success and not to release when an error has occured. However
this is broken (returning from coap_send with an error and a deallocated
pdu ptr is possible).
In the patch, I propose to let the calling function (of coap_send) be
responsible for free'ing the allocated pointer.
This commit is contained in:
Olaf Bergmann committed 2012-05-29 19:41:22 +02:00
1 parent 3d71f4ee64
commit 28a6d45cde
9 files changed
+53 -51

No files matched your search

+15 -6
View File
@@ -190,7 +190,8 @@ clear_obs(coap_context_t *ctx, const coap_address_t *remote) {
if (tid == COAP_INVALID_TID) {
debug("clear_obs: error sending new request");
coap_delete_pdu(pdu);
}
} else if (pdu->hdr->type != COAP_MESSAGE_CON)
coap_delete_pdu(pdu);
}
return tid;
}
@@ -387,9 +388,13 @@ message_handler(struct coap_context_t *ctx,
if (tid == COAP_INVALID_TID) {
debug("message_handler: error sending new request");
coap_delete_pdu(pdu);
} else
coap_delete_pdu(pdu);
} else {
set_timeout(&max_wait, wait_seconds);
if (received->hdr->type != COAP_MESSAGE_CON)
coap_delete_pdu(pdu);
}
return;
}
}
@@ -413,8 +418,8 @@ message_handler(struct coap_context_t *ctx,
/* finally send new request, if needed */
if (pdu && coap_send(ctx, remote, pdu) == COAP_INVALID_TID) {
debug("message_handler: error sending response");
coap_delete_pdu(pdu);
}
coap_delete_pdu(pdu);
/* our job is done, we can exit at any time */
ready = coap_check_option(received, COAP_OPTION_SUBSCRIPTION, &opt_iter) == NULL;
@@ -908,6 +913,7 @@ main(int argc, char **argv) {
int opt, res;
char *group = NULL;
coap_log_t log_level = LOG_WARN;
coap_tid_t tid = COAP_INVALID_TID;
while ((opt = getopt(argc, argv, "Nb:e:f:g:m:p:s:t:o:v:A:B:O:P:T:")) != -1) {
switch (opt) {
@@ -1065,9 +1071,12 @@ main(int argc, char **argv) {
#endif
if (pdu->hdr->type == COAP_MESSAGE_CON)
coap_send_confirmed(ctx, &dst, pdu);
tid = coap_send_confirmed(ctx, &dst, pdu);
else
coap_send(ctx, &dst, pdu);
tid = coap_send(ctx, &dst, pdu);
if (pdu->hdr->type != COAP_MESSAGE_CON || tid == COAP_INVALID_TID)
coap_delete_pdu(pdu);
set_timeout(&max_wait, wait_seconds);
debug("timeout is set to %d seconds\n", wait_seconds);