FS/XFS testing framework
 help / color / mirror / Atom feed
* [patch] resvtest.c: fix invalid use of sizeof()
@ 2014-06-05 19:26 Jeff Moyer
  2014-06-05 19:29 ` Alex Elder
  0 siblings, 1 reply; 2+ messages in thread
From: Jeff Moyer @ 2014-06-05 19:26 UTC (permalink / raw)
  To: fstests

Hi,

sizeof(pointer) will give you the size of a pointer, not the space
allocated to it.  I noticed this when gcc complained:

resvtest.c:76:33: warning: argument to 'sizeof' in 'memset' call is the same expression as the destination; did you mean to provide an explicit length? [-Wsizeof-pointer-memaccess]
  memset(writebuffer, 'A', sizeof(writebuffer));

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>

diff --git a/src/resvtest.c b/src/resvtest.c
index 037d9ea..ecad031 100644
--- a/src/resvtest.c
+++ b/src/resvtest.c
@@ -73,7 +73,7 @@ main(int argc, char **argv)
 		perror("open");
 		exit(1);
 	}
-	memset(writebuffer, 'A', sizeof(writebuffer));
+	memset(writebuffer, 'A', bsize);
 
 	unlink(filename);
 	writefd = open(filename, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
@@ -111,7 +111,7 @@ main(int argc, char **argv)
 		char *p;
 		int numerrors;
 
-		if (write(writefd, writebuffer, sizeof(writebuffer)) < 0) {
+		if (write(writefd, writebuffer, bsize) < 0) {
 			perror("write");
 			exit(1);
 		}
@@ -128,11 +128,11 @@ main(int argc, char **argv)
 		lseek(readfd, SEEK_SET, 0);
 		numerrors = 0;
 		for (j = 0; j < n; j++) {
-			if (read(readfd, readbuffer, sizeof(readbuffer)) < 0) {
+			if (read(readfd, readbuffer, bsize) < 0) {
 				perror("read");
 				exit(1);
 			}
-			for (i = 0; i < sizeof(readbuffer); i++) {
+			for (i = 0; i < bsize; i++) {
 				if (readbuffer[i] != 'A') {
 					fprintf(stderr,
 "errors detected in file, pos: %d (%lld+%d), nwrites: %d [val=0x%x].\n",

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

* Re: [patch] resvtest.c: fix invalid use of sizeof()
  2014-06-05 19:26 [patch] resvtest.c: fix invalid use of sizeof() Jeff Moyer
@ 2014-06-05 19:29 ` Alex Elder
  0 siblings, 0 replies; 2+ messages in thread
From: Alex Elder @ 2014-06-05 19:29 UTC (permalink / raw)
  To: Jeff Moyer, fstests

On 06/05/2014 02:26 PM, Jeff Moyer wrote:
> Hi,
> 
> sizeof(pointer) will give you the size of a pointer, not the space
> allocated to it.  I noticed this when gcc complained:
> 
> resvtest.c:76:33: warning: argument to 'sizeof' in 'memset' call is the same expression as the destination; did you mean to provide an explicit length? [-Wsizeof-pointer-memaccess]
>   memset(writebuffer, 'A', sizeof(writebuffer));

Nice catch, gcc!

Looks good.

Reviewed-by: Alex Elder <elder@linaro.org>

> Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
> 
> diff --git a/src/resvtest.c b/src/resvtest.c
> index 037d9ea..ecad031 100644
> --- a/src/resvtest.c
> +++ b/src/resvtest.c
> @@ -73,7 +73,7 @@ main(int argc, char **argv)
>  		perror("open");
>  		exit(1);
>  	}
> -	memset(writebuffer, 'A', sizeof(writebuffer));
> +	memset(writebuffer, 'A', bsize);
>  
>  	unlink(filename);
>  	writefd = open(filename, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
> @@ -111,7 +111,7 @@ main(int argc, char **argv)
>  		char *p;
>  		int numerrors;
>  
> -		if (write(writefd, writebuffer, sizeof(writebuffer)) < 0) {
> +		if (write(writefd, writebuffer, bsize) < 0) {
>  			perror("write");
>  			exit(1);
>  		}
> @@ -128,11 +128,11 @@ main(int argc, char **argv)
>  		lseek(readfd, SEEK_SET, 0);
>  		numerrors = 0;
>  		for (j = 0; j < n; j++) {
> -			if (read(readfd, readbuffer, sizeof(readbuffer)) < 0) {
> +			if (read(readfd, readbuffer, bsize) < 0) {
>  				perror("read");
>  				exit(1);
>  			}
> -			for (i = 0; i < sizeof(readbuffer); i++) {
> +			for (i = 0; i < bsize; i++) {
>  				if (readbuffer[i] != 'A') {
>  					fprintf(stderr,
>  "errors detected in file, pos: %d (%lld+%d), nwrites: %d [val=0x%x].\n",
> --
> To unsubscribe from this list: send the line "unsubscribe fstests" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
> 


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

end of thread, other threads:[~2014-06-05 19:29 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-06-05 19:26 [patch] resvtest.c: fix invalid use of sizeof() Jeff Moyer
2014-06-05 19:29 ` Alex Elder

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