@matheusbsbs / matheus-bagliano / commits / 93d26e4cb9
Fork 0

short i/o: fix calls to read to use xread or read_in_full

We have a number of badly checked read() calls. Often we are expecting read() to read exactly the size we requested or fail, this fails to handle interrupts or short reads. Add a read_in_full() providing those semantics. Otherwise we at a minimum need to check for EINTR and EAGAIN, where this is appropriate use xread(). Signed-off-by: Andy Whitcroft <apw@shadowen.org> Signed-off-by: Junio C Hamano <junkio@cox.net>

Andy Whitcroft committed Jan 8, 2007 at 15:58 UTC 93d26e4cb9cec2eb8abb4f37e6dda2c86fcceeac
17 files changed +57 -39
builtin-grep.c
+1 -1
@@ -136,7 +136,7 @@ static int grep_file(struct grep_opt *opt, const char *filename)
136 if (i < 0)
137 goto err_ret;
138 data = xmalloc(st.st_size + 1);
139 - if (st.st_size != xread(i, data, st.st_size)) {
139 + if (st.st_size != read_in_full(i, data, st.st_size)) {
140 error("'%s': short read %s", filename, strerror(errno));
141 close(i);
142 free(data);
builtin-tar-tree.c
+1 -1
@@ -74,7 +74,7 @@ int cmd_get_tar_commit_id(int argc, const char **argv, const char *prefix)
74 char *content = buffer + RECORDSIZE;
75 ssize_t n;
76
77 - n = xread(0, buffer, HEADERSIZE);
77 + n = read_in_full(0, buffer, HEADERSIZE);
78 if (n < HEADERSIZE)
79 die("git-get-tar-commit-id: read error");
80 if (header->typeflag[0] != 'g')
builtin-upload-archive.c
+1 -1
@@ -91,7 +91,7 @@ static void process_input(int child_fd, int band)
91 char buf[16384];
92 ssize_t sz = read(child_fd, buf, sizeof(buf));
93 if (sz < 0) {
94 - if (errno != EINTR)
94 + if (errno != EAGAIN && errno != EINTR)
95 error_clnt("read error: %s\n", strerror(errno));
96 return;
97 }
cache.h
+1
@@ -432,6 +432,7 @@ extern char *git_commit_encoding;
432 extern char *git_log_output_encoding;
433
434 extern int copy_fd(int ifd, int ofd);
435 +extern int read_in_full(int fd, void *buf, size_t count);
436 extern void read_or_die(int fd, void *buf, size_t count);
437 extern int write_in_full(int fd, const void *buf, size_t count);
438 extern void write_or_die(int fd, const void *buf, size_t count);
dir.c
+1 -1
@@ -142,7 +142,7 @@ static int add_excludes_from_file_1(const char *fname,
142 return 0;
143 }
144 buf = xmalloc(size+1);
145 - if (read(fd, buf, size) != size)
145 + if (read_in_full(fd, buf, size) != size)
146 goto err;
147 close(fd);
148
http-fetch.c
+1 -1
@@ -175,7 +175,7 @@ static void start_object_request(struct object_request *obj_req)
175 prevlocal = open(prevfile, O_RDONLY);
176 if (prevlocal != -1) {
177 do {
178 - prev_read = read(prevlocal, prev_buf, PREV_BUF_SIZE);
178 + prev_read = xread(prevlocal, prev_buf, PREV_BUF_SIZE);
179 if (prev_read>0) {
180 if (fwrite_sha1_file(prev_buf,
181 1,
http-push.c
+1 -1
@@ -288,7 +288,7 @@ static void start_fetch_loose(struct transfer_request *request)
288 prevlocal = open(prevfile, O_RDONLY);
289 if (prevlocal != -1) {
290 do {
291 - prev_read = read(prevlocal, prev_buf, PREV_BUF_SIZE);
291 + prev_read = xread(prevlocal, prev_buf, PREV_BUF_SIZE);
292 if (prev_read>0) {
293 if (fwrite_sha1_file(prev_buf,
294 1,
imap-send.c
+2 -2
@@ -224,7 +224,7 @@ socket_perror( const char *func, Socket_t *sock, int ret )
224 static int
225 socket_read( Socket_t *sock, char *buf, int len )
226 {
227 - int n = read( sock->fd, buf, len );
227 + int n = xread( sock->fd, buf, len );
228 if (n <= 0) {
229 socket_perror( "read", sock, n );
230 close( sock->fd );
@@ -390,7 +390,7 @@ arc4_init( void )
390 fprintf( stderr, "Fatal: no random number source available.\n" );
391 exit( 3 );
392 }
393 - if (read( fd, dat, 128 ) != 128) {
393 + if (read_in_full( fd, dat, 128 ) != 128) {
394 fprintf( stderr, "Fatal: cannot read random number source.\n" );
395 exit( 3 );
396 }
index-pack.c
+1 -1
@@ -638,7 +638,7 @@ static void readjust_pack_header_and_sha1(unsigned char *sha1)
638 /* Rewrite pack header with updated object number */
639 if (lseek(output_fd, 0, SEEK_SET) != 0)
640 die("cannot seek back: %s", strerror(errno));
641 - if (xread(output_fd, &hdr, sizeof(hdr)) != sizeof(hdr))
641 + if (read_in_full(output_fd, &hdr, sizeof(hdr)) != sizeof(hdr))
642 die("cannot read pack header back: %s", strerror(errno));
643 hdr.hdr_entries = htonl(nr_objects);
644 if (lseek(output_fd, 0, SEEK_SET) != 0)
local-fetch.c
+1 -1
@@ -184,7 +184,7 @@ int fetch_ref(char *ref, unsigned char *sha1)
184 fprintf(stderr, "cannot open %s\n", filename);
185 return -1;
186 }
187 - if (read(ifd, hex, 40) != 40 || get_sha1_hex(hex, sha1)) {
187 + if (read_in_full(ifd, hex, 40) != 40 || get_sha1_hex(hex, sha1)) {
188 close(ifd);
189 fprintf(stderr, "cannot read from %s\n", filename);
190 return -1;
path.c
+1 -1
@@ -113,7 +113,7 @@ int validate_symref(const char *path)
113 fd = open(path, O_RDONLY);
114 if (fd < 0)
115 return -1;
116 - len = read(fd, buffer, sizeof(buffer)-1);
116 + len = read_in_full(fd, buffer, sizeof(buffer)-1);
117 close(fd);
118
119 /*
refs.c
+1 -1
@@ -284,7 +284,7 @@ const char *resolve_ref(const char *ref, unsigned char *sha1, int reading, int *
284 fd = open(path, O_RDONLY);
285 if (fd < 0)
286 return NULL;
287 - len = read(fd, buffer, sizeof(buffer)-1);
287 + len = read_in_full(fd, buffer, sizeof(buffer)-1);
288 close(fd);
289
290 /*
sha1_file.c
+1 -1
@@ -1869,7 +1869,7 @@ int write_sha1_from_fd(const unsigned char *sha1, int fd, char *buffer,
1869 if (ret != Z_OK)
1870 break;
1871 }
1872 - size = read(fd, buffer + *bufposn, bufsize - *bufposn);
1872 + size = xread(fd, buffer + *bufposn, bufsize - *bufposn);
1873 if (size <= 0) {
1874 close(local);
1875 unlink(tmpfile);
ssh-fetch.c
+7 -4
@@ -82,7 +82,7 @@ int fetch(unsigned char *sha1)
82 remote = conn_buf[0];
83 memmove(conn_buf, conn_buf + 1, --conn_buf_posn);
84 } else {
85 - if (read(fd_in, &remote, 1) < 1)
85 + if (xread(fd_in, &remote, 1) < 1)
86 return -1;
87 }
88 /* fprintf(stderr, "Got %d\n", remote); */
@@ -99,7 +99,7 @@ static int get_version(void)
99 char type = 'v';
100 write(fd_out, &type, 1);
101 write(fd_out, &local_version, 1);
102 - if (read(fd_in, &remote_version, 1) < 1) {
102 + if (xread(fd_in, &remote_version, 1) < 1) {
103 return error("Couldn't read version from remote end");
104 }
105 return 0;
@@ -111,10 +111,13 @@ int fetch_ref(char *ref, unsigned char *sha1)
111 char type = 'r';
112 write(fd_out, &type, 1);
113 write(fd_out, ref, strlen(ref) + 1);
114 - read(fd_in, &remote, 1);
114 +
115 + if (read_in_full(fd_in, &remote, 1) != 1)
116 + return -1;
117 if (remote < 0)
118 return remote;
117 - read(fd_in, sha1, 20);
119 + if (read_in_full(fd_in, sha1, 20) != 20)
120 + return -1;
121 return 0;
122 }
123
ssh-upload.c
+11 -14
@@ -21,17 +21,14 @@ static int serve_object(int fd_in, int fd_out) {
21 ssize_t size;
22 unsigned char sha1[20];
23 signed char remote;
24 - int posn = 0;
25 - do {
26 - size = read(fd_in, sha1 + posn, 20 - posn);
27 - if (size < 0) {
28 - perror("git-ssh-upload: read ");
29 - return -1;
30 - }
31 - if (!size)
32 - return -1;
33 - posn += size;
34 - } while (posn < 20);
24 +
25 + size = read_in_full(fd_in, sha1, 20);
26 + if (size < 0) {
27 + perror("git-ssh-upload: read ");
28 + return -1;
29 + }
30 + if (!size)
31 + return -1;
32
33 if (verbose)
34 fprintf(stderr, "Serving %s\n", sha1_to_hex(sha1));
@@ -54,7 +51,7 @@ static int serve_object(int fd_in, int fd_out) {
51
52 static int serve_version(int fd_in, int fd_out)
53 {
57 - if (read(fd_in, &remote_version, 1) < 1)
54 + if (xread(fd_in, &remote_version, 1) < 1)
55 return -1;
56 write(fd_out, &local_version, 1);
57 return 0;
@@ -67,7 +64,7 @@ static int serve_ref(int fd_in, int fd_out)
64 int posn = 0;
65 signed char remote = 0;
66 do {
70 - if (posn >= PATH_MAX || read(fd_in, ref + posn, 1) < 1)
67 + if (posn >= PATH_MAX || xread(fd_in, ref + posn, 1) < 1)
68 return -1;
69 posn++;
70 } while (ref[posn - 1]);
@@ -89,7 +86,7 @@ static void service(int fd_in, int fd_out) {
86 char type;
87 int retval;
88 do {
92 - retval = read(fd_in, &type, 1);
89 + retval = xread(fd_in, &type, 1);
90 if (retval < 1) {
91 if (retval < 0)
92 perror("git-ssh-upload: read ");
upload-pack.c
+2 -2
@@ -242,7 +242,7 @@ static void create_pack_file(void)
242 *cp++ = buffered;
243 outsz++;
244 }
245 - sz = read(pu_pipe[0], cp,
245 + sz = xread(pu_pipe[0], cp,
246 sizeof(data) - outsz);
247 if (0 < sz)
248 ;
@@ -267,7 +267,7 @@ static void create_pack_file(void)
267 /* Status ready; we ship that in the side-band
268 * or dump to the standard error.
269 */
270 - sz = read(pe_pipe[0], progress,
270 + sz = xread(pe_pipe[0], progress,
271 sizeof(progress));
272 if (0 < sz)
273 send_client_data(2, progress, sz);
write_or_die.c
+23 -6
@@ -1,19 +1,36 @@
1 #include "cache.h"
2
3 -void read_or_die(int fd, void *buf, size_t count)
3 +int read_in_full(int fd, void *buf, size_t count)
4 {
5 char *p = buf;
6 - ssize_t loaded;
6 + ssize_t total = 0;
7 + ssize_t loaded = 0;
8
9 while (count > 0) {
10 loaded = xread(fd, p, count);
10 - if (loaded == 0)
11 - die("unexpected end of file");
12 - else if (loaded < 0)
13 - die("read error (%s)", strerror(errno));
11 + if (loaded <= 0) {
12 + if (total)
13 + return total;
14 + else
15 + return loaded;
16 + }
17 count -= loaded;
18 p += loaded;
19 + total += loaded;
20 }
21 +
22 + return total;
23 +}
24 +
25 +void read_or_die(int fd, void *buf, size_t count)
26 +{
27 + ssize_t loaded;
28 +
29 + loaded = read_in_full(fd, buf, count);
30 + if (loaded == 0)
31 + die("unexpected end of file");
32 + else if (loaded < 0)
33 + die("read error (%s)", strerror(errno));
34 }
35
36 void write_or_die(int fd, const void *buf, size_t count)