aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGarrett D'Amore <garrett@damore.org>2020-11-23 23:09:18 -0800
committerGarrett D'Amore <garrett@damore.org>2020-11-23 23:09:18 -0800
commit5ea2a1845f3393e91d6d102a8a89f339dd24f467 (patch)
treed17d6cd87d1a540e50f2e996924d8e1c92e1c8f0 /src
parentd1218d7309475193b53911667911c4f59a1a7752 (diff)
downloadnng-5ea2a1845f3393e91d6d102a8a89f339dd24f467.tar.gz
nng-5ea2a1845f3393e91d6d102a8a89f339dd24f467.tar.bz2
nng-5ea2a1845f3393e91d6d102a8a89f339dd24f467.zip
fixes #1358 nni_strtou64 and nni_strtox64 could be replaced with strtoull
Diffstat (limited to 'src')
-rw-r--r--src/core/strs.c74
-rw-r--r--src/core/strs.h2
-rw-r--r--src/supplemental/http/http_client.c5
-rw-r--r--src/supplemental/http/http_server.c4
-rw-r--r--src/transport/zerotier/zerotier.c4
5 files changed, 10 insertions, 79 deletions
diff --git a/src/core/strs.c b/src/core/strs.c
index ce03de22..3f51e59a 100644
--- a/src/core/strs.c
+++ b/src/core/strs.c
@@ -173,77 +173,3 @@ nni_asprintf(char **sp, const char *fmt, ...)
*sp = s;
return (0);
}
-
-int
-nni_strtou64(const char *s, uint64_t *u)
-{
- uint64_t v = 0;
-
- // Arguably we could use strtoull, but Windows doesn't conform
- // to C99, and so lacks it.
-
- if ((s == NULL) || (*s == '\0')) {
- // Require a non-empty string.
- return (NNG_EINVAL);
- }
- while (*s) {
- uint64_t last = v;
- if (isdigit(*s)) {
- v *= 10;
- v += (*s - '0');
- } else {
- return (NNG_EINVAL);
- }
- if (v < last) {
- // Overflow!
- return (NNG_EINVAL);
- }
- s++;
- }
- *u = v;
- return (0);
-}
-
-int
-nni_strtox64(const char *s, uint64_t *u)
-{
- uint64_t v = 0;
-
- // Arguably we could use strtoull, but Windows doesn't conform
- // to C99, and so lacks it.
-
- if (s == NULL) {
- return (NNG_EINVAL);
- }
- // Skip over 0x if present.
- if ((s[0] == '0') && ((s[1] == 'x') || (s[1] == 'X'))) {
- s += 2;
- }
- if (*s == '\0') {
- // Require a non-empty string.
- return (NNG_EINVAL);
- }
-
- while (*s) {
- uint64_t last = v;
- if (isdigit(*s)) {
- v *= 16;
- v += (*s - '0');
- } else if ((*s >= 'a') && (*s <= 'f')) {
- v *= 16;
- v += (*s - 'a') + 10;
- } else if ((*s >= 'A') && (*s <= 'F')) {
- v *= 16;
- v += (*s - 'A') + 10;
- } else {
- return (NNG_EINVAL);
- }
- if (v < last) {
- // Overflow!
- return (NNG_EINVAL);
- }
- s++;
- }
- *u = v;
- return (0);
-}
diff --git a/src/core/strs.h b/src/core/strs.h
index 91639c64..697b4763 100644
--- a/src/core/strs.h
+++ b/src/core/strs.h
@@ -21,7 +21,5 @@ extern char * nni_strcasestr(const char *, const char *);
extern int nni_strncasecmp(const char *, const char *, size_t);
extern int nni_strcasecmp(const char *, const char *);
extern int nni_asprintf(char **, const char *, ...);
-extern int nni_strtou64(const char *, uint64_t *); // parses decimal
-extern int nni_strtox64(const char *, uint64_t *); // parses hex
#endif // CORE_STRS_H
diff --git a/src/supplemental/http/http_client.c b/src/supplemental/http/http_client.c
index 36446b63..c6f5167a 100644
--- a/src/supplemental/http/http_client.c
+++ b/src/supplemental/http/http_client.c
@@ -10,6 +10,7 @@
//
#include <stdbool.h>
+#include <stdlib.h>
#include <string.h>
#include "core/nng_impl.h"
@@ -257,6 +258,7 @@ http_txn_cb(void *arg)
{
http_txn * txn = arg;
const char * str;
+ char * end;
int rv;
uint64_t len;
nni_iov iov;
@@ -306,7 +308,8 @@ http_txn_cb(void *arg)
if ((nni_strcasecmp(str, "HEAD") == 0) ||
((str = nni_http_res_get_header(
txn->res, "Content-Length")) == NULL) ||
- (nni_strtou64(str, &len) != 0) || (len == 0)) {
+ ((len = (uint64_t) strtoull(str, &end, 10)) == 0) ||
+ (end == NULL) || (*end != '\0')) {
// If no content-length, or HEAD (which per RFC
// never transfers data), then we are done.
http_txn_finish_aios(txn, 0);
diff --git a/src/supplemental/http/http_server.c b/src/supplemental/http/http_server.c
index 49c236e4..e1f51e52 100644
--- a/src/supplemental/http/http_server.c
+++ b/src/supplemental/http/http_server.c
@@ -704,8 +704,10 @@ http_sconn_rxdone(void *arg)
if ((h->getbody) &&
((cls = nni_http_req_get_header(req, "Content-Length")) != NULL)) {
uint64_t len;
+ char *end;
- if ((nni_strtou64(cls, &len) != 0) || (len > h->maxbody)) {
+ len = strtoull(cls, &end, 10);
+ if ((end == NULL) || (*end != '\0') || (len > h->maxbody)) {
nni_mtx_unlock(&s->mtx);
http_sconn_error(sc, NNG_HTTP_STATUS_BAD_REQUEST);
return;
diff --git a/src/transport/zerotier/zerotier.c b/src/transport/zerotier/zerotier.c
index c66a4b2c..896add29 100644
--- a/src/transport/zerotier/zerotier.c
+++ b/src/transport/zerotier/zerotier.c
@@ -1521,12 +1521,14 @@ zt_walk_moons(const char *path, void *arg)
{
zt_node * ztn = arg;
const char *bn = nni_file_basename(path);
+ char * end;
uint64_t moonid;
if (strncmp(bn, "moon.", 5) != 0) {
return (NNI_FILE_WALK_CONTINUE);
}
- if (nni_strtox64(bn + 5, &moonid) == 0) {
+ if (((moonid = (uint64_t) strtoull(bn + 5, &end, 16)) != 0) &&
+ (*end == '\0')) {
ZT_Node_orbit(ztn->zn_znode, NULL, moonid, 0);
}
return (NNI_FILE_WALK_CONTINUE);