diff mbox

[1/3] xfs_scrub: synchronize error levels & logging

Message ID 3b8c1fd3-46bb-39ea-fca9-63d308cbe6b4@redhat.com (mailing list archive)
State Superseded
Headers show

Commit Message

Eric Sandeen March 27, 2018, 10:08 p.m. UTC
Having only a subset of the five error_levels present in
the log_level[] array is asking for trouble when someone
tries to __str_log(S_PREEN ...) and overruns the array.

Tie it all together in a single structure that's
initialized together to make the mapping more obvious and
idiot-proof.

Fixes-coverity-id: 1433618
Signed-off-by: Eric Sandeen <sandeen@redhat.com>
---

--
To unsubscribe from this list: send the line "unsubscribe linux-xfs" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Comments

Darrick J. Wong March 27, 2018, 10:14 p.m. UTC | #1
On Tue, Mar 27, 2018 at 05:08:54PM -0500, Eric Sandeen wrote:
> Having only a subset of the five error_levels present in
> the log_level[] array is asking for trouble when someone
> tries to __str_log(S_PREEN ...) and overruns the array.
> 
> Tie it all together in a single structure that's
> initialized together to make the mapping more obvious and
> idiot-proof.
> 
> Fixes-coverity-id: 1433618
> Signed-off-by: Eric Sandeen <sandeen@redhat.com>
> ---
> 
> diff --git a/scrub/common.c b/scrub/common.c
> index 5a37a98..02c6c9d 100644
> --- a/scrub/common.c
> +++ b/scrub/common.c
> @@ -56,18 +56,15 @@ xfs_scrub_excessive_errors(
>  	return ret;
>  }
>  
> -static const char *err_str[] = {
> -	[S_ERROR]	= "Error",
> -	[S_WARN]	= "Warning",
> -	[S_REPAIR]	= "Repaired",
> -	[S_INFO]	= "Info",
> -	[S_PREEN]	= "Optimized",
> -};
> -
> -static int log_level[] = {
> -	[S_ERROR]	= LOG_ERR,
> -	[S_WARN]	= LOG_WARNING,
> -	[S_INFO]	= LOG_INFO,
> +static struct {
> +	const char *string;
> +	int loglevel;
> +} err_levels[] = {
> +	[S_ERROR]  = { .string = "Error",	.loglevel = LOG_ERR },
> +	[S_WARN]   = { .string = "Warning",	.loglevel = LOG_WARNING },
> +	[S_REPAIR] = { .string = "Repaired",	.loglevel = LOG_WARNING },
> +	[S_INFO]   = { .string = "Info",	.loglevel = LOG_INFO },
> +	[S_PREEN]  = { .string = "Optimized",	.loglevel = LOG_INFO }
>  };
>  
>  /* If stream is a tty, clear to end of line to clean up progress bar. */
> @@ -106,8 +103,8 @@ __str_out(
>  	if (level == S_PREEN && !debug && !verbose)
>  		goto out_record;
>  
> -	fprintf(stream, "%s%s: %s: ", stream_start(stream), _(err_str[level]),
> -			descr);
> +	fprintf(stream, "%s%s: %s: ", stream_start(stream),
> +			err_levels[level].string, descr);

_(err_levels[level]) here ^^^^^^^^^^^^

Otherwise we end up with:

Error: 你吃饭了吗?你多吃一点。

>  	if (error) {
>  		fprintf(stream, _("%s."), strerror_r(error, buf, DESCR_BUFSZ));
>  	} else {
> @@ -152,10 +149,6 @@ __str_log(
>  	char			buf[LOG_BUFSZ];
>  	int			sz;
>  
> -	/* We only want to hear about optimizing when in debug/verbose mode. */
> -	if (level == S_PREEN && !debug && !verbose)
> -		return;

Why is this being removed?

--D

> -
>  	/*
>  	 * Skip logging if we're being run as a service (presumably the
>  	 * service will log stdout/stderr); if we're being run in a non
> @@ -168,11 +161,11 @@ __str_log(
>  	snprintf(logname, LOGNAME_BUFSZ, "%s@%s", progname, ctx->mntpoint);
>  	openlog(logname, LOG_PID, LOG_DAEMON);
>  
> -	sz = snprintf(buf, LOG_BUFSZ, "%s: ", _(err_str[level]));
> +	sz = snprintf(buf, LOG_BUFSZ, "%s: ", _(err_levels[level].string));
>  	va_start(args, format);
>  	vsnprintf(buf + sz, LOG_BUFSZ - sz, format, args);
>  	va_end(args);
> -	syslog(log_level[level], "%s", buf);
> +	syslog(err_levels[level].loglevel, "%s", buf);
>  
>  	closelog();
>  }
> --
> To unsubscribe from this list: send the line "unsubscribe linux-xfs" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
--
To unsubscribe from this list: send the line "unsubscribe linux-xfs" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Eric Sandeen March 27, 2018, 10:20 p.m. UTC | #2
On 3/27/18 5:14 PM, Darrick J. Wong wrote:
> On Tue, Mar 27, 2018 at 05:08:54PM -0500, Eric Sandeen wrote:
>> Having only a subset of the five error_levels present in
>> the log_level[] array is asking for trouble when someone
>> tries to __str_log(S_PREEN ...) and overruns the array.
>>
>> Tie it all together in a single structure that's
>> initialized together to make the mapping more obvious and
>> idiot-proof.
>>
>> Fixes-coverity-id: 1433618
>> Signed-off-by: Eric Sandeen <sandeen@redhat.com>
>> ---
>>
>> diff --git a/scrub/common.c b/scrub/common.c
>> index 5a37a98..02c6c9d 100644
>> --- a/scrub/common.c
>> +++ b/scrub/common.c
>> @@ -56,18 +56,15 @@ xfs_scrub_excessive_errors(
>>  	return ret;
>>  }
>>  
>> -static const char *err_str[] = {
>> -	[S_ERROR]	= "Error",
>> -	[S_WARN]	= "Warning",
>> -	[S_REPAIR]	= "Repaired",
>> -	[S_INFO]	= "Info",
>> -	[S_PREEN]	= "Optimized",
>> -};
>> -
>> -static int log_level[] = {
>> -	[S_ERROR]	= LOG_ERR,
>> -	[S_WARN]	= LOG_WARNING,
>> -	[S_INFO]	= LOG_INFO,
>> +static struct {
>> +	const char *string;
>> +	int loglevel;
>> +} err_levels[] = {
>> +	[S_ERROR]  = { .string = "Error",	.loglevel = LOG_ERR },
>> +	[S_WARN]   = { .string = "Warning",	.loglevel = LOG_WARNING },
>> +	[S_REPAIR] = { .string = "Repaired",	.loglevel = LOG_WARNING },
>> +	[S_INFO]   = { .string = "Info",	.loglevel = LOG_INFO },
>> +	[S_PREEN]  = { .string = "Optimized",	.loglevel = LOG_INFO }
>>  };
>>  
>>  /* If stream is a tty, clear to end of line to clean up progress bar. */
>> @@ -106,8 +103,8 @@ __str_out(
>>  	if (level == S_PREEN && !debug && !verbose)
>>  		goto out_record;
>>  
>> -	fprintf(stream, "%s%s: %s: ", stream_start(stream), _(err_str[level]),
>> -			descr);
>> +	fprintf(stream, "%s%s: %s: ", stream_start(stream),
>> +			err_levels[level].string, descr);
> 
> _(err_levels[level]) here ^^^^^^^^^^^^
> 
> Otherwise we end up with:
> 
> Error: 你吃饭了吗?你多吃一点。

oops

but: _(err_levels[level].string) right ...

> 
>>  	if (error) {
>>  		fprintf(stream, _("%s."), strerror_r(error, buf, DESCR_BUFSZ));
>>  	} else {
>> @@ -152,10 +149,6 @@ __str_log(
>>  	char			buf[LOG_BUFSZ];
>>  	int			sz;
>>  
>> -	/* We only want to hear about optimizing when in debug/verbose mode. */
>> -	if (level == S_PREEN && !debug && !verbose)
>> -		return;
> 
> Why is this being removed?

It seemed like a cut & paste error originally but ... it's a leftover, sorry. 

-Eric
--
To unsubscribe from this list: send the line "unsubscribe linux-xfs" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Darrick J. Wong March 27, 2018, 10:22 p.m. UTC | #3
On Tue, Mar 27, 2018 at 05:20:36PM -0500, Eric Sandeen wrote:
> On 3/27/18 5:14 PM, Darrick J. Wong wrote:
> > On Tue, Mar 27, 2018 at 05:08:54PM -0500, Eric Sandeen wrote:
> >> Having only a subset of the five error_levels present in
> >> the log_level[] array is asking for trouble when someone
> >> tries to __str_log(S_PREEN ...) and overruns the array.
> >>
> >> Tie it all together in a single structure that's
> >> initialized together to make the mapping more obvious and
> >> idiot-proof.
> >>
> >> Fixes-coverity-id: 1433618
> >> Signed-off-by: Eric Sandeen <sandeen@redhat.com>
> >> ---
> >>
> >> diff --git a/scrub/common.c b/scrub/common.c
> >> index 5a37a98..02c6c9d 100644
> >> --- a/scrub/common.c
> >> +++ b/scrub/common.c
> >> @@ -56,18 +56,15 @@ xfs_scrub_excessive_errors(
> >>  	return ret;
> >>  }
> >>  
> >> -static const char *err_str[] = {
> >> -	[S_ERROR]	= "Error",
> >> -	[S_WARN]	= "Warning",
> >> -	[S_REPAIR]	= "Repaired",
> >> -	[S_INFO]	= "Info",
> >> -	[S_PREEN]	= "Optimized",
> >> -};
> >> -
> >> -static int log_level[] = {
> >> -	[S_ERROR]	= LOG_ERR,
> >> -	[S_WARN]	= LOG_WARNING,
> >> -	[S_INFO]	= LOG_INFO,
> >> +static struct {
> >> +	const char *string;
> >> +	int loglevel;
> >> +} err_levels[] = {
> >> +	[S_ERROR]  = { .string = "Error",	.loglevel = LOG_ERR },
> >> +	[S_WARN]   = { .string = "Warning",	.loglevel = LOG_WARNING },
> >> +	[S_REPAIR] = { .string = "Repaired",	.loglevel = LOG_WARNING },
> >> +	[S_INFO]   = { .string = "Info",	.loglevel = LOG_INFO },
> >> +	[S_PREEN]  = { .string = "Optimized",	.loglevel = LOG_INFO }
> >>  };
> >>  
> >>  /* If stream is a tty, clear to end of line to clean up progress bar. */
> >> @@ -106,8 +103,8 @@ __str_out(
> >>  	if (level == S_PREEN && !debug && !verbose)
> >>  		goto out_record;
> >>  
> >> -	fprintf(stream, "%s%s: %s: ", stream_start(stream), _(err_str[level]),
> >> -			descr);
> >> +	fprintf(stream, "%s%s: %s: ", stream_start(stream),
> >> +			err_levels[level].string, descr);
> > 
> > _(err_levels[level]) here ^^^^^^^^^^^^
> > 
> > Otherwise we end up with:
> > 
> > Error: 你吃饭了吗?你多吃一点。
> 
> oops
> 
> but: _(err_levels[level].string) right ...

Er, right.

> > 
> >>  	if (error) {
> >>  		fprintf(stream, _("%s."), strerror_r(error, buf, DESCR_BUFSZ));
> >>  	} else {
> >> @@ -152,10 +149,6 @@ __str_log(
> >>  	char			buf[LOG_BUFSZ];
> >>  	int			sz;
> >>  
> >> -	/* We only want to hear about optimizing when in debug/verbose mode. */
> >> -	if (level == S_PREEN && !debug && !verbose)
> >> -		return;
> > 
> > Why is this being removed?
> 
> It seemed like a cut & paste error originally but ... it's a leftover, sorry. 

:)

--D

> -Eric
> --
> To unsubscribe from this list: send the line "unsubscribe linux-xfs" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
--
To unsubscribe from this list: send the line "unsubscribe linux-xfs" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/scrub/common.c b/scrub/common.c
index 5a37a98..02c6c9d 100644
--- a/scrub/common.c
+++ b/scrub/common.c
@@ -56,18 +56,15 @@  xfs_scrub_excessive_errors(
 	return ret;
 }
 
-static const char *err_str[] = {
-	[S_ERROR]	= "Error",
-	[S_WARN]	= "Warning",
-	[S_REPAIR]	= "Repaired",
-	[S_INFO]	= "Info",
-	[S_PREEN]	= "Optimized",
-};
-
-static int log_level[] = {
-	[S_ERROR]	= LOG_ERR,
-	[S_WARN]	= LOG_WARNING,
-	[S_INFO]	= LOG_INFO,
+static struct {
+	const char *string;
+	int loglevel;
+} err_levels[] = {
+	[S_ERROR]  = { .string = "Error",	.loglevel = LOG_ERR },
+	[S_WARN]   = { .string = "Warning",	.loglevel = LOG_WARNING },
+	[S_REPAIR] = { .string = "Repaired",	.loglevel = LOG_WARNING },
+	[S_INFO]   = { .string = "Info",	.loglevel = LOG_INFO },
+	[S_PREEN]  = { .string = "Optimized",	.loglevel = LOG_INFO }
 };
 
 /* If stream is a tty, clear to end of line to clean up progress bar. */
@@ -106,8 +103,8 @@  __str_out(
 	if (level == S_PREEN && !debug && !verbose)
 		goto out_record;
 
-	fprintf(stream, "%s%s: %s: ", stream_start(stream), _(err_str[level]),
-			descr);
+	fprintf(stream, "%s%s: %s: ", stream_start(stream),
+			err_levels[level].string, descr);
 	if (error) {
 		fprintf(stream, _("%s."), strerror_r(error, buf, DESCR_BUFSZ));
 	} else {
@@ -152,10 +149,6 @@  __str_log(
 	char			buf[LOG_BUFSZ];
 	int			sz;
 
-	/* We only want to hear about optimizing when in debug/verbose mode. */
-	if (level == S_PREEN && !debug && !verbose)
-		return;
-
 	/*
 	 * Skip logging if we're being run as a service (presumably the
 	 * service will log stdout/stderr); if we're being run in a non
@@ -168,11 +161,11 @@  __str_log(
 	snprintf(logname, LOGNAME_BUFSZ, "%s@%s", progname, ctx->mntpoint);
 	openlog(logname, LOG_PID, LOG_DAEMON);
 
-	sz = snprintf(buf, LOG_BUFSZ, "%s: ", _(err_str[level]));
+	sz = snprintf(buf, LOG_BUFSZ, "%s: ", _(err_levels[level].string));
 	va_start(args, format);
 	vsnprintf(buf + sz, LOG_BUFSZ - sz, format, args);
 	va_end(args);
-	syslog(log_level[level], "%s", buf);
+	syslog(err_levels[level].loglevel, "%s", buf);
 
 	closelog();
 }