From 008dd6ab21889e189384210d4506bf5640db348e Mon Sep 17 00:00:00 2001 From: Andrew Bettison Date: Tue, 29 Apr 2014 13:39:21 +0930 Subject: [PATCH] Add size_t arg to strn_to_sid_t() --- dataformats.c | 25 ++++++++++++------------- meshms_restful.c | 4 ++-- rhizome_restful.c | 2 +- serval.h | 2 +- 4 files changed, 16 insertions(+), 17 deletions(-) diff --git a/dataformats.c b/dataformats.c index 8d26393c..4e8bfd15 100644 --- a/dataformats.c +++ b/dataformats.c @@ -28,30 +28,29 @@ int cmp_sid_t(const sid_t *a, const sid_t *b) return memcmp(a, b, sizeof a->binary); } -int str_to_sid_t(sid_t *sid, const char *hex) +int str_to_sid_t(sid_t *sidp, const char *hex) { - if (strcmp(hex, "broadcast") == 0) { - if (sid) - *sid = SID_BROADCAST; - return 0; - } - return sid ? fromhexstr(sid->binary, hex, sizeof sid->binary) : is_xstring(hex, SID_STRLEN) ? 0 : -1; + const char *end; + return strn_to_sid_t(sidp, hex, SIZE_MAX, &end) != -1 && *end == '\0' ? 0 : -1; } -int strn_to_sid_t(sid_t *sid, const char *hex, const char **endp) +int strn_to_sid_t(sid_t *sidp, const char *hex, size_t hexlen, const char **endp) { - if (str_startswith(hex, "broadcast", endp)) { - if (sid) - *sid = SID_BROADCAST; + if (strn_startswith(hex, hexlen, "broadcast", endp)) { + if (sidp) + *sidp = SID_BROADCAST; return 0; } sid_t tmp; + if (hexlen < sizeof tmp.binary * 2) + return -1; int n = fromhex(tmp.binary, hex, sizeof tmp.binary); if (n != sizeof tmp.binary) return -1; - *sid = tmp; + if (sidp) + *sidp = tmp; if (endp) - *endp = hex + sizeof sid->binary * 2; + *endp = hex + sizeof tmp.binary * 2; return 0; } diff --git a/meshms_restful.c b/meshms_restful.c index 19cf2a4c..3b2233a2 100644 --- a/meshms_restful.c +++ b/meshms_restful.c @@ -121,12 +121,12 @@ int restful_meshms_(httpd_request *r, const char *remainder) const char *verb = HTTP_VERB_GET; HTTP_HANDLER *handler = NULL; const char *end; - if (strn_to_sid_t(&r->sid1, remainder, &end) != -1) { + if (strn_to_sid_t(&r->sid1, remainder, SIZE_MAX, &end) != -1) { remainder = end; if (strcmp(remainder, "/conversationlist.json") == 0) { handler = restful_meshms_conversationlist_json; remainder = ""; - } else if (*remainder == '/' && strn_to_sid_t(&r->sid2, remainder + 1, &end) != -1) { + } else if (*remainder == '/' && strn_to_sid_t(&r->sid2, remainder + 1, SIZE_MAX, &end) != -1) { remainder = end; if (strcmp(remainder, "/messagelist.json") == 0) { handler = restful_meshms_messagelist_json; diff --git a/rhizome_restful.c b/rhizome_restful.c index bc85e1bd..ad6f6cad 100644 --- a/rhizome_restful.c +++ b/rhizome_restful.c @@ -414,7 +414,7 @@ static int insert_mime_part_end(struct http_request *hr) httpd_request *r = (httpd_request *) hr; if (r->u.insert.current_part == PART_AUTHOR) { if ( r->u.insert.author_hex_len != sizeof r->u.insert.author_hex - || strn_to_sid_t(&r->u.insert.author, r->u.insert.author_hex, NULL) == -1 + || strn_to_sid_t(&r->u.insert.author, r->u.insert.author_hex, sizeof r->u.insert.author_hex, NULL) == -1 ) return http_response_form_part(r, "Invalid", PART_AUTHOR, r->u.insert.author_hex, r->u.insert.author_hex_len); r->u.insert.received_author = 1; diff --git a/serval.h b/serval.h index 0d483fb3..24e06dce 100644 --- a/serval.h +++ b/serval.h @@ -192,7 +192,7 @@ typedef struct sid_binary { int cmp_sid_t(const sid_t *a, const sid_t *b); int str_to_sid_t(sid_t *sid, const char *hex); -int strn_to_sid_t(sid_t *sid, const char *hex, const char **endp); +int strn_to_sid_t(sid_t *sid, const char *hex, size_t hexlen, const char **endp); #define alloca_tohex_sas(sas) alloca_tohex((sas), SAS_SIZE)