Fix bug causing short reads of data

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@25016 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
rfm 2007-04-13 05:14:21 +00:00
parent b0c9f96b58
commit 33cba73847
2 changed files with 19 additions and 31 deletions

View file

@ -1,3 +1,8 @@
2007-04-13 Richard Frith-Macdonald <rfm@gnu.org>
* Source/GSFileHandle.m: ([-readDataOfLength:]) fix bug which could
cause short reads.
2007-04-12 Adam Fedor <fedor@gnu.org> 2007-04-12 Adam Fedor <fedor@gnu.org>
* Tool/HTMLLinker.m: Re-add. * Tool/HTMLLinker.m: Re-add.

View file

@ -1353,51 +1353,34 @@ NSString * const GSSOCKSRecvAddr = @"GSSOCKSRecvAddr";
{ {
NSMutableData *d; NSMutableData *d;
int got; int got;
char buf[READ_SIZE];
[self checkRead]; [self checkRead];
if (isNonBlocking == YES) if (isNonBlocking == YES)
{ {
[self setNonBlocking: NO]; [self setNonBlocking: NO];
} }
if (len <= 65536)
{
char *buf;
buf = NSZoneMalloc(NSDefaultMallocZone(), len); d = [NSMutableData dataWithCapacity: len < READ_SIZE ? len : READ_SIZE];
d = [NSMutableData dataWithBytesNoCopy: buf length: len]; do
got = [self read: [d mutableBytes] length: len]; {
if (got < 0) int chunk = len > sizeof(buf) ? sizeof(buf) : len;
got = [self read: buf length: chunk];
if (got > 0)
{
[d appendBytes: buf length: got];
len -= got;
}
else if (got < 0)
{ {
[NSException raise: NSFileHandleOperationException [NSException raise: NSFileHandleOperationException
format: @"unable to read from descriptor - %@", format: @"unable to read from descriptor - %@",
[NSError _last]]; [NSError _last]];
} }
[d setLength: got];
} }
else while (len > 0 && got > 0);
{
char buf[READ_SIZE];
d = [NSMutableData dataWithCapacity: 0];
do
{
int chunk = len > sizeof(buf) ? sizeof(buf) : len;
got = [self read: buf length: chunk];
if (got > 0)
{
[d appendBytes: buf length: got];
len -= got;
}
else if (got < 0)
{
[NSException raise: NSFileHandleOperationException
format: @"unable to read from descriptor - %@",
[NSError _last]];
}
}
while (len > 0 && got > 0);
}
return d; return d;
} }