From 0fa4308c1a12fd3867c865429fa52d329e420a90 Mon Sep 17 00:00:00 2001 From: rfm Date: Sat, 29 May 2010 08:05:03 +0000 Subject: [PATCH] attempt fix for bug 29920 git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@30471 72102866-910b-0410-8b05-ffd578937521 --- ChangeLog | 4 ++++ Source/NSData.m | 32 ++++++++++++++++++++++++-------- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/ChangeLog b/ChangeLog index 85fa7dd00..25ee5e4bf 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +2010-05-29 Richard Frith-Macdonald + + * NSData.m: Attempted fix for bug #29920 + 2010-05-29 Richard Frith-Macdonald * NSConcreteHashTable.m: diff --git a/Source/NSData.m b/Source/NSData.m index f0abd0b73..8c56fe21c 100644 --- a/Source/NSData.m +++ b/Source/NSData.m @@ -169,7 +169,7 @@ readContentsOfFile(NSString* path, void** buf, unsigned int* len, NSZone* zone) NSDebugFLog(@"Open (%@) attempt failed - %@", path, [NSError _last]); goto failure; } - + /* * Seek to the end of the file. */ @@ -203,7 +203,8 @@ readContentsOfFile(NSString* path, void** buf, unsigned int* len, NSZone* zone) [NSError _last]); goto failure; } - + + clearerr(theFile); if (fileLength == 0) { unsigned char buf[BUFSIZ]; @@ -246,6 +247,8 @@ readContentsOfFile(NSString* path, void** buf, unsigned int* len, NSZone* zone) } else { + long offset = 0; + #if GS_WITH_GC tmp = NSAllocateCollectable(fileLength, 0); #else @@ -258,15 +261,28 @@ readContentsOfFile(NSString* path, void** buf, unsigned int* len, NSZone* zone) goto failure; } - c = fread(tmp, 1, fileLength, theFile); - if (c != (int)fileLength) + while (offset < fileLength + && (c = fread(tmp + offset, 1, fileLength - offset, theFile)) != 0) { - NSWarnFLog(@"read of file (%@) contents failed - %@", path, - [NSError _last]); - goto failure; + offset += c; + } + if (offset < fileLength) + { + fileLength = offset; +#if GS_WITH_GC + tmp = NSReallocateCollectable(tmp, fileLength, 0); +#else + tmp = NSZoneRealloc(zone, tmp, fileLength); +#endif } } - + if (ferror(theFile)) + { + NSWarnFLog(@"read of file (%@) contents failed - %@", path, + [NSError _last]); + goto failure; + } + *buf = tmp; *len = fileLength; fclose(theFile);