memcheck: Handle Err_ReallocSizeZero in MC_(eq_Error)
[valgrind.git] / memcheck / tests / writev1.c
blobf537058e919a71546e4c39e0328aedc5b0c7d411
1 #include <stdio.h>
2 #include <sys/types.h>
3 #include <sys/uio.h>
4 #include <fcntl.h>
5 #include <errno.h>
6 #include <unistd.h>
7 #include <string.h>
9 #define K_1 8192
10 #define NBUFS 2
11 #define CHUNK K_1 /* single chunk */
12 #define MAX_IOVEC 2
13 #define DATA_FILE "writev_data_file"
15 static char buf1[K_1];
16 static char buf2[K_1];
17 static char *buf_list[NBUFS], f_name[]="writev_data_file";
18 static int fd;
20 struct iovec wr_iovec[MAX_IOVEC] = {
21 {(caddr_t)-1, CHUNK},
22 {(caddr_t)NULL, 0}
25 int main(void)
27 int nbytes;
29 /* Fill the buf_list[0] and buf_list[1] with 0 zeros */
30 buf_list[0] = buf1;
31 buf_list[1] = buf2;
32 memset(buf_list[0], 0, K_1);
33 memset(buf_list[1], 0, K_1);
35 if ((fd = open(f_name, O_WRONLY | O_CREAT, 0666)) < 0) {
36 fprintf(stderr, "open(2) failed: fname = %s, errno = %d\n",
37 f_name, errno);
38 return 1;
39 } else if ((nbytes = write(fd, buf_list[1], K_1)) != K_1) {
40 fprintf(stderr, "write(2) failed: nbytes = %d, errno = %d\n",
41 nbytes, errno);
42 return 1;
44 if (close(fd) < 0) {
45 fprintf(stderr, "close failed: errno = %d\n", errno);
46 return 1;
48 fprintf(stderr, "Test file created.\n");
49 if ((fd = open(f_name, O_RDWR, 0666)) < 0) {
50 fprintf(stderr, "open failed: fname = %s, errno = %d\n",
51 f_name, errno);
52 return 1;
55 lseek(fd, 0, 0);
56 if (writev(fd, wr_iovec, 2) < 0) {
57 if (errno == EFAULT)
58 fprintf(stderr, "Received EFAULT as expected\n");
59 else
60 fprintf(stderr, "Expected EFAULT, got %d\n", errno);
61 lseek(fd, K_1, 0);
62 if ((nbytes = read(fd, buf_list[0], CHUNK)) != 0)
63 fprintf(stderr, "Expected nbytes = 0, got %d\n", nbytes);
65 else
66 fprintf(stderr, "Error writev returned a positive value\n");
67 // Now check invalid vector count
68 if (writev(fd, wr_iovec, -1) < 0) {
69 if (errno == EINVAL)
70 fprintf(stderr, "Received EINVAL as expected\n");
71 else
72 fprintf(stderr, "expected errno = EINVAL, got %d\n", errno);
74 else
75 fprintf(stderr, "Error writev returned a positive value\n");
76 if (readv(fd, wr_iovec, -1) < 0) {
77 if (errno == EINVAL)
78 fprintf(stderr, "Received EINVAL as expected\n");
79 else
80 fprintf(stderr, "expected errno = EINVAL, got %d\n", errno);
82 else
83 fprintf(stderr, "Error readv returned a positive value\n");
85 // test with totally bogus iovec pointer
86 // see bugz 424012
87 if (writev(fd, (const struct iovec *)1, 1) < 0) {
88 if (errno == EFAULT)
89 fprintf(stderr, "Received EFAULT as expected\n");
90 else
91 fprintf(stderr, "Expected EFAULT, got %d\n", errno);
93 else
94 fprintf(stderr, "Error writev returned a positive value\n");
96 if (readv(fd, (const struct iovec *)1, 1) < 0) {
97 if (errno == EFAULT)
98 fprintf(stderr, "Received EFAULT as expected\n");
99 else
100 fprintf(stderr, "Expected EFAULT, got %d\n", errno);
102 else
103 fprintf(stderr, "Error readv returned a positive value\n");
105 unlink(f_name);
107 return 0;