THE SOFTWARE.
*/
+#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
return addr;
}
+static bool
+stricttol(const char *p, unsigned long *l, char **q)
+{
+ if ((isdigit((int)(p[0])) == 0) || (p[0] == '0'))
+ return false;
+ errno = 0;
+ *l = strtoul(p, q, 10);
+ if (errno != 0) {
+ perror("strtoul()");
+ return false;
+ }
+ return true;
+}
+
static int
parse_message(
const unsigned char *buf,
if (tid_return) {
p = memmem(buf, buflen, "1:t", 3);
if (p) {
- l = strtoul((const char *)p + 3, &q, 10);
- if (q && *q == ':' && l > 0 && l < *tid_len) {
+ if (stricttol((const char *)p + 3, &l, &q) && (q && *q == ':') &&
+ (l > 0 && l < *tid_len)) {
CHECK(q + 1, l);
memcpy(tid_return, q + 1, l);
*tid_len = l;
if (port_return) {
p = memmem(buf, buflen, "porti", 5);
if (p) {
- l = strtoul((const char *)p + 5, &q, 10);
- if (q && *q == 'e' && l > 0 && l < 0x10000)
+ if (stricttol((const char *)p + 5, &l, &q) && (q && *q == 'e') &&
+ (l > 0 && l < 0x10000))
*port_return = (unsigned short)l;
else
*port_return = 0;
if (token_return) {
p = memmem(buf, buflen, "5:token", 7);
if (p) {
- l = strtoul((const char *)p + 7, &q, 10);
- if (q && *q == ':' && l > 0 && l < *token_len) {
+ if (stricttol((const char *)p + 7, &l, &q) && (q && *q == ':') &&
+ (l > 0 && l < *token_len)) {
CHECK(q + 1, l);
memcpy(token_return, q + 1, l);
*token_len = l;
if (nodes_len) {
p = memmem(buf, buflen, "5:nodes", 7);
if (p) {
- l = strtoul((const char *)p + 7, &q, 10);
- if (q && *q == ':' && l > 0 && l < *nodes_len) {
+ if (stricttol((const char *)p + 7, &l, &q) && (q && *q == ':') &&
+ (l > 0 && l < *nodes_len)) {
CHECK(q + 1, l);
memcpy(nodes_return, q + 1, l);
*nodes_len = l;
if (nodes6_len) {
p = memmem(buf, buflen, "6:nodes6", 8);
if (p) {
- l = strtoul((const char *)p + 8, &q, 10);
- if (q && *q == ':' && l > 0 && l < *nodes6_len) {
+ if (stricttol((const char *)p + 8, &l, &q) && (q && *q == ':') &&
+ (l > 0 && l < *nodes6_len)) {
CHECK(q + 1, l);
memcpy(nodes6_return, q + 1, l);
*nodes6_len = l;
size_t i = p - buf + 9;
unsigned long j = 0, j6 = 0;
for (;;) {
- l = strtoul((const char *)buf + i, &q, 10);
- if (q && *q == ':' && l > 0) {
+ if (stricttol((const char *)buf + i, &l, &q) && (q && *q == ':') &&
+ (l > 0)) {
CHECK(q + 1, l);
if (l == 6) {
if (j + l > *values_len)