Skip to content

Commit d9395d8

Browse files
rlubosfabiobaltieri
authored andcommitted
net: lwm2m: json: Check for int64_t overflow when parsing integer
Verify if the integer value being parsed does not overflow int64_t type and report an error in such cases. Signed-off-by: Robert Lubos <[email protected]>
1 parent c1aa7f5 commit d9395d8

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

subsys/net/lib/lwm2m/lwm2m_rw_json.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -640,13 +640,14 @@ static int read_int(struct lwm2m_input_context *in, int64_t *value,
640640
bool accept_sign)
641641
{
642642
struct json_in_formatter_data *fd;
643+
uint64_t temp;
643644
uint8_t *buf;
644645
int i = 0;
645646
bool neg = false;
646647
char c;
647648

648649
/* initialize values to 0 */
649-
*value = 0;
650+
temp = 0;
650651

651652
fd = engine_get_in_user_data(in);
652653
if (!fd || (fd->object_bit_field & JSON_V_TYPE) == 0) {
@@ -663,17 +664,18 @@ static int read_int(struct lwm2m_input_context *in, int64_t *value,
663664
if (c == '-' && accept_sign && i == 0) {
664665
neg = true;
665666
} else if (isdigit(c) != 0) {
666-
*value = *value * 10 + (c - '0');
667+
temp = temp * 10ULL + (c - '0');
668+
if (temp > ((uint64_t)INT64_MAX + (neg ? 1ULL : 0ULL))) {
669+
return -EINVAL;
670+
}
667671
} else {
668672
/* anything else stop reading */
669673
break;
670674
}
671675
i++;
672676
}
673677

674-
if (neg) {
675-
*value = -*value;
676-
}
678+
*value = neg ? -temp : temp;
677679

678680
return i;
679681
}

0 commit comments

Comments
 (0)