Fix coverity defects: CID 147692, 147693, 147694

CID:147692, Type:Uninitialized scalar variable
CID:147693, Type:Uninitialized scalar variable
CID:147694, Type:Uninitialized scalar variable

Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Signed-off-by: cao.xuewen <cao.xuewen@zte.com.cn>
Closes #5252
This commit is contained in:
cao 2016-10-14 05:38:59 +08:00 committed by Brian Behlendorf
parent 3f93077b02
commit a85a90557d
3 changed files with 30 additions and 11 deletions

View File

@ -64,7 +64,7 @@ main(int argc, char **argv)
offset_t llseek_ret = 0; offset_t llseek_ret = 0;
int write_ret = 0; int write_ret = 0;
int err = 0; int err = 0;
char mybuf[5]; char mybuf[5] = "aaaa\0";
char *testfile; char *testfile;
mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
@ -78,30 +78,31 @@ main(int argc, char **argv)
fd = open(testfile, O_CREAT | O_RDWR, mode); fd = open(testfile, O_CREAT | O_RDWR, mode);
if (fd < 0) { if (fd < 0) {
perror("Failed to create testfile");
err = errno; err = errno;
goto out; perror("Failed to create testfile");
free(testfile);
return (err);
} }
llseek_ret = lseek64(fd, offset, SEEK_SET); llseek_ret = lseek64(fd, offset, SEEK_SET);
if (llseek_ret < 0) { if (llseek_ret < 0) {
perror("Failed to seek to end of testfile");
err = errno; err = errno;
perror("Failed to seek to end of testfile");
goto out; goto out;
} }
write_ret = write(fd, mybuf, 1); write_ret = write(fd, mybuf, 1);
if (write_ret < 0) { if (write_ret < 0) {
perror("Failed to write to end of file");
err = errno; err = errno;
perror("Failed to write to end of file");
goto out; goto out;
} }
offset = 0; offset = 0;
llseek_ret = lseek64(fd, offset, SEEK_CUR); llseek_ret = lseek64(fd, offset, SEEK_CUR);
if (llseek_ret < 0) { if (llseek_ret < 0) {
perror("Failed to seek to end of file");
err = errno; err = errno;
perror("Failed to seek to end of file");
goto out; goto out;
} }
@ -111,8 +112,8 @@ main(int argc, char **argv)
(void) printf("write errno=EFBIG: success\n"); (void) printf("write errno=EFBIG: success\n");
err = 0; err = 0;
} else { } else {
perror("Did not receive EFBIG");
err = errno; err = errno;
perror("Did not receive EFBIG");
} }
} else { } else {
(void) printf("write completed successfully, test failed\n"); (void) printf("write completed successfully, test failed\n");
@ -122,6 +123,7 @@ main(int argc, char **argv)
out: out:
(void) unlink(testfile); (void) unlink(testfile);
free(testfile); free(testfile);
close(fd);
return (err); return (err);
} }

View File

@ -28,6 +28,7 @@
#include <fcntl.h> #include <fcntl.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include <sys/mman.h> #include <sys/mman.h>
#include <pthread.h> #include <pthread.h>
@ -67,9 +68,11 @@ int
main(int argc, char **argv) main(int argc, char **argv)
{ {
int fd; int fd;
char buf[BUFSIZ]; char buf[1024];
pthread_t tid; pthread_t tid;
memset(buf, 'a', sizeof (buf));
if (argc != 2) { if (argc != 2) {
(void) printf("usage: %s <file name>\n", argv[0]); (void) printf("usage: %s <file name>\n", argv[0]);
exit(1); exit(1);
@ -83,15 +86,19 @@ main(int argc, char **argv)
(void) pthread_setconcurrency(2); (void) pthread_setconcurrency(2);
if (pthread_create(&tid, NULL, mapper, &fd) != 0) { if (pthread_create(&tid, NULL, mapper, &fd) != 0) {
perror("pthread_create"); perror("pthread_create");
close(fd);
exit(1); exit(1);
} }
for (;;) { for (;;) {
if (write(fd, buf, sizeof (buf)) == -1) { if (write(fd, buf, sizeof (buf)) == -1) {
perror("write"); perror("write");
close(fd);
exit(1); exit(1);
} }
} }
close(fd);
/* NOTREACHED */ /* NOTREACHED */
return (0); return (0);
} }

View File

@ -32,6 +32,7 @@
#include <sys/types.h> #include <sys/types.h>
#include <unistd.h> #include <unistd.h>
#include <fcntl.h> #include <fcntl.h>
#include <string.h>
#include <linux/falloc.h> #include <linux/falloc.h>
/* /*
@ -54,7 +55,7 @@ int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
char *filename = NULL; char *filename = NULL;
char *buf; char *buf = NULL;
size_t filesize = 0; size_t filesize = 0;
off_t start_off = 0; off_t start_off = 0;
off_t off_len = 0; off_t off_len = 0;
@ -88,11 +89,18 @@ main(int argc, char *argv[])
return (1); return (1);
} }
buf = (char *)malloc(filesize); buf = (char *)calloc(1, filesize);
if (buf == NULL) {
perror("write");
close(fd);
return (1);
}
memset(buf, 'c', filesize);
if (write(fd, buf, filesize) < filesize) { if (write(fd, buf, filesize) < filesize) {
free(buf); free(buf);
perror("write"); perror("write");
close(fd);
return (1); return (1);
} }
@ -102,14 +110,16 @@ main(int argc, char *argv[])
if (fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, if (fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
start_off, off_len) < 0) { start_off, off_len) < 0) {
perror("fallocate"); perror("fallocate");
close(fd);
return (1); return (1);
} }
#else /* !(defined(FALLOC_FL_PUNCH_HOLE) && defined(FALLOC_FL_KEEP_SIZE)) */ #else /* !(defined(FALLOC_FL_PUNCH_HOLE) && defined(FALLOC_FL_KEEP_SIZE)) */
{ {
perror("FALLOC_FL_PUNCH_HOLE unsupported"); perror("FALLOC_FL_PUNCH_HOLE unsupported");
close(fd);
return (1); return (1);
} }
#endif /* defined(FALLOC_FL_PUNCH_HOLE) && defined(FALLOC_FL_KEEP_SIZE) */ #endif /* defined(FALLOC_FL_PUNCH_HOLE) && defined(FALLOC_FL_KEEP_SIZE) */
close(fd);
return (0); return (0);
} }