From 0c53e63a3d685b1cd185ebe849a80cbe1cf32740 Mon Sep 17 00:00:00 2001 From: Olaf Bergmann Date: Mon, 9 Mar 2015 17:49:21 +0100 Subject: [PATCH] Fixed URI parser bug and removed broken parser iterator The parser iterator did not break up URIs correctly. As this cannot be fixed easily, the parser iterator has been removed. coap_split_path_impl() now operates directly on the message buffer instead of the iterator abstraction. This bug also caused t_parse_uri12() in the URI test suite to fail. --- ChangeLog | 5 ++ include/coap/uri.h | 55 ------------------ src/uri.c | 141 +++++++++++++++------------------------------ 3 files changed, 53 insertions(+), 148 deletions(-) diff --git a/ChangeLog b/ChangeLog index c606e247..138c5e95 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2015-03-09 Olaf Bergmann + + * src/uri.c (coap_split_path): Fixed URI parser bug and + removed broken parse iterator. + 2015-03-05 Olaf Bergmann * src/coap_time.c (coap_ticks): Changed POSIX implementation diff --git a/include/coap/uri.h b/include/coap/uri.h index 3eb283db..438478a6 100644 --- a/include/coap/uri.h +++ b/include/coap/uri.h @@ -60,61 +60,6 @@ int coap_hash_path(const unsigned char *path, size_t len, coap_key_t key); * @{ */ -/** - * Iterator to for tokenizing a URI path or query. This structure must - * be initialized with coap_parse_iterator_init(). Call - * coap_parse_next() to walk through the tokens. - * - * @code - * unsigned char *token; - * coap_parse_iterator_t pi; - * coap_parse_iterator_init(uri.path.s, uri.path.length, '/', "?#", 2, &pi); - * - * while ((token = coap_parse_next(&pi))) { - * ... do something with token ... - * } - * @endcode - */ -typedef struct { - size_t n; /**< number of remaining characters in buffer */ - unsigned char separator; /**< segment separators */ - unsigned char *delim; /**< delimiters where to split the string */ - size_t dlen; /**< length of separator */ - unsigned char *pos; /**< current position in buffer */ - size_t segment_length; /**< length of current segment */ -} coap_parse_iterator_t; - -/** - * Initializes the given iterator @p pi. - * - * @param s The string to tokenize. - * @param n The length of @p s. - * @param separator The separator character that delimits tokens. - * @param delim A set of characters that delimit @p s. - * @param dlen The length of @p delim. - * @param pi The iterator object to initialize. - * - * @return The initialized iterator object @p pi. - */ -coap_parse_iterator_t * -coap_parse_iterator_init(unsigned char *s, size_t n, - unsigned char separator, - unsigned char *delim, size_t dlen, - coap_parse_iterator_t *pi); - -/** - * Updates the iterator @p pi to point to the next token. This - * function returns a pointer to that token or @c NULL if no more - * tokens exist. The contents of @p pi will be updated. In particular, - * @c pi->segment_length specifies the length of the current token, @c - * pi->pos points to its beginning. - * - * @param pi The iterator to update. - * - * @return The next token or @c NULL if no more tokens exist. - */ -unsigned char *coap_parse_next(coap_parse_iterator_t *pi); - /** * Parses a given string into URI components. The identified syntactic * components are stored in the result parameter @p uri. Optional URI diff --git a/src/uri.c b/src/uri.c index 74ea80d4..66046b1f 100644 --- a/src/uri.c +++ b/src/uri.c @@ -1,6 +1,6 @@ /* uri.c -- helper functions for URI treatment * - * Copyright (C) 2010--2012 Olaf Bergmann + * Copyright (C) 2010--2012,2015 Olaf Bergmann * * This file is part of the CoAP library libcoap. Please see * README for terms of use. @@ -297,6 +297,14 @@ make_decoded_option(const unsigned char *s, size_t length, typedef void (*segment_handler_t)(unsigned char *, size_t, void *); +/** + * Checks if path segment @p s consists of one or two dots. + */ +static inline int +dots(unsigned char *s, size_t len) { + return *s == '.' && (len == 1 || (*(s+1) == '.' && len == 2)); +} + /** * Splits the given string into segments. You should call one of the * macros coap_split_path() or coap_split_query() instead. @@ -308,23 +316,32 @@ typedef void (*segment_handler_t)(unsigned char *, size_t, void *); * @return The number of characters that have been parsed from @p s. */ static size_t -coap_split_path_impl(coap_parse_iterator_t *parse_iter, +coap_split_path_impl(const unsigned char *s, size_t length, segment_handler_t h, void *data) { - unsigned char *seg; - size_t length; - - assert(parse_iter); - assert(h); - length = parse_iter->n; - - while ( (seg = coap_parse_next(parse_iter)) ) { + const unsigned char *p, *q; - /* any valid path segment is handled here: */ - h(seg, parse_iter->segment_length, data); + p = q = s; + while (length > 0 && !strnchr((unsigned char *)"?#", 2, *q)) { + if (*q == '/') { /* start new segment */ + + if (!dots((unsigned char *)p, q - p)) { + h((unsigned char *)p, q - p, data); + } + + p = q + 1; + } + + q++; + length--; } - - return length - (parse_iter->n - parse_iter->segment_length); + + /* write last segment */ + if (!dots((unsigned char *)p, q - p)) { + h((unsigned char *)p, q - p, data); + } + + return q - s; } struct cnt_str { @@ -338,10 +355,6 @@ write_option(unsigned char *s, size_t len, void *data) { int res; assert(state); - /* skip empty segments and those that consist of only one or two dots */ - if (memcmp(s, "..", min(len,2)) == 0) - return; - res = make_decoded_option(s, len, state->buf.s, state->buf.length); if (res > 0) { state->buf.s += res; @@ -354,13 +367,11 @@ int coap_split_path(const unsigned char *s, size_t length, unsigned char *buf, size_t *buflen) { struct cnt_str tmp = { { *buflen, buf }, 0 }; - coap_parse_iterator_t pi; - coap_parse_iterator_init((unsigned char *)s, length, - '/', (unsigned char *)"?#", 2, &pi); - coap_split_path_impl(&pi, write_option, &tmp); + coap_split_path_impl(s, length, write_option, &tmp); *buflen = *buflen - tmp.buf.length; + return tmp.n; } @@ -368,14 +379,23 @@ int coap_split_query(const unsigned char *s, size_t length, unsigned char *buf, size_t *buflen) { struct cnt_str tmp = { { *buflen, buf }, 0 }; - coap_parse_iterator_t pi; + const unsigned char *p; - coap_parse_iterator_init((unsigned char *)s, length, - '&', (unsigned char *)"#", 1, &pi); + p = s; + while (length > 0 && *s != '#') { + if (*s == '&') { /* start new query element */ + write_option((unsigned char *)p, s - p, &tmp); + p = s + 1; + } - coap_split_path_impl(&pi, write_option, &tmp); + s++; + length--; + } - *buflen = tmp.buf.length; + /* write last query element */ + write_option((unsigned char *)p, s - p, &tmp); + + *buflen = *buflen - tmp.buf.length; return tmp.n; } @@ -452,77 +472,12 @@ hash_segment(unsigned char *s, size_t len, void *data) { int coap_hash_path(const unsigned char *path, size_t len, coap_key_t key) { - coap_parse_iterator_t pi; - if (!path) return 0; memset(key, 0, sizeof(coap_key_t)); - coap_parse_iterator_init((unsigned char *)path, len, - '/', (unsigned char *)"?#", 2, &pi); - coap_split_path_impl(&pi, hash_segment, key); + coap_split_path_impl(path, len, hash_segment, key); return 1; } - -/* iterator functions */ - -coap_parse_iterator_t * -coap_parse_iterator_init(unsigned char *s, size_t n, - unsigned char separator, - unsigned char *delim, size_t dlen, - coap_parse_iterator_t *pi) { - assert(pi); - assert(separator); - - pi->separator = separator; - pi->delim = delim; - pi->dlen = dlen; - pi->pos = s; - pi->n = n; - pi->segment_length = 0; - - return pi; -} - -unsigned char * -coap_parse_next(coap_parse_iterator_t *pi) { - unsigned char *p; - - if (!pi) - return NULL; - - /* proceed to the next segment */ - pi->n -= pi->segment_length; - pi->pos += pi->segment_length; - pi->segment_length = 0; - - /* last segment? */ - if (!pi->n || strnchr(pi->delim, pi->dlen, *pi->pos)) { - pi->pos = NULL; - return NULL; - } - - /* skip following separator (the first segment might not have one) */ - if (*pi->pos == pi->separator) { - ++pi->pos; - --pi->n; - } - - p = pi->pos; - - while (pi->segment_length < pi->n && *p != pi->separator && - !strnchr(pi->delim, pi->dlen, *p)) { - ++p; - ++pi->segment_length; - } - - if (!pi->n) { - pi->pos = NULL; - pi->segment_length = 0; - } - - return pi->pos; -} -