From 82c043f108321faed9f14b08c6b280d34a53ac05 Mon Sep 17 00:00:00 2001 From: CaS Date: Sat, 1 Nov 2003 11:11:13 +0000 Subject: [PATCH] Minor fix for strings initialised with data they don't own ... consistent with MacOS-X implementation. git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@18019 72102866-910b-0410-8b05-ffd578937521 --- ChangeLog | 7 +++++++ Source/GSString.m | 51 +++++++++++++++++++++++++++++++++++++++++++---- Source/NSString.m | 13 +++++++----- 3 files changed, 62 insertions(+), 9 deletions(-) diff --git a/ChangeLog b/ChangeLog index 10d5c6605..ffbeaa831 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +Sat Nov 01 11:10:00 2003 Richard Frith-Macdonald + + * Source/NSString.m: ([-copyWithZone:]) always do true copy + * Source/GSString.m: Where the string data pointed to is not + owned by the string instance, have copy operations create new + instances rather than retaining the receiver. + Sat Nov 01 07:05:00 2003 Richard Frith-Macdonald * Source/NSNotificationCenter.m: Simplify locking code as original diff --git a/Source/GSString.m b/Source/GSString.m index 151e9d2f3..face896c1 100644 --- a/Source/GSString.m +++ b/Source/GSString.m @@ -1955,7 +1955,8 @@ transmute(ivars self, NSString *aString) - (id) copy { - if (NSShouldRetainWithZone(self, NSDefaultMallocZone()) == NO) + if (_flags.free == NO + || NSShouldRetainWithZone(self, NSDefaultMallocZone()) == NO) { GSCString *obj; @@ -1978,7 +1979,8 @@ transmute(ivars self, NSString *aString) - (id) copyWithZone: (NSZone*)z { - if (NSShouldRetainWithZone(self, z) == NO) + if (_flags.free == NO + || NSShouldRetainWithZone(self, z) == NO) { NSString *obj; @@ -2195,6 +2197,25 @@ transmute(ivars self, NSString *aString) _flags.wide = 0; return self; } +- (id) copy +{ + return RETAIN(self); +} +- (id) copyWithZone: (NSZone*)z +{ + if (NSShouldRetainWithZone(self, z) == NO) + { + NSString *obj; + + obj = (NSString*)NSAllocateObject(GSCInlineStringClass, _count, z); + obj = [obj initWithCString: _contents.c length: _count]; + return obj; + } + else + { + return RETAIN(self); + } +} - (void) dealloc { NSDeallocateObject(self); @@ -2258,7 +2279,8 @@ transmute(ivars self, NSString *aString) - (id) copy { - if (NSShouldRetainWithZone(self, NSDefaultMallocZone()) == NO) + if (_flags.free == NO + || NSShouldRetainWithZone(self, NSDefaultMallocZone()) == NO) { GSUnicodeString *obj; @@ -2281,7 +2303,8 @@ transmute(ivars self, NSString *aString) - (id) copyWithZone: (NSZone*)z { - if (NSShouldRetainWithZone(self, z) == NO) + if (_flags.free == NO + || NSShouldRetainWithZone(self, z) == NO) { NSString *obj; @@ -2504,6 +2527,26 @@ transmute(ivars self, NSString *aString) _flags.wide = 1; return self; } +- (id) copy +{ + return RETAIN(self); +} +- (id) copyWithZone: (NSZone*)z +{ + if (NSShouldRetainWithZone(self, z) == NO) + { + NSString *obj; + + obj = (NSString*)NSAllocateObject(GSUnicodeInlineStringClass, + _count*sizeof(unichar), z); + obj = [obj initWithCharacters: _contents.u length: _count]; + return obj; + } + else + { + return RETAIN(self); + } +} - (void) dealloc { NSDeallocateObject(self); diff --git a/Source/NSString.m b/Source/NSString.m index 495a435d8..d92cd60c4 100644 --- a/Source/NSString.m +++ b/Source/NSString.m @@ -3848,11 +3848,14 @@ handle_printf_atsign (FILE *stream, - (id) copyWithZone: (NSZone*)zone { - if ([self isKindOfClass: [NSMutableString class]] || - NSShouldRetainWithZone(self, zone) == NO) - return [[NSStringClass allocWithZone: zone] initWithString: self]; - else - return RETAIN(self); + /* + * Default implementation should not simply retain ... the string may + * have been initialised with freeWhenDone==NO and not own its + * characters ... so the code which created it may destroy the memory + * when it has finished with the original string ... leaving the + * copy with pointers to invalid data. So, we always copy in full. + */ + return [[NSStringClass allocWithZone: zone] initWithString: self]; } - (id) mutableCopyWithZone: (NSZone*)zone