oss-fuzz: Fix undefined-behavior

Analyzer run time does not like A -= B or A +=B even if A and B are uint32_t as there is potential
for overflow.

Analyzer run time does not like overflow when doing coap_tick_t - coap_tick_t.

Analyzer run time does not like (uint8_t << 24) as uint8_t is promoted as int, not u_int.

Analyzer run time does not like while(A--) {} even if A is uint32_t, as A is always decremented on
the test, even if the value of A is 0.

Fix potential overflow when analyzing an asn.1 tag.

Fix potential overflow when doing decode_segment() on a % hex value.
This commit is contained in:
Jon Shallow authored and Jon Shallow committed 2026-03-10 11:09:10 +00:00
1 parent fe41b51aab
commit 40502ffebb
9 files changed
+44 -37

No files matched your search

+1 -1
View File
@@ -72,7 +72,7 @@ asn1_tag_c(const uint8_t **ptr, size_t *plen, int *constructed, int *cls) {
tag = (tag << 7) + (byte & 0x7F);
(*plen)--;
(*ptr)++;
if (*plen == 0)
if (*plen == 0 || tag > (INT_MAX >> 7))
return COAP_ASN1_FAIL;
byte = (**ptr);
}
+1 -1
View File
@@ -3611,7 +3611,7 @@ derive_cbor_value(const uint8_t **bp, size_t rem_len) {
}
if (rem_len < 4)
return (uint32_t)-1;
value = **bp << 24;
value = (uint32_t)(**bp) << 24;
(*bp)++;
value |= **bp << 16;
(*bp)++;
+4 -2
View File
@@ -58,7 +58,8 @@ coap_encode_var_safe(uint8_t *buf, size_t length, unsigned int val) {
return 0;
}
i = n;
while (i--) {
while (i) {
i--;
buf[i] = val & 0xff;
val >>= 8;
}
@@ -92,7 +93,8 @@ coap_encode_var_safe8(uint8_t *buf, size_t length, uint64_t val) {
return 0;
}
i = n;
while (i--) {
while (i) {
i--;
buf[i] = val & 0xff;
val >>= 8;
}
+3 -3
View File
@@ -116,12 +116,12 @@ coap_free_node(coap_queue_t *node) {
unsigned int
coap_adjust_basetime(coap_context_t *ctx, coap_tick_t now) {
unsigned int result = 0;
coap_tick_diff_t delta = now - ctx->sendqueue_basetime;
coap_tick_diff_t delta = (coap_tick_diff_t)now - (coap_tick_diff_t)ctx->sendqueue_basetime;
if (ctx->sendqueue) {
/* delta < 0 means that the new time stamp is before the old. */
if (delta <= 0) {
ctx->sendqueue->t -= delta;
ctx->sendqueue->t = (coap_tick_diff_t)ctx->sendqueue->t - delta;
} else {
/* This case is more complex: The time must be advanced forward,
* thus possibly leading to timed out elements at the queue's
@@ -145,7 +145,7 @@ coap_adjust_basetime(coap_context_t *ctx, coap_tick_t now) {
}
/* adjust basetime */
ctx->sendqueue_basetime += delta;
ctx->sendqueue_basetime = (coap_tick_diff_t)ctx->sendqueue_basetime + delta;
return result;
}
+2 -1
View File
@@ -1452,7 +1452,8 @@ coap_pdu_parse_opt(coap_pdu_t *pdu, coap_opt_filter_t *error_opts) {
ok = ok && write_prefix(&obp, &outbuflen, " ", 1);
}
tlen = opt - opt_last;
while (tlen--) {
while (tlen) {
tlen--;
ok = ok && write_char(&obp, &outbuflen, *opt_last, i);
opt_last++;
}
+4 -1
View File
@@ -439,9 +439,12 @@ coap_host_is_unix_domain(const coap_str_const_t *host) {
static void
decode_segment(const uint8_t *seg, size_t length, unsigned char *buf) {
while (length--) {
while (length) {
length--;
if (*seg == '%') {
if (length < 2)
return;
*buf = (hexchar_to_dec(seg[1]) << 4) + hexchar_to_dec(seg[2]);
seg += 2;