From 7aeb8651ba0f3b69ff53bca043fe339d595842ab Mon Sep 17 00:00:00 2001 From: Jan Vidar Krey Date: Mon, 23 Mar 2009 21:47:05 +0100 Subject: [PATCH] Fix bug #12: asserts in adc_msg_parse -> enabled strict utf8 parsing. Signed-off-by: Jan Vidar Krey --- ChangeLog | 1 + src/message.c | 9 +++++++-- src/misc.c | 35 +++++++++++++++++++++++++++++------ src/misc.h | 1 + version.h | 2 +- 5 files changed, 39 insertions(+), 9 deletions(-) diff --git a/ChangeLog b/ChangeLog index de6de0a..6ceb293 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,6 +1,7 @@ 0.2.8: - Fix bug #13: getsockname() failure, use sockaddr from accept() instead. - Fix bug #10: Improve logging, ensure logs are machine readable. +- Fix bug #12: asserts in adc_msg_parse -> enabled strict utf8 parsing. 0.2.7: diff --git a/src/message.c b/src/message.c index 92d1a95..372c9ec 100644 --- a/src/message.c +++ b/src/message.c @@ -26,8 +26,6 @@ uhub_assert(X->capacity); \ uhub_assert(X->length); \ uhub_assert(X->length <= X->capacity); \ - if (X->length != strlen(X->cache)) \ - hub_log(log_error, "Assert: X->length (%d) != strlen(X->cache) (%d)", X->length, strlen(X->cache)); \ uhub_assert(X->length == strlen(X->cache)); \ uhub_assert(X->references >= 0); #else @@ -265,6 +263,13 @@ struct adc_message* adc_msg_parse(const char* line, size_t length) if (command == NULL) return NULL; /* OOM */ + + if (!is_printable_utf8(line, length)) + { + hub_log(log_debug, "Dropped message with non-printable UTF-8 characters."); + hub_free(command); + return NULL; + } if (line[length-1] != '\n') { diff --git a/src/misc.c b/src/misc.c index 20242f9..7434817 100644 --- a/src/misc.c +++ b/src/misc.c @@ -31,6 +31,16 @@ int is_white_space(char c) return 0; } +int is_printable(char c) +{ + if (c >= 32) + return 1; + + if (c == '\t' || c == '\r' || c == '\n') + return 1; + return 0; +} + char* strip_white_space(char* string) { @@ -48,17 +58,15 @@ char* strip_white_space(char* string) return string; } - -int is_valid_utf8(const char* string) +static int is_valid_utf8_str(const char* string, size_t length) { int expect = 0; char div = 0; - int pos = 0; - int length = strlen(string); + size_t pos = 0; if (length == 0) return 1; - for (pos = 0; pos < strlen(string); pos++) + for (pos = 0; pos < length; pos++) { if (expect) { @@ -74,13 +82,28 @@ int is_valid_utf8(const char* string) if (string[pos] & div) expect++; else break; } - if ((string[pos] & div) || (pos+expect >= strlen(string))) return 0; + if ((string[pos] & div) || (pos+expect >= length)) return 0; } } } return 1; } +int is_valid_utf8(const char* string) +{ + return is_valid_utf8_str(string, strlen(string)); +} + +int is_printable_utf8(const char* string, size_t length) +{ + size_t pos = 0; + for (pos = 0; pos < length; pos++) + { + if (!is_printable(string[pos])) + return 0; + } + return is_valid_utf8_str(string, length); +} int is_valid_base32_char(char c) { diff --git a/src/misc.h b/src/misc.h index 789c616..bac0928 100644 --- a/src/misc.h +++ b/src/misc.h @@ -26,6 +26,7 @@ extern int is_num(char c); extern int is_space(char c); extern int is_white_space(char c); extern int is_valid_utf8(const char* string); +extern int is_printable_utf8(const char* string, size_t length); extern int is_valid_base32_char(char c); extern void base32_encode(const unsigned char* buffer, size_t len, char* result); extern void base32_decode(const char* src, unsigned char* dst, size_t len); diff --git a/version.h b/version.h index 2bb34ed..335a101 100644 --- a/version.h +++ b/version.h @@ -7,7 +7,7 @@ #endif #ifndef VERSION -#define VERSION "0.2.7" +#define VERSION "0.2.8-alpha" #endif #ifndef COPYRIGHT