mirror of
https://gitlab.torproject.org/tpo/core/tor.git
synced 2025-02-23 22:47:12 +01:00
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
This commit is contained in:
parent
41fdaf01cf
commit
09586ef712
1 changed files with 18 additions and 4 deletions
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue