From 10845eb6557719d912d47c99f9e5de3003d14c10 Mon Sep 17 00:00:00 2001 From: Richard Frith-MacDonald Date: Fri, 13 Apr 2007 05:16:36 +0000 Subject: [PATCH] backport bugfix for readDataOfLength: git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/branches/stable@25017 72102866-910b-0410-8b05-ffd578937521 --- ChangeLog | 5 +++++ Source/GSFileHandle.m | 45 ++++++++++++++----------------------------- 2 files changed, 19 insertions(+), 31 deletions(-) diff --git a/ChangeLog b/ChangeLog index d8d686f6f..c48419d26 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2007-04-13 Richard Frith-Macdonald + + * Source/GSFileHandle.m: ([-readDataOfLength:]) backport of fix of + bug which could cause short reads. + 2007-04-12 Adam Fedor * Documentation/coding-standards.texi: Add info section diff --git a/Source/GSFileHandle.m b/Source/GSFileHandle.m index ba5c4efb5..b5158f8ea 100644 --- a/Source/GSFileHandle.m +++ b/Source/GSFileHandle.m @@ -1353,51 +1353,34 @@ NSString * const GSSOCKSRecvAddr = @"GSSOCKSRecvAddr"; { NSMutableData *d; int got; + char buf[READ_SIZE]; [self checkRead]; if (isNonBlocking == YES) { [self setNonBlocking: NO]; } - if (len <= 65536) - { - char *buf; - buf = NSZoneMalloc(NSDefaultMallocZone(), len); - d = [NSMutableData dataWithBytesNoCopy: buf length: len]; - got = [self read: [d mutableBytes] length: len]; - if (got < 0) + d = [NSMutableData dataWithCapacity: len < READ_SIZE ? len : READ_SIZE]; + 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]]; } - [d setLength: got]; } - else - { - char buf[READ_SIZE]; + while (len > 0 && got > 0); - 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; }