qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [Qemu-devel] [PATCH] qemu-io: reject invalid pattern
@ 2009-07-19 23:19 Christoph Hellwig
  2009-07-20  8:05 ` Kevin Wolf
  0 siblings, 1 reply; 3+ messages in thread
From: Christoph Hellwig @ 2009-07-19 23:19 UTC (permalink / raw)
  To: qemu-devel

Replace the use of atoi which is used for pattern parsing currently with
strtol.  Atoi won't parse sedecimal pattern values (it always returns 0),
but qemu-iotests use such pattern values.  Also reject every pattern
that is not a unsigned char as we pass the pattern to memset which
expect a bye value (despite having the pattern argument declared as int).

Based on an earlier patch by Stefan Weil which did not include the
error handling.


Signed-off-by: Christoph Hellwig <hch@lst.de>
Reported-by: Stefan Weil <weil@mail.berlios.de>

Index: qemu/qemu-io.c
===================================================================
--- qemu.orig/qemu-io.c	2009-07-20 00:59:41.616922630 +0200
+++ qemu/qemu-io.c	2009-07-20 01:06:23.824900811 +0200
@@ -26,6 +26,26 @@ static BlockDriverState *bs;
 static int misalign;
 
 /*
+ * Parse the pattern argument to various sub-commands.
+ *
+ * Because the pattern is used as an argument to memset it must evaluate
+ * to an unsigned integer that fits into a single byte.
+ */
+static int parse_pattern(const char *arg)
+{
+	char *endptr = NULL;
+	long pattern;
+
+	pattern = strtol(arg, &endptr, 0);
+	if (pattern < 0 || pattern > UCHAR_MAX || *endptr != '\0') {
+		printf("%s is not a valid pattern byte\n", arg);
+		return -1;
+	}
+
+	return pattern;
+}
+
+/*
  * Memory allocation helpers.
  *
  * Make sure memory is aligned by default, or purposefully misaligned if
@@ -304,7 +324,9 @@ read_f(int argc, char **argv)
 			break;
 		case 'P':
 			Pflag = 1;
-			pattern = atoi(optarg);
+			pattern = parse_pattern(optarg);
+			if (pattern < 0)
+				return 0;
 			break;
 		case 'q':
 			qflag = 1;
@@ -469,7 +491,9 @@ readv_f(int argc, char **argv)
 			break;
 		case 'P':
 			Pflag = 1;
-			pattern = atoi(optarg);
+			pattern = parse_pattern(optarg);
+			if (pattern < 0)
+				return 0;
 			break;
 		case 'q':
 			qflag = 1;
@@ -594,7 +618,9 @@ write_f(int argc, char **argv)
 			pflag = 1;
 			break;
 		case 'P':
-			pattern = atoi(optarg);
+			pattern = parse_pattern(optarg);
+			if (pattern < 0)
+				return 0;
 			break;
 		case 'q':
 			qflag = 1;
@@ -721,7 +747,9 @@ writev_f(int argc, char **argv)
 			qflag = 1;
 			break;
 		case 'P':
-			pattern = atoi(optarg);
+			pattern = parse_pattern(optarg);
+			if (pattern < 0)
+				return 0;
 			break;
 		default:
 			return command_usage(&writev_cmd);
@@ -895,7 +923,9 @@ aio_read_f(int argc, char **argv)
 			break;
 		case 'P':
 			ctx->Pflag = 1;
-			ctx->pattern = atoi(optarg);
+			ctx->pattern = parse_pattern(optarg);
+			if (ctx->pattern < 0)
+				return 0;
 			break;
 		case 'q':
 			ctx->qflag = 1;
@@ -995,7 +1025,9 @@ aio_write_f(int argc, char **argv)
 			ctx->qflag = 1;
 			break;
 		case 'P':
-			pattern = atoi(optarg);
+			pattern = parse_pattern(optarg);
+			if (pattern < 0)
+				return 0;
 			break;
 		default:
 			free(ctx);

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

* Re: [Qemu-devel] [PATCH] qemu-io: reject invalid pattern
  2009-07-19 23:19 [Qemu-devel] [PATCH] qemu-io: reject invalid pattern Christoph Hellwig
@ 2009-07-20  8:05 ` Kevin Wolf
  2009-07-20 12:33   ` Christoph Hellwig
  0 siblings, 1 reply; 3+ messages in thread
From: Kevin Wolf @ 2009-07-20  8:05 UTC (permalink / raw)
  To: Christoph Hellwig; +Cc: qemu-devel

Christoph Hellwig schrieb:
> Replace the use of atoi which is used for pattern parsing currently with
> strtol.  Atoi won't parse sedecimal pattern values (it always returns 0),
> but qemu-iotests use such pattern values.  Also reject every pattern
> that is not a unsigned char as we pass the pattern to memset which
> expect a bye value (despite having the pattern argument declared as int).
> 
> Based on an earlier patch by Stefan Weil which did not include the
> error handling.
> 
> 
> Signed-off-by: Christoph Hellwig <hch@lst.de>
> Reported-by: Stefan Weil <weil@mail.berlios.de>
> 
> Index: qemu/qemu-io.c
> ===================================================================
> --- qemu.orig/qemu-io.c	2009-07-20 00:59:41.616922630 +0200
> +++ qemu/qemu-io.c	2009-07-20 01:06:23.824900811 +0200
> @@ -26,6 +26,26 @@ static BlockDriverState *bs;
>  static int misalign;
>  
>  /*
> + * Parse the pattern argument to various sub-commands.
> + *
> + * Because the pattern is used as an argument to memset it must evaluate
> + * to an unsigned integer that fits into a single byte.

+ * Returns the pattern byte or -1 in case arg does not contain a valid
pattern

> + */
> +static int parse_pattern(const char *arg)
> +{
> +	char *endptr = NULL;
> +	long pattern;
> +
> +	pattern = strtol(arg, &endptr, 0);
> +	if (pattern < 0 || pattern > UCHAR_MAX || *endptr != '\0') {
> +		printf("%s is not a valid pattern byte\n", arg);
> +		return -1;
> +	}
> +
> +	return pattern;
> +}
> +
> +/*
>   * Memory allocation helpers.
>   *
>   * Make sure memory is aligned by default, or purposefully misaligned if
> @@ -304,7 +324,9 @@ read_f(int argc, char **argv)
>  			break;
>  		case 'P':
>  			Pflag = 1;
> -			pattern = atoi(optarg);
> +			pattern = parse_pattern(optarg);
> +			if (pattern < 0)
> +				return 0;

Coding style (braces).

Looks good to me otherwise.

Kevin

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

* Re: [Qemu-devel] [PATCH] qemu-io: reject invalid pattern
  2009-07-20  8:05 ` Kevin Wolf
@ 2009-07-20 12:33   ` Christoph Hellwig
  0 siblings, 0 replies; 3+ messages in thread
From: Christoph Hellwig @ 2009-07-20 12:33 UTC (permalink / raw)
  To: Kevin Wolf; +Cc: qemu-devel

On Mon, Jul 20, 2009 at 10:05:34AM +0200, Kevin Wolf wrote:
> > + * Because the pattern is used as an argument to memset it must evaluate
> > + * to an unsigned integer that fits into a single byte.
> 
> + * Returns the pattern byte or -1 in case arg does not contain a valid
> pattern
> 

> > +			pattern = parse_pattern(optarg);
> > +			if (pattern < 0)
> > +				return 0;
> 
> Coding style (braces).
> 
> Looks good to me otherwise.

Thanks, I've resent an updated version.

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

end of thread, other threads:[~2009-07-20 12:33 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-07-19 23:19 [Qemu-devel] [PATCH] qemu-io: reject invalid pattern Christoph Hellwig
2009-07-20  8:05 ` Kevin Wolf
2009-07-20 12:33   ` Christoph Hellwig

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).