Skip to content

Commit

Permalink
Merge pull request #20552 from maribu/backport/2024.01/lookup-plausib…
Browse files Browse the repository at this point in the history
…ility

cord_lc: Process truncated reads [backport 2024.01]
  • Loading branch information
maribu authored Apr 8, 2024
2 parents e354f87 + 41b21da commit e8f5882
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions sys/net/application_layer/cord/lc/cord_lc.c
Original file line number Diff line number Diff line change
Expand Up @@ -204,30 +204,39 @@ static void _on_rd_init(const gcoap_request_memo_t *memo, coap_pkt_t *pdu,
(void)remote;

thread_flags_t flag = FLAG_NORSC;
size_t full_buf_len = _result_buf_len;
_result_buf_len = 0;

if (memo->state == GCOAP_MEMO_RESP) {
unsigned ct = coap_get_content_type(pdu);
if (ct != COAP_FORMAT_LINK) {
DEBUG("cord_lc: error payload not in link format: %u\n", ct);
goto end;
}
if (pdu->payload_len == 0) {
size_t size = pdu->payload_len;

if (size == 0) {
DEBUG("cord_lc: error empty payload\n");
goto end;
}
memcpy(_result_buf, pdu->payload, pdu->payload_len);
_result_buf_len = pdu->payload_len;
_result_buf[_result_buf_len] = '\0';
if (size >= full_buf_len) {
DEBUG("cord_lc: truncating response from %" PRIuSIZE " to %" PRIuSIZE "\n", size, full_buf_len);

Check warning on line 223 in sys/net/application_layer/cord/lc/cord_lc.c

View workflow job for this annotation

GitHub Actions / static-tests

line is longer than 100 characters
/* Not setting FLAG_OVERFLOW: There can still be valid
* .well-known/core lookup data in the usable area, which will be
* used as long as endpoint and resource lookup are both found */
size = full_buf_len;
memcpy(_result_buf, pdu->payload, full_buf_len);
}
else {
memcpy(_result_buf, pdu->payload, size);
}
_result_buf_len = size;
flag = FLAG_SUCCESS;
} else if (memo->state == GCOAP_MEMO_TIMEOUT) {
flag = FLAG_TIMEOUT;
}

end:
if (flag != FLAG_SUCCESS) {
_result_buf = NULL;
_result_buf_len = 0;
}
thread_flags_set(_waiter, flag);
}

Expand Down Expand Up @@ -281,7 +290,9 @@ int cord_lc_rd_init(cord_lc_rd_t *rd, void *buf, size_t maxlen,
clif_attr_t attrs[MAX_EXPECTED_ATTRS];
unsigned attrs_used = 0;
size_t parsed_len = 0;
while ((!rd->res_lookif || !rd->ep_lookif) ||
/* Quitting the loop once everything we are interested in was found allows
* us to succeed even if the data was truncated by a too small buffer */
while ((!rd->res_lookif || !rd->ep_lookif) &&
(parsed_len != _result_buf_len)) {

ssize_t ret = clif_decode_link(&lookif, attrs + attrs_used,
Expand Down

0 comments on commit e8f5882

Please sign in to comment.