Skip to contentSkip to conversation

resvtest.c: fix invalid use of sizeof()

PATCH In review Jeff Moyer 2 participants 2 messages Jun 5, 2014

lore
Share & tools
lore

Jeff Moyer

author
#0 lore raw

PATCH 1/1 resvtest.c: fix invalid use of sizeof()

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));

src/resvtest.c +4 -4
index 037d9ea..ecad031 100644
@@ -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",
Reviewed-by on v1 1/1
#1 lore raw
Quoted 7 lines · Jeff Moyer

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.

Quoted 43 lines
src/resvtest.c +4 -5
index 037d9ea..ecad031 100644
@@ -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 [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html