From a8ee174c8cefe8781eb3453c7e1b3b9c22ae4213 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Mon, 14 Mar 2016 22:54:38 +0000 Subject: [PATCH] Sync from rspamd --- src/dns_private.h | 8 -------- src/logger.h | 8 ++++---- src/packet.c | 21 +++++++++++++-------- src/punycode.c | 8 +++++++- 4 files changed, 24 insertions(+), 21 deletions(-) diff --git a/src/dns_private.h b/src/dns_private.h index d23f845..76e3249 100644 --- a/src/dns_private.h +++ b/src/dns_private.h @@ -30,14 +30,6 @@ #include "upstream.h" #include "ref.h" -static const unsigned base = 36; -static const unsigned t_min = 1; -static const unsigned t_max = 26; -static const unsigned skew = 38; -static const unsigned damp = 700; -static const unsigned initial_n = 128; -static const unsigned initial_bias = 72; - static const int dns_port = 53; static const int default_io_cnt = 8; diff --git a/src/logger.h b/src/logger.h index 91108f4..f9c33bb 100644 --- a/src/logger.h +++ b/src/logger.h @@ -34,9 +34,9 @@ void rdns_logger_helper (struct rdns_resolver *resolver, enum rdns_log_level level, const char *function, const char *format, ...); -#define rdns_err(...) do { rdns_logger_helper (resolver, RDNS_LOG_ERROR, __FUNCTION__, __VA_ARGS__); } while (0) -#define rdns_warn(...) do { rdns_logger_helper (resolver, RDNS_LOG_WARNING, __FUNCTION__, __VA_ARGS__); } while (0) -#define rdns_info(...) do { rdns_logger_helper (resolver, RDNS_LOG_INFO, __FUNCTION__, __VA_ARGS__); } while (0) -#define rdns_debug(...) do { rdns_logger_helper (resolver, RDNS_LOG_DEBUG, __FUNCTION__, __VA_ARGS__); } while (0) +#define rdns_err(...) do { rdns_logger_helper (resolver, RDNS_LOG_ERROR, __func__, __VA_ARGS__); } while (0) +#define rdns_warn(...) do { rdns_logger_helper (resolver, RDNS_LOG_WARNING, __func__, __VA_ARGS__); } while (0) +#define rdns_info(...) do { rdns_logger_helper (resolver, RDNS_LOG_INFO, __func__, __VA_ARGS__); } while (0) +#define rdns_debug(...) do { rdns_logger_helper (resolver, RDNS_LOG_DEBUG, __func__, __VA_ARGS__); } while (0) #endif /* LOGGER_H_ */ diff --git a/src/packet.c b/src/packet.c index 88e51df..532b5de 100644 --- a/src/packet.c +++ b/src/packet.c @@ -129,10 +129,10 @@ rdns_format_dns_name (struct rdns_resolver *resolver, const char *in, } /* We need to encode */ - p = in; - olen = inlen + 1 + sizeof ("xn--") * labels; - *out = malloc (olen); + /* We allocate 4 times more memory as we cannot guarantee encoding bounds */ + olen = inlen * sizeof (int32_t) + 1 + sizeof ("xn--") * labels; + *out = malloc (olen + 1); if (*out == NULL) { return false; @@ -158,8 +158,7 @@ rdns_format_dns_name (struct rdns_resolver *resolver, const char *in, } else { rdns_info ("no buffer remain for punycoding query"); - free (*out); - return false; + goto err; } free (uclabel); @@ -183,7 +182,7 @@ rdns_format_dns_name (struct rdns_resolver *resolver, const char *in, } if (remain < label_len + 1) { rdns_info ("no buffer remain for punycoding query"); - return false; + goto err; } if (label_len == 0) { /* Two dots in order, skip this */ @@ -208,7 +207,7 @@ rdns_format_dns_name (struct rdns_resolver *resolver, const char *in, } if (remain < label_len + 1) { rdns_info ("no buffer remain for punycoding query"); - return false; + goto err; } memcpy (o, p, label_len); o += label_len; @@ -220,14 +219,20 @@ rdns_format_dns_name (struct rdns_resolver *resolver, const char *in, } if (remain == 0) { rdns_info ("no buffer remain for punycoding query"); - return false; + goto err; } } + *o = '\0'; *outlen = o - *out; return true; + + err: + free (*out); + *out = NULL; + return false; } bool diff --git a/src/punycode.c b/src/punycode.c index 6ce3484..909d0d9 100644 --- a/src/punycode.c +++ b/src/punycode.c @@ -33,7 +33,13 @@ */ #include "dns_private.h" - +static const unsigned base = 36; +static const unsigned t_min = 1; +static const unsigned t_max = 26; +static const unsigned skew = 38; +static const unsigned damp = 700; +static const unsigned initial_n = 128; +static const unsigned initial_bias = 72; /* Punycode utility */ static unsigned int digit (unsigned n)