MT#55283 switch to bounded str_to_*

Closes #2199

Change-Id: Id447e5f189b1bb1f9361fd6f63f420727731ce07
(cherry picked from commit 41d34290ff)
mr13.5
Richard Fuchs 1 month ago
parent 0cafa51704
commit 84a8d2beb4

@ -9,6 +9,7 @@
#include <ctype.h>
#include "helpers.h"
#include "str.h"
/* set to 0 for alloc debugging, e.g. through valgrind */
#define BENCODE_MIN_BUFFER_PIECE_LEN 4096
@ -517,7 +518,7 @@ static bencode_item_t *__bencode_decode_list(bencode_buffer_t *buf, const char *
static bencode_item_t *__bencode_decode_integer(bencode_buffer_t *buf, const char *s, const char *end) {
long long int i;
const char *orig = s;
char *convend;
const char *convend;
bencode_item_t *ret;
if (*s != 'i')
@ -530,21 +531,23 @@ static bencode_item_t *__bencode_decode_integer(bencode_buffer_t *buf, const cha
if (*s == '0') {
i = 0;
s++;
if (s >= end)
return NULL;
if (*s != 'e')
return NULL;
s++;
goto done;
}
i = strtoll(s, &convend, 10);
if (convend == s)
convend = memchr(s, 'e', end - s);
if (!convend || convend == s)
return NULL;
s += (convend - s);
done:
if (s >= end)
return NULL;
if (*s != 'e')
i = str_to_ll(&STR_LEN(s, convend - s), 0);
if (i == 0)
return NULL;
s++;
s = convend + 1;
done:
ret = __bencode_item_alloc(buf, 0);
if (!ret)
return NULL;
@ -562,28 +565,30 @@ done:
static bencode_item_t *__bencode_decode_string(bencode_buffer_t *buf, const char *s, const char *end) {
unsigned long int sl;
char *convend;
const char *convend;
const char *orig = s;
bencode_item_t *ret;
if (*s == '0') {
sl = 0;
s++;
goto colon;
if (s >= end)
return NULL;
if (*s != ':')
return NULL;
s++;
goto done;
}
sl = strtoul(s, &convend, 10);
if (convend == s)
return NULL;
s += (convend - s);
colon:
if (s >= end)
convend = memchr(s, ':', end - s);
if (!convend || convend == s)
return NULL;
if (*s != ':')
sl = str_to_ul(&STR_LEN(s, convend - s), 0);
if (sl == 0)
return NULL;
s++;
s = convend + 1;
done:
if (s + sl > end)
return NULL;
@ -688,15 +693,12 @@ bencode_item_t *bencode_dictionary_get_len(bencode_item_t *dict, const char *key
static ssize_t __bencode_string(const char *s, ssize_t offset, size_t len) {
size_t pos;
unsigned long sl;
char *end;
for (pos = offset; s[pos] != ':' && isdigit(s[pos]) && pos < len; ++pos);
if (pos == len)
return -1;
sl = strtoul(s + offset, &end, 10);
if (s + offset == end || end != s + pos)
return -2;
sl = str_to_ul(&STR_LEN(s + offset, pos - offset), 0);
if (pos + sl >= len)
return -1;

Loading…
Cancel
Save