Add some argument checking

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@28454 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
rfm 2009-08-12 07:49:42 +00:00
parent 523ae77842
commit 2313de9e53
2 changed files with 69 additions and 74 deletions

View file

@ -1,3 +1,8 @@
2009-08-12 Richard Frith-Macdonald <rfm@gnu.org>
* Source/NSData.m: Remove some redundant methods and add checks for
attempts to use a null pointer to set bytes in the data.
2009-08-11 Fred Kiefer <FredKiefer@gmx.de> 2009-08-11 Fred Kiefer <FredKiefer@gmx.de>
* Source/NSAttributedString.m: Fix keyeded encodgin decoding for * Source/NSAttributedString.m: Fix keyeded encodgin decoding for

View file

@ -553,16 +553,22 @@ failure:
if (bufferSize > 0) if (bufferSize > 0)
{ {
if (aBuffer == 0)
{
[NSException raise: NSInvalidArgumentException
format: @"[%@-initWithBytes:length:] called with "
@"length but null bytes", NSStringFromClass([self class])];
}
#if GS_WITH_GC #if GS_WITH_GC
ptr = NSAllocateCollectable(bufferSize, 0); ptr = NSAllocateCollectable(bufferSize, 0);
#else #else
ptr = NSZoneMalloc(NSDefaultMallocZone(), bufferSize); ptr = NSZoneMalloc(NSDefaultMallocZone(), bufferSize);
#endif #endif
if (ptr == 0) if (ptr == 0)
{ {
DESTROY(self); DESTROY(self);
return nil; return nil;
} }
memcpy(ptr, aBuffer, bufferSize); memcpy(ptr, aBuffer, bufferSize);
} }
return [self initWithBytesNoCopy: ptr return [self initWithBytesNoCopy: ptr
@ -2494,22 +2500,11 @@ failure:
/* Creation and Destruction of objects. */ /* Creation and Destruction of objects. */
- (id) copy
{
return RETAIN(self);
}
- (id) copyWithZone: (NSZone*)z - (id) copyWithZone: (NSZone*)z
{ {
return RETAIN(self); return RETAIN(self);
} }
- (id) mutableCopy
{
return [[mutableDataMalloc allocWithZone: NSDefaultMallocZone()]
initWithBytes: bytes length: length];
}
- (id) mutableCopyWithZone: (NSZone*)z - (id) mutableCopyWithZone: (NSZone*)z
{ {
return [[mutableDataMalloc allocWithZone: z] return [[mutableDataMalloc allocWithZone: z]
@ -2527,6 +2522,12 @@ failure:
length: (NSUInteger)bufferSize length: (NSUInteger)bufferSize
freeWhenDone: (BOOL)shouldFree freeWhenDone: (BOOL)shouldFree
{ {
if (bytes == 0 && length > 0)
{
[NSException raise: NSInvalidArgumentException
format: @"[%@-initWithBytesNoCopy:length:freeWhenDone:] called with "
@"length but null bytes", NSStringFromClass([self class])];
}
bytes = aBuffer; bytes = aBuffer;
length = bufferSize; length = bufferSize;
return self; return self;
@ -2921,15 +2922,6 @@ getBytes(void* dst, void* src, unsigned len, unsigned limit, unsigned *pos)
@implementation NSDataMalloc @implementation NSDataMalloc
- (id) copy
{
if (NSShouldRetainWithZone(self, NSDefaultMallocZone()))
return RETAIN(self);
else
return [[dataMalloc allocWithZone: NSDefaultMallocZone()]
initWithBytes: bytes length: length];
}
- (id) copyWithZone: (NSZone*)z - (id) copyWithZone: (NSZone*)z
{ {
if (NSShouldRetainWithZone(self, z)) if (NSShouldRetainWithZone(self, z))
@ -2953,6 +2945,12 @@ getBytes(void* dst, void* src, unsigned len, unsigned limit, unsigned *pos)
length: (NSUInteger)bufferSize length: (NSUInteger)bufferSize
freeWhenDone: (BOOL)shouldFree freeWhenDone: (BOOL)shouldFree
{ {
if (aBuffer == 0 && bufferSize > 0)
{
[NSException raise: NSInvalidArgumentException
format: @"[%@-initWithBytesNoCopy:length:freeWhenDone:] called with "
@"length but null bytes", NSStringFromClass([self class])];
}
if (shouldFree == NO) if (shouldFree == NO)
{ {
GSPrivateSwizzle(self, dataStatic); GSPrivateSwizzle(self, dataStatic);
@ -3103,8 +3101,14 @@ getBytes(void* dst, void* src, unsigned len, unsigned limit, unsigned *pos)
- (id) initWithBytes: (const void*)aBuffer length: (NSUInteger)bufferSize - (id) initWithBytes: (const void*)aBuffer length: (NSUInteger)bufferSize
{ {
shmid = -1; shmid = -1;
if (aBuffer && bufferSize) if (bufferSize > 0)
{ {
if (aBuffer == 0)
{
[NSException raise: NSInvalidArgumentException
format: @"[%@-initWithBytes:length:] called with "
@"length but null bytes", NSStringFromClass([self class])];
}
shmid = shmget(IPC_PRIVATE, bufferSize, IPC_CREAT|VM_RDONLY); shmid = shmget(IPC_PRIVATE, bufferSize, IPC_CREAT|VM_RDONLY);
if (shmid == -1) /* Created memory? */ if (shmid == -1) /* Created memory? */
{ {
@ -3190,12 +3194,6 @@ getBytes(void* dst, void* src, unsigned len, unsigned limit, unsigned *pos)
return NSMutableDataAbstract; return NSMutableDataAbstract;
} }
- (id) copy
{
return [[dataMalloc allocWithZone: NSDefaultMallocZone()]
initWithBytes: bytes length: length];
}
- (id) copyWithZone: (NSZone*)z - (id) copyWithZone: (NSZone*)z
{ {
return [[dataMalloc allocWithZone: z] return [[dataMalloc allocWithZone: z]
@ -3222,10 +3220,16 @@ getBytes(void* dst, void* src, unsigned len, unsigned limit, unsigned *pos)
self = [self initWithCapacity: bufferSize]; self = [self initWithCapacity: bufferSize];
if (self) if (self)
{ {
if (aBuffer && bufferSize > 0) if (bufferSize > 0)
{ {
memcpy(bytes, aBuffer, bufferSize); if (aBuffer == 0)
{
[NSException raise: NSInvalidArgumentException
format: @"[%@-initWithBytes:length:] called with "
@"length but null bytes", NSStringFromClass([self class])];
}
length = bufferSize; length = bufferSize;
memcpy(bytes, aBuffer, length);
} }
} }
return self; return self;
@ -3237,11 +3241,14 @@ getBytes(void* dst, void* src, unsigned len, unsigned limit, unsigned *pos)
{ {
if (aBuffer == 0) if (aBuffer == 0)
{ {
self = [self initWithCapacity: bufferSize]; if (bufferSize > 0)
if (self != nil)
{ {
[self setLength: bufferSize]; [NSException raise: NSInvalidArgumentException
format: @"[%@-initWithBytesNoCopy:length:freeWhenDone:] called with "
@"length but null bytes", NSStringFromClass([self class])];
} }
self = [self initWithCapacity: bufferSize];
[self setLength: 0];
return self; return self;
} }
#if GS_WITH_GC #if GS_WITH_GC
@ -3326,26 +3333,6 @@ getBytes(void* dst, void* src, unsigned len, unsigned limit, unsigned *pos)
return self; return self;
} }
- (id) initWithContentsOfFile: (NSString *)path
{
self = [self initWithCapacity: 0];
#if GS_WITH_GC
if (readContentsOfFile(path, &bytes, &length, 0) == NO)
{
return nil;
}
#else
zone = GSObjCZone(self);
if (readContentsOfFile(path, &bytes, &length, zone) == NO)
{
[self release];
return nil;
}
#endif
capacity = length;
return self;
}
- (id) initWithContentsOfMappedFile: (NSString *)path - (id) initWithContentsOfMappedFile: (NSString *)path
{ {
return [self initWithContentsOfFile: path]; return [self initWithContentsOfFile: path];
@ -3354,15 +3341,24 @@ getBytes(void* dst, void* src, unsigned len, unsigned limit, unsigned *pos)
- (void) appendBytes: (const void*)aBuffer - (void) appendBytes: (const void*)aBuffer
length: (NSUInteger)bufferSize length: (NSUInteger)bufferSize
{ {
unsigned oldLength = length; if (bufferSize > 0)
unsigned minimum = length + bufferSize;
if (minimum > capacity)
{ {
[self _grow: minimum]; unsigned oldLength = length;
unsigned minimum = length + bufferSize;
if (aBuffer == 0)
{
[NSException raise: NSInvalidArgumentException
format: @"[%@-appendBytes:length:] called with "
@"length but null bytes", NSStringFromClass([self class])];
}
if (minimum > capacity)
{
[self _grow: minimum];
}
memcpy(bytes + oldLength, aBuffer, bufferSize);
length = minimum;
} }
memcpy(bytes + oldLength, aBuffer, bufferSize);
length = minimum;
} }
- (NSUInteger) capacity - (NSUInteger) capacity
@ -3402,10 +3398,16 @@ getBytes(void* dst, void* src, unsigned len, unsigned limit, unsigned *pos)
if (aRange.location > length) if (aRange.location > length)
{ {
[NSException raise: NSRangeException [NSException raise: NSRangeException
format: @"location bad in replaceByteInRange:withBytes:"]; format: @"location bad in replaceBytesInRange:withBytes:"];
} }
if (aRange.length > 0) if (aRange.length > 0)
{ {
if (moreBytes == 0)
{
[NSException raise: NSInvalidArgumentException
format: @"[%@-replaceBytesInRange:withBytes:] called with "
@"range but null bytes", NSStringFromClass([self class])];
}
if (need > length) if (need > length)
{ {
[self setCapacity: need]; [self setCapacity: need];
@ -3838,18 +3840,6 @@ getBytes(void* dst, void* src, unsigned len, unsigned limit, unsigned *pos)
[super finalize]; [super finalize];
} }
- (id) initWithBytes: (const void*)aBuffer length: (NSUInteger)bufferSize
{
self = [self initWithCapacity: bufferSize];
if (self)
{
if (bufferSize && aBuffer)
memcpy(bytes, aBuffer, bufferSize);
length = bufferSize;
}
return self;
}
- (id) initWithCapacity: (NSUInteger)bufferSize - (id) initWithCapacity: (NSUInteger)bufferSize
{ {
shmid = shmget(IPC_PRIVATE, bufferSize, IPC_CREAT|VM_ACCESS); shmid = shmget(IPC_PRIVATE, bufferSize, IPC_CREAT|VM_ACCESS);