Tarantool development patches archive
 help / color / mirror / Atom feed
* [tarantool-patches] [PATCH v2 0/2] Refactor in box and say modules.
@ 2018-07-17 15:15 Olga Arkhangelskaia
  2018-07-17 15:15 ` [tarantool-patches] [PATCH 1/2] Ractoring in string validation of log parametrs Olga Arkhangelskaia
  2018-07-17 15:15 ` [tarantool-patches] [PATCH 2/2] Added strdup fail checks in say Olga Arkhangelskaia
  0 siblings, 2 replies; 5+ messages in thread
From: Olga Arkhangelskaia @ 2018-07-17 15:15 UTC (permalink / raw)
  To: tarantool-patches; +Cc: Olga Arkhangelskaia

This is small refactor that deletes double parametr check and
throws notifications in case of strdup failure.

There is no ticket for this changes.

v2:
- added strdup checks
- Introduced new function say_check_cfg that encapsulates all checks.

Olga Arkhangelskaia (2):
  Ractoring in string validation of log parametrs
  Added strdup fail checks in say

 src/box/box.cc | 29 +----------------------------
 src/say.c      | 51 ++++++++++++++++++++++++++++++++++++---------------
 src/say.h      |  9 ++++-----
 3 files changed, 41 insertions(+), 48 deletions(-)

-- 
2.14.3 (Apple Git-98)

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

* [tarantool-patches] [PATCH 1/2] Ractoring in string validation of log parametrs
  2018-07-17 15:15 [tarantool-patches] [PATCH v2 0/2] Refactor in box and say modules Olga Arkhangelskaia
@ 2018-07-17 15:15 ` Olga Arkhangelskaia
  2018-07-18 12:18   ` Vladimir Davydov
  2018-07-17 15:15 ` [tarantool-patches] [PATCH 2/2] Added strdup fail checks in say Olga Arkhangelskaia
  1 sibling, 1 reply; 5+ messages in thread
From: Olga Arkhangelskaia @ 2018-07-17 15:15 UTC (permalink / raw)
  To: tarantool-patches; +Cc: Olga Arkhangelskaia

Removed unnecessary double check of log configuration string.
Instead all log configuration ckecks were moved to separate function.

v2:
- Introduced new function say_check_cfg that encapsulates all checks.
---
 src/box/box.cc | 29 +----------------------------
 src/say.c      | 36 +++++++++++++++++++++++-------------
 src/say.h      |  9 ++++-----
 3 files changed, 28 insertions(+), 46 deletions(-)

diff --git a/src/box/box.cc b/src/box/box.cc
index 9252e82c1..9ef923616 100644
--- a/src/box/box.cc
+++ b/src/box/box.cc
@@ -362,36 +362,9 @@ box_check_say()
 			  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);
-			}
-			diag_raise();
-		}
-		say_free_syslog_opts(&opts);
-	}
-
 	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");
-	}
+	say_check_cfg(log, log_format, log_nonblock, type);
 }
 
 static enum say_format
diff --git a/src/say.c b/src/say.c
index 43124083c..501095b3e 100644
--- a/src/say.c
+++ b/src/say.c
@@ -952,25 +952,35 @@ write_to_syslog(struct log *log, int total)
 /** Loggers }}} */
 
 /*
- * Init string parser(s)
+ * Checks logger configuration
  */
-
-int
-say_check_init_str(const char *str)
+void
+say_check_cfg(const char *log, const char *log_format,
+	      int log_nonblock, enum say_logger_type type)
 {
-	enum say_logger_type type;
-	if (say_parse_logger_type(&str, &type)) {
-		diag_set(IllegalParams, logger_syntax_reminder);
-		return -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);
+			diag_raise();
+		}
+	}
+
+	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");
+		diag_raise();
+	}
+	if (log_nonblock == 1 && type == SAY_LOGGER_FILE) {
+		diag_set(IllegalParams, "Illegal parametrs log, log_nonblock");
+		diag_raise();
 	}
-	return 0;
 }
 
 /**
diff --git a/src/say.h b/src/say.h
index ad3ba3417..88cf9bd78 100644
--- a/src/say.h
+++ b/src/say.h
@@ -371,12 +371,11 @@ 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;
  */
-int
-say_check_init_str(const char *str);
+void
+say_check_cfg(const char *log, const char *log_format,
+	           int log_nonblock, enum say_logger_type type);
 
 /* internals, for unit testing */
 
-- 
2.14.3 (Apple Git-98)

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

* [tarantool-patches] [PATCH 2/2] Added strdup fail checks in say
  2018-07-17 15:15 [tarantool-patches] [PATCH v2 0/2] Refactor in box and say modules Olga Arkhangelskaia
  2018-07-17 15:15 ` [tarantool-patches] [PATCH 1/2] Ractoring in string validation of log parametrs Olga Arkhangelskaia
@ 2018-07-17 15:15 ` Olga Arkhangelskaia
  2018-07-18 12:21   ` Vladimir Davydov
  1 sibling, 1 reply; 5+ messages in thread
From: Olga Arkhangelskaia @ 2018-07-17 15:15 UTC (permalink / raw)
  To: tarantool-patches; +Cc: Olga Arkhangelskaia

Strdup may silently fail without any message from tarantool.
Patch adds this checks.

v2:
there is no v1 for this changes
---
 src/say.c | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)

diff --git a/src/say.c b/src/say.c
index 501095b3e..d824e778b 100644
--- a/src/say.c
+++ b/src/say.c
@@ -498,10 +498,21 @@ log_syslog_init(struct log *log, const char *init_str)
 	if (say_parse_syslog_opts(init_str, &opts) < 0)
 		return -1;
 
-	if (opts.identity == NULL)
+	if (opts.identity == NULL) {
 		log->syslog_ident = strdup("tarantool");
-	else
+		if (log->syslog_ident == NULL) {
+			diag_set(OutOfMemory, strlen("tarantool"), "malloc",
+				 "log->syslog_ident");
+			return -1;
+		}
+	} 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 1/2] Ractoring in string validation of log parametrs
  2018-07-17 15:15 ` [tarantool-patches] [PATCH 1/2] Ractoring in string validation of log parametrs Olga Arkhangelskaia
@ 2018-07-18 12:18   ` Vladimir Davydov
  0 siblings, 0 replies; 5+ messages in thread
From: Vladimir Davydov @ 2018-07-18 12:18 UTC (permalink / raw)
  To: Olga Arkhangelskaia; +Cc: tarantool-patches

On Tue, Jul 17, 2018 at 06:15:20PM +0300, Olga Arkhangelskaia wrote:
> Removed unnecessary double check of log configuration string.
> Instead all log configuration ckecks were moved to separate function.
> 
> v2:
> - Introduced new function say_check_cfg that encapsulates all checks.
> ---
>  src/box/box.cc | 29 +----------------------------
>  src/say.c      | 36 +++++++++++++++++++++++-------------
>  src/say.h      |  9 ++++-----
>  3 files changed, 28 insertions(+), 46 deletions(-)
> 
> diff --git a/src/box/box.cc b/src/box/box.cc
> index 9252e82c1..9ef923616 100644
> --- a/src/box/box.cc
> +++ b/src/box/box.cc
> @@ -362,36 +362,9 @@ box_check_say()
>  			  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);
> -			}
> -			diag_raise();
> -		}
> -		say_free_syslog_opts(&opts);
> -	}
> -
>  	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");
> -	}
> +	say_check_cfg(log, log_format, log_nonblock, type);

Please fold the call to say_parse_logger_type() in say_check_cfg() as
well.

>  }
>  
>  static enum say_format
> diff --git a/src/say.c b/src/say.c
> index 43124083c..501095b3e 100644
> --- a/src/say.c
> +++ b/src/say.c
> @@ -952,25 +952,35 @@ write_to_syslog(struct log *log, int total)
>  /** Loggers }}} */
>  
>  /*
> - * Init string parser(s)
> + * Checks logger configuration
>   */
> -
> -int
> -say_check_init_str(const char *str)
> +void
> +say_check_cfg(const char *log, const char *log_format,
> +	      int log_nonblock, enum say_logger_type type)
>  {
> -	enum say_logger_type type;
> -	if (say_parse_logger_type(&str, &type)) {
> -		diag_set(IllegalParams, logger_syntax_reminder);
> -		return -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);
> +			diag_raise();

We don't raise exceptions from C files. Instead we return -1 after
diag_set(). Please move diag_raise() to box.cc.

Also, note box_check_say() should raise ClientError(ER_CFG), i.e. it
should convert IllegalParams error to ClientError, like here (this is
1.10):

https://github.com/tarantool/tarantool/blob/9f1e0f445d2c42d0901baa3eb084a57b036435e2/src/box/box.cc#L357

> +		}
> +	}
> +
> +	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");
> +		diag_raise();
> +	}
> +	if (log_nonblock == 1 && type == SAY_LOGGER_FILE) {
> +		diag_set(IllegalParams, "Illegal parametrs log, log_nonblock");
> +		diag_raise();
>  	}
> -	return 0;
>  }
>  
>  /**
> diff --git a/src/say.h b/src/say.h
> index ad3ba3417..88cf9bd78 100644
> --- a/src/say.h
> +++ b/src/say.h
> @@ -371,12 +371,11 @@ 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;

Nit: s/;/.

>   */
> -int
> -say_check_init_str(const char *str);
> +void
> +say_check_cfg(const char *log, const char *log_format,
> +	           int log_nonblock, enum say_logger_type type);
>  
>  /* internals, for unit testing */

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

* Re: [tarantool-patches] [PATCH 2/2] Added strdup fail checks in say
  2018-07-17 15:15 ` [tarantool-patches] [PATCH 2/2] Added strdup fail checks in say Olga Arkhangelskaia
@ 2018-07-18 12:21   ` Vladimir Davydov
  0 siblings, 0 replies; 5+ messages in thread
From: Vladimir Davydov @ 2018-07-18 12:21 UTC (permalink / raw)
  To: Olga Arkhangelskaia; +Cc: tarantool-patches

On Tue, Jul 17, 2018 at 06:15:21PM +0300, Olga Arkhangelskaia wrote:
> Strdup may silently fail without any message from tarantool.
> Patch adds this checks.
> 
> v2:
> there is no v1 for this changes
> ---
>  src/say.c | 15 +++++++++++++--
>  1 file changed, 13 insertions(+), 2 deletions(-)
> 
> diff --git a/src/say.c b/src/say.c
> index 501095b3e..d824e778b 100644
> --- a/src/say.c
> +++ b/src/say.c
> @@ -498,10 +498,21 @@ log_syslog_init(struct log *log, const char *init_str)
>  	if (say_parse_syslog_opts(init_str, &opts) < 0)
>  		return -1;
>  
> -	if (opts.identity == NULL)
> +	if (opts.identity == NULL) {
>  		log->syslog_ident = strdup("tarantool");
> -	else
> +		if (log->syslog_ident == NULL) {
> +			diag_set(OutOfMemory, strlen("tarantool"), "malloc",
> +				 "log->syslog_ident");
> +			return -1;
> +		}
> +	} else {
>  		log->syslog_ident = strdup(opts.identity);
> +		if (log->syslog_ident == NULL) {
> +			diag_set(OutOfMemory, strlen(opts.identity), "malloc",
> +				 "log->syslog_ident");
> +			return -1;
> +		}
> +	}

You can avoid code duplication by checking syslog_ident once after
if-else block:

	if (opts.identity == NULL)
		log->syslog_ident = ...
	else
		log->syslog_ident = ...
	if (log->syslog_ident) {
		diag_set(...)
		reutrn -1;
	}

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

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

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-07-17 15:15 [tarantool-patches] [PATCH v2 0/2] Refactor in box and say modules Olga Arkhangelskaia
2018-07-17 15:15 ` [tarantool-patches] [PATCH 1/2] Ractoring in string validation of log parametrs Olga Arkhangelskaia
2018-07-18 12:18   ` Vladimir Davydov
2018-07-17 15:15 ` [tarantool-patches] [PATCH 2/2] Added strdup fail checks in say Olga Arkhangelskaia
2018-07-18 12:21   ` Vladimir Davydov

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