From 940b372554cebf986056be19d7ff42544b3d25af Mon Sep 17 00:00:00 2001 From: Olaf Bergmann Date: Fri, 10 Jun 2011 18:02:17 +0200 Subject: [PATCH] Deprecate coap_new_uri() as it might waste storage --- uri.c | 20 ++++++++++++++++---- uri.h | 6 +++++- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/uri.c b/uri.c index aa1a1454..6cea5898 100644 --- a/uri.c +++ b/uri.c @@ -17,7 +17,7 @@ #include "uri.h" int -coap_split_uri(unsigned char *str_var, size_t len, coap_uri_t *uri) { +coap_split_uri(const unsigned char *str_var, size_t len, coap_uri_t *uri) { unsigned char *p, *q; int secure = 0, res = 0; @@ -303,14 +303,26 @@ coap_split_path_impl(unsigned char *s, size_t length, int is_path, coap_uri_t * coap_new_uri(const unsigned char *uri, unsigned int length) { - unsigned char *result = coap_malloc(length + 1 + sizeof(coap_uri_t)); - if ( !result ) + unsigned char *result; + + /** + * @bug Some additional storage is needed to split path and query + * into segments. Unfortunately, we do not know in advance, how + * many segments we will get. A quick hack is to assume that every + * segment will have 4 characters in average and hope for the best. + */ + result = coap_malloc(length + 1 + sizeof(coap_uri_t)); + + if (!result) return NULL; memcpy(URI_DATA(result), uri, length); URI_DATA(result)[length] = '\0'; /* make it zero-terminated */ - coap_split_uri( URI_DATA(result), length, (coap_uri_t *)result ); + if (coap_split_uri(URI_DATA(result), length, (coap_uri_t *)result) < 0) { + free(result); + return NULL; + } return (coap_uri_t *)result; } diff --git a/uri.h b/uri.h index caf0a8c9..a70f609d 100644 --- a/uri.h +++ b/uri.h @@ -36,7 +36,7 @@ typedef struct { * @return @c 0 on success, or < 0 on error. */ int -coap_split_uri(unsigned char *str_var, size_t len, coap_uri_t *uri); +coap_split_uri(const unsigned char *str_var, size_t len, coap_uri_t *uri); /** * Splits the given string into segments. You should call one of the @@ -86,6 +86,10 @@ int coap_split_path_impl(unsigned char *s, size_t length, int is_path, * @param uri The URI path to copy. * @para length The length of uri. * @return New URI object or NULL on error. + * + * @depreated This function has inconvenient storage allocation + * characteristics to split URI path and query. Better do that + * manually. */ coap_uri_t *coap_new_uri(const unsigned char *uri, unsigned int length);