From: Stephan Bosch Date: Fri, 15 Oct 2021 19:08:50 +0000 (+0200) Subject: lib: uri-util - Add uri_char_sanitize() and use it in error messages. X-Git-Tag: 2.4.0~4709 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=6dc0dd42209cd624a8cf083874f8b12d423e0f13;p=thirdparty%2Fdovecot%2Fcore.git lib: uri-util - Add uri_char_sanitize() and use it in error messages. --- diff --git a/src/lib-imap/imap-url.c b/src/lib-imap/imap-url.c index 6da6e21752..5213fd1857 100644 --- a/src/lib-imap/imap-url.c +++ b/src/lib-imap/imap-url.c @@ -231,7 +231,9 @@ static int imap_url_parse_iserver(struct imap_url_parser *url_parser) for (p += 6; *p != '\0'; p++) { if (*p == ';' || *p == ':') { parser->error = t_strdup_printf( - "Stray '%c' in userinfo `%s'", *p, auth.enc_userinfo); + "Stray %s in userinfo `%s'", + uri_char_sanitize(*p), + auth.enc_userinfo); return -1; } } diff --git a/src/lib/uri-util.c b/src/lib/uri-util.c index 4e73d6d764..e4dc419b8a 100644 --- a/src/lib/uri-util.c +++ b/src/lib/uri-util.c @@ -150,7 +150,8 @@ uri_parse_pct_encoded_data(struct uri_parser *parser, if (value < 0) { parser->error = p_strdup_printf( parser->pool, - "Expecting hex digit after '%%', but found '%c'", **p); + "Expecting hex digit after '%%', but found %s", + uri_char_sanitize(**p)); return -1; } @@ -161,8 +162,8 @@ uri_parse_pct_encoded_data(struct uri_parser *parser, if (value < 0) { parser->error = p_strdup_printf( parser->pool, - "Expecting hex digit after '%%%c', but found '%c'", - *((*p)-1), **p); + "Expecting hex digit after '%%%c', but found %s", + *((*p)-1), uri_char_sanitize(**p)); return -1; } diff --git a/src/lib/uri-util.h b/src/lib/uri-util.h index b129730311..cc8febac5a 100644 --- a/src/lib/uri-util.h +++ b/src/lib/uri-util.h @@ -38,6 +38,13 @@ struct uri_parser { bool allow_pct_nul:1; }; +static inline const char *uri_char_sanitize(unsigned char c) +{ + if (c >= 0x20 && c < 0x7F) + return t_strdup_printf("'%c'", c); + return t_strdup_printf("<0x%02x>", c); +} + /* Parse one instance of percent encoding. Returns 1 for success, 0 if none is present at the current parser position, and -1 in case of error. The decoded character is returned in ch_r upon success. */