diff options
author | Nick Mathewson <nickm@torproject.org> | 2004-09-26 16:51:31 +0000 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2004-09-26 16:51:31 +0000 |
commit | 09586ef71218a12dacffaf0e14cf7448705b6e57 (patch) | |
tree | 70f7ddc568053b2e7ac784595f5b5d620de0827c /src | |
parent | 41fdaf01cf678be07b9bfc9ee9e23f8c7731bf5f (diff) | |
download | tor-09586ef71218a12dacffaf0e14cf7448705b6e57.tar tor-09586ef71218a12dacffaf0e14cf7448705b6e57.tar.gz |
Make read_all distinguish between error and EOF; read_file_to_string should only check bytes read against st_size when in binary mode.
svn:r2375
Diffstat (limited to 'src')
-rw-r--r-- | src/common/util.c | 22 |
1 files changed, 18 insertions, 4 deletions
diff --git a/src/common/util.c b/src/common/util.c index 8a3e528b9..00a758779 100644 --- a/src/common/util.c +++ b/src/common/util.c @@ -1087,7 +1087,9 @@ int write_all(int fd, const char *buf, size_t count, int isSocket) { return count; } -/** Read <b>count</b> bytes from <b>fd</b> to <b>buf</b>. isSocket must be 1 if fd +/** Read from <b>fd</b> to <b>buf</b>, until we get <b>count</b> bytes + * or reach the end of the file. + * isSocket must be 1 if fd * was returned by socket() or accept(), and 0 if fd was returned by * open(). Return the number of bytes read, or -1 on error. Only use * if fd is a blocking fd. */ @@ -1100,8 +1102,10 @@ int read_all(int fd, char *buf, size_t count, int isSocket) { result = recv(fd, buf+numread, count-numread, 0); else result = read(fd, buf+numread, count-numread); - if(result<=0) + if(result<0) return -1; + else if (result == 0) + break; numread += result; } return count; @@ -1525,6 +1529,7 @@ char *read_file_to_str(const char *filename, int bin) { int fd; /* router file */ struct stat statbuf; char *string; + int r; tor_assert(filename); @@ -1541,10 +1546,19 @@ char *read_file_to_str(const char *filename, int bin) { string = tor_malloc(statbuf.st_size+1); - if(read_all(fd,string,statbuf.st_size,0) != statbuf.st_size) { + r = read_all(fd,string,statbuf.st_size,0); + if (r<0) { + log_fn(LOG_WARN,"Error reading from file '%s': %s", filename, + strerror(errno)); + tor_free(string); + close(fd); + return NULL; + } else if (bin && r != statbuf.st_size) { + /* If we're in binary mode, then we'd better have an exact match for + * size. Otherwise, win32 encoding may throw us off, and that's okay. */ log_fn(LOG_WARN,"Couldn't read all %ld bytes of file '%s'.", (long)statbuf.st_size,filename); - free(string); + tor_free(string); close(fd); return NULL; } |