Tarantool development patches archive
 help / color / mirror / Atom feed
* [tarantool-patches] [PATCH v2 1/2] Refactoring in string validation of log parameters
@ 2018-07-19 10:54 Olga Arkhangelskaia
  2018-07-19 10:54 ` [tarantool-patches] [PATCH v2 2/2] Added strdup fail checks in say Olga Arkhangelskaia
  2018-07-19 12:24 ` [tarantool-patches] [PATCH v2 1/2] Refactoring in string validation of log parameters Vladimir Davydov
  0 siblings, 2 replies; 5+ messages in thread
From: Olga Arkhangelskaia @ 2018-07-19 10:54 UTC (permalink / raw)
  To: tarantool-patches; +Cc: Olga Arkhangelskaia

Removed unnecessary double check of log configuration string.
Instead all log configuration checks were moved to separate function.
---
Branch: https://github.com/tarantool/tarantool/tree/OKriw/refactor-log-cfg

v1:
https://www.freelists.org/post/tarantool-patches/PATCH-Refactoring-in-string-validation-of-log-parametrs

Changes in v2:
- exceptions transfered to box.cc
- say_parse_logger_type traansfered to say_ckeck_cfg


 src/box/box.cc | 40 ++++++++--------------------------------
 src/say.c      | 34 +++++++++++++++++++++++++---------
 src/say.h      |  8 ++++----
 3 files changed, 37 insertions(+), 45 deletions(-)

diff --git a/src/box/box.cc b/src/box/box.cc
index 7c6312d78..db598957a 100644
--- a/src/box/box.cc
+++ b/src/box/box.cc
@@ -356,41 +356,17 @@ box_check_say()
 	const char *log = cfg_gets("log");
 	if (log == NULL)
 		return;
-	enum say_logger_type type;
-	if (say_parse_logger_type(&log, &type) < 0) {
-		tnt_raise(ClientError, ER_CFG, "log",
-			  diag_last_error(diag_get())->errmsg);
-	}
-
-	if (say_check_init_str(log) == -1) {
-
-		diag_raise();
-	}
-
-	if (type == SAY_LOGGER_SYSLOG) {
-		struct say_syslog_opts opts;
-		if (say_parse_syslog_opts(log, &opts) < 0) {
-			if (diag_last_error(diag_get())->type ==
-			    &type_IllegalParams) {
-				tnt_raise(ClientError, ER_CFG, "log",
-					  diag_last_error(diag_get())->errmsg);
-			}
-		}
-		say_free_syslog_opts(&opts);
-		diag_raise();
-	}
 
 	const char *log_format = cfg_gets("log_format");
-	enum say_format format = say_format_by_name(log_format);
-	if (format == say_format_MAX)
-		diag_set(ClientError, ER_CFG, "log_format",
-			 "expected 'plain' or 'json'");
-	if (type == SAY_LOGGER_SYSLOG && format == SF_JSON) {
-		tnt_raise(ClientError, ER_ILLEGAL_PARAMS, "log, log_format");
-	}
 	int log_nonblock = cfg_getb("log_nonblock");
-	if (log_nonblock == 1 && type == SAY_LOGGER_FILE) {
-		tnt_raise(ClientError, ER_ILLEGAL_PARAMS, "log, log_nonblock");
+	if (say_check_cfg(log, log_format, log_nonblock) < 0) {
+		if (diag_last_error(diag_get())->type ==
+		    &type_IllegalParams) {
+			tnt_raise(ClientError, ER_CFG,
+				  "log, log_format or log_nonblock",
+				  diag_last_error(diag_get())->errmsg);
+		}
+
 	}
 }
 
diff --git a/src/say.c b/src/say.c
index 43124083c..92e0e6d9f 100644
--- a/src/say.c
+++ b/src/say.c
@@ -952,25 +952,41 @@ write_to_syslog(struct log *log, int total)
 /** Loggers }}} */
 
 /*
- * Init string parser(s)
+ * Checks in logger configuration
  */
 
 int
-say_check_init_str(const char *str)
+say_check_cfg(const char *log, const char *log_format, int log_nonblock)
 {
+	int res = 0;
 	enum say_logger_type type;
-	if (say_parse_logger_type(&str, &type)) {
-		diag_set(IllegalParams, logger_syntax_reminder);
-		return -1;
+	if (say_parse_logger_type(&log, &type) < 0) {
+		diag_set(IllegalParams, "Unrecognised logger type '%s'", log);
+		res = -1;
 	}
 	if (type == SAY_LOGGER_SYSLOG) {
 		struct say_syslog_opts opts;
-
-		if (say_parse_syslog_opts(str, &opts) < 0)
-			return -1;
+		int ret = say_parse_syslog_opts(log, &opts);
 		say_free_syslog_opts(&opts);
+		if (ret < 0) {
+			diag_set(IllegalParams, "Incorrect option '%s'", log);
+			res = -1;
+		}
 	}
-	return 0;
+	enum say_format format = say_format_by_name(log_format);
+	if (format == say_format_MAX) {
+		diag_set(IllegalParams,
+			"for log_format expected 'plain' or 'json'");
+	}
+	if (type == SAY_LOGGER_SYSLOG && format == SF_JSON) {
+		diag_set(IllegalParams, "Illegal parametrs log, log_format");
+		res = -1;
+	}
+	if (log_nonblock == 1 && type == SAY_LOGGER_FILE) {
+		diag_set(IllegalParams, "Illegal parametrs log, log_nonblock");
+		res = -1;
+	}
+	return res;
 }
 
 /**
diff --git a/src/say.h b/src/say.h
index ad3ba3417..d14ae8b45 100644
--- a/src/say.h
+++ b/src/say.h
@@ -371,12 +371,12 @@ CFORMAT(printf, 5, 0) extern sayfunc_t _say;
 	log_say_level(log, S_SYSERROR, strerror(errno), format, ##__VA_ARGS__)
 
 /**
- * validates logger init string;
- * @returns 0 if validation passed or -1
- *           with an error message written to diag
+ * Validates logger configuration
+ * @retval -1 check is failed, invalid parameters
+ * @retval 0 check is successful
  */
 int
-say_check_init_str(const char *str);
+say_check_cfg(const char *log, const char *log_format, int log_nonblock);
 
 /* internals, for unit testing */
 
-- 
2.14.3 (Apple Git-98)

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [tarantool-patches] [PATCH v2 2/2] Added strdup fail checks in say
  2018-07-19 10:54 [tarantool-patches] [PATCH v2 1/2] Refactoring in string validation of log parameters Olga Arkhangelskaia
@ 2018-07-19 10:54 ` Olga Arkhangelskaia
  2018-07-19 12:26   ` Vladimir Davydov
  2018-07-19 13:12   ` Vladimir Davydov
  2018-07-19 12:24 ` [tarantool-patches] [PATCH v2 1/2] Refactoring in string validation of log parameters Vladimir Davydov
  1 sibling, 2 replies; 5+ messages in thread
From: Olga Arkhangelskaia @ 2018-07-19 10:54 UTC (permalink / raw)
  To: tarantool-patches; +Cc: Olga Arkhangelskaia

Strdup may silently fail without any message from tarantool.
Patch adds this checks.
---
Branch: https://github.com/tarantool/tarantool/tree/OKriw/refactor-log-cfg

v1:
https://www.freelists.org/post/tarantool-patches/PATCH-22-Added-strdup-fail-checks-in-say

Changes in v2:
- no double check now

 src/say.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/src/say.c b/src/say.c
index 92e0e6d9f..99344b685 100644
--- a/src/say.c
+++ b/src/say.c
@@ -502,6 +502,11 @@ log_syslog_init(struct log *log, const char *init_str)
 		log->syslog_ident = strdup("tarantool");
 	else
 		log->syslog_ident = strdup(opts.identity);
+	if (log->syslog_ident == NULL) {
+		diag_set(OutOfMemory, strlen(opts.identity), "malloc",
+		         "log->syslog_ident");
+		return -1;
+	}
 
 	if (opts.facility == syslog_facility_MAX)
 		log->syslog_facility = SYSLOG_LOCAL7;
-- 
2.14.3 (Apple Git-98)

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [tarantool-patches] [PATCH v2 1/2] Refactoring in string validation of log parameters
  2018-07-19 10:54 [tarantool-patches] [PATCH v2 1/2] Refactoring in string validation of log parameters Olga Arkhangelskaia
  2018-07-19 10:54 ` [tarantool-patches] [PATCH v2 2/2] Added strdup fail checks in say Olga Arkhangelskaia
@ 2018-07-19 12:24 ` Vladimir Davydov
  1 sibling, 0 replies; 5+ messages in thread
From: Vladimir Davydov @ 2018-07-19 12:24 UTC (permalink / raw)
  To: Olga Arkhangelskaia; +Cc: tarantool-patches

On Thu, Jul 19, 2018 at 01:54:48PM +0300, Olga Arkhangelskaia wrote:
> Removed unnecessary double check of log configuration string.
> Instead all log configuration checks were moved to separate function.
> ---
> Branch: https://github.com/tarantool/tarantool/tree/OKriw/refactor-log-cfg
> 
> v1:
> https://www.freelists.org/post/tarantool-patches/PATCH-Refactoring-in-string-validation-of-log-parametrs
> 
> Changes in v2:
> - exceptions transfered to box.cc
> - say_parse_logger_type traansfered to say_ckeck_cfg
> 
> 
>  src/box/box.cc | 40 ++++++++--------------------------------
>  src/say.c      | 34 +++++++++++++++++++++++++---------
>  src/say.h      |  8 ++++----
>  3 files changed, 37 insertions(+), 45 deletions(-)
> 
> diff --git a/src/box/box.cc b/src/box/box.cc
> index 7c6312d78..db598957a 100644
> --- a/src/box/box.cc
> +++ b/src/box/box.cc
> @@ -356,41 +356,17 @@ box_check_say()
>  	const char *log = cfg_gets("log");
>  	if (log == NULL)
>  		return;
> -	enum say_logger_type type;
> -	if (say_parse_logger_type(&log, &type) < 0) {
> -		tnt_raise(ClientError, ER_CFG, "log",
> -			  diag_last_error(diag_get())->errmsg);
> -	}
> -
> -	if (say_check_init_str(log) == -1) {
> -
> -		diag_raise();
> -	}
> -
> -	if (type == SAY_LOGGER_SYSLOG) {
> -		struct say_syslog_opts opts;
> -		if (say_parse_syslog_opts(log, &opts) < 0) {
> -			if (diag_last_error(diag_get())->type ==
> -			    &type_IllegalParams) {
> -				tnt_raise(ClientError, ER_CFG, "log",
> -					  diag_last_error(diag_get())->errmsg);
> -			}
> -		}
> -		say_free_syslog_opts(&opts);
> -		diag_raise();
> -	}
>  
>  	const char *log_format = cfg_gets("log_format");
> -	enum say_format format = say_format_by_name(log_format);
> -	if (format == say_format_MAX)
> -		diag_set(ClientError, ER_CFG, "log_format",
> -			 "expected 'plain' or 'json'");
> -	if (type == SAY_LOGGER_SYSLOG && format == SF_JSON) {
> -		tnt_raise(ClientError, ER_ILLEGAL_PARAMS, "log, log_format");
> -	}
>  	int log_nonblock = cfg_getb("log_nonblock");
> -	if (log_nonblock == 1 && type == SAY_LOGGER_FILE) {
> -		tnt_raise(ClientError, ER_ILLEGAL_PARAMS, "log, log_nonblock");
> +	if (say_check_cfg(log, log_format, log_nonblock) < 0) {
> +		if (diag_last_error(diag_get())->type ==
> +		    &type_IllegalParams) {
> +			tnt_raise(ClientError, ER_CFG,
> +				  "log, log_format or log_nonblock",
> +				  diag_last_error(diag_get())->errmsg);

Hmm, no - an error message produced by this code doesn't look neat,
nor informative:

  Incorrect value for option 'log, log_format or log_nonblock': Illegal parametrs log, log_nonblock

I guess, I see now why all these checks sit in box.cc instead of being
isolated in say.c - for the sake of error reporting. Sorry I confused
you :-( If you don't have a better idea in mind, let's return to where
we started, i.e. to removal of say_check_init_str(). Since it's not
going to be used after you remove the call to it from box_check_say(),
please remove it from say.[hc] as well.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [tarantool-patches] [PATCH v2 2/2] Added strdup fail checks in say
  2018-07-19 10:54 ` [tarantool-patches] [PATCH v2 2/2] Added strdup fail checks in say Olga Arkhangelskaia
@ 2018-07-19 12:26   ` Vladimir Davydov
  2018-07-19 13:12   ` Vladimir Davydov
  1 sibling, 0 replies; 5+ messages in thread
From: Vladimir Davydov @ 2018-07-19 12:26 UTC (permalink / raw)
  To: Olga Arkhangelskaia; +Cc: tarantool-patches

On Thu, Jul 19, 2018 at 01:54:49PM +0300, Olga Arkhangelskaia wrote:
> Strdup may silently fail without any message from tarantool.
> Patch adds this checks.
> ---
> Branch: https://github.com/tarantool/tarantool/tree/OKriw/refactor-log-cfg
> 
> v1:
> https://www.freelists.org/post/tarantool-patches/PATCH-22-Added-strdup-fail-checks-in-say
> 
> Changes in v2:
> - no double check now
> 
>  src/say.c | 5 +++++
>  1 file changed, 5 insertions(+)

This one looks good to me.

> 
> diff --git a/src/say.c b/src/say.c
> index 92e0e6d9f..99344b685 100644
> --- a/src/say.c
> +++ b/src/say.c
> @@ -502,6 +502,11 @@ log_syslog_init(struct log *log, const char *init_str)
>  		log->syslog_ident = strdup("tarantool");
>  	else
>  		log->syslog_ident = strdup(opts.identity);
> +	if (log->syslog_ident == NULL) {
> +		diag_set(OutOfMemory, strlen(opts.identity), "malloc",
> +		         "log->syslog_ident");
> +		return -1;
> +	}
>  
>  	if (opts.facility == syslog_facility_MAX)
>  		log->syslog_facility = SYSLOG_LOCAL7;

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [tarantool-patches] [PATCH v2 2/2] Added strdup fail checks in say
  2018-07-19 10:54 ` [tarantool-patches] [PATCH v2 2/2] Added strdup fail checks in say Olga Arkhangelskaia
  2018-07-19 12:26   ` Vladimir Davydov
@ 2018-07-19 13:12   ` Vladimir Davydov
  1 sibling, 0 replies; 5+ messages in thread
From: Vladimir Davydov @ 2018-07-19 13:12 UTC (permalink / raw)
  To: Olga Arkhangelskaia; +Cc: tarantool-patches

Pushed to 1.9

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2018-07-19 13:12 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-07-19 10:54 [tarantool-patches] [PATCH v2 1/2] Refactoring in string validation of log parameters Olga Arkhangelskaia
2018-07-19 10:54 ` [tarantool-patches] [PATCH v2 2/2] Added strdup fail checks in say Olga Arkhangelskaia
2018-07-19 12:26   ` Vladimir Davydov
2018-07-19 13:12   ` Vladimir Davydov
2018-07-19 12:24 ` [tarantool-patches] [PATCH v2 1/2] Refactoring in string validation of log parameters Vladimir Davydov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox