From a31258c1d17d299c3082d3807a3b9f5a09590227 Mon Sep 17 00:00:00 2001 From: Wolfgang Lux Date: Sat, 23 Mar 2013 18:09:25 +0000 Subject: [PATCH] Check the result of the super class initializer and assign it to self. Also fix two memory management errors. git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/steptalk/trunk@36403 72102866-910b-0410-8b05-ffd578937521 --- Languages/Smalltalk/ChangeLog | 39 +++++++++++++ Languages/Smalltalk/STBlock.m | 22 ++++--- Languages/Smalltalk/STBlockContext.m | 12 ++-- Languages/Smalltalk/STBytecodeInterpreter.m | 22 ++++--- Languages/Smalltalk/STBytecodes.m | 25 ++++---- Languages/Smalltalk/STCompiledCode.m | 31 +++++----- Languages/Smalltalk/STCompiledMethod.m | 45 +++++++-------- Languages/Smalltalk/STCompiledScript.m | 8 +-- Languages/Smalltalk/STCompiler.m | 28 ++++----- Languages/Smalltalk/STCompilerUtils.m | 57 ++++++++++--------- Languages/Smalltalk/STExecutionContext.m | 10 ++-- Languages/Smalltalk/STLiterals.m | 14 +++-- Languages/Smalltalk/STMessage.m | 9 +-- Languages/Smalltalk/STMethodContext.m | 26 +++++---- Languages/Smalltalk/STSmalltalkScriptObject.m | 36 ++++++------ Languages/Smalltalk/STSourceReader.m | 16 +++--- Languages/Smalltalk/STStack.m | 12 ++-- 17 files changed, 240 insertions(+), 172 deletions(-) diff --git a/Languages/Smalltalk/ChangeLog b/Languages/Smalltalk/ChangeLog index 776221e..14edefe 100644 --- a/Languages/Smalltalk/ChangeLog +++ b/Languages/Smalltalk/ChangeLog @@ -1,3 +1,42 @@ +2013-03-23 Wolfgang Lux + + * STBlock.m (-initWithInterpreter:...): + * STBlockContext.m (initWithInterpreter:initialIP:stackSize:): + * STBytecodeInterpreter.m (-initWithEnvironment:): + * STBytecodes.m (-initWithData:, -initWithCoder:): + * STCompiledMethod.m (-initWihtSelector:argumentCount:bytecodeData:...) + -initWithCoder:): + * STCompiledScript.m (-initWithVariableNames:): + * STCompiler.m (-initWithEnvironment:, -init): + * STCompilerUtils.m(STCMethod) (-initWithPattern:statements:): + * STCompilerUtils.m(STCMessage) (-init): + * STCompilerUtils.m(STCMessageExpression) (-initWithTarget:message:): + * STCompilerUtils.m(STCPrimaryExpression) (-initWithObject:): + * STCompilerUtils.m(STCPrimary) (-initWithType:object:): + * STExecutionContext.m (-initWithStackSize:): + * STLiterals.m(STObjectReferenceLiteral) (initWithObjectName:poolName:): + * STLiterals.m(STBlockLiteral) (-initWithArgumentCount:): + * STMessage.m (-initWithSelector:arguments:): + * STMethodContext.m (-initWithMethod:environment:): + * STSmalltalkScriptObject.m (-initWithEnvironment:compiledScript:): + * STSourceReader.m (initWithString:range:): + * STStack.m (-initWithSize:): + Check the result of the super class initializer and assign it to + self. + + * STBlock.m (-initWithInterpreter:..., -dealloc): Retain and release + the homeContext and interpreter attributes. + + * STBytecodeInterpreter.m (-delloc): Release environment attribute to + fix space leak. + + * STBytecodeInterpreter.m (createBlockWithArgumentCount:stackSize:): + * STCompiledMethod.m (+methodWithCode:messagePattern:): + * STCompilerUtils.m (+primaryWithVariable:, +primaryWithLiteral:, + +primaryWithBlock:, +primaryWithExpression:): + Use idiomatic Objective-C code, which never assigns the result of an + +alloc method to a local variable. + 2013-03-02 Wolfgang Lux * STSourceReader.m (_STNormalizeStringToken): Unescape two successive diff --git a/Languages/Smalltalk/STBlock.m b/Languages/Smalltalk/STBlock.m index c1d9bb7..1cd4323 100644 --- a/Languages/Smalltalk/STBlock.m +++ b/Languages/Smalltalk/STBlock.m @@ -48,15 +48,23 @@ Class STBlockContextClass = nil; argumentCount:(int)count stackSize:(int)size { - homeContext = context; - argCount = count; - stackSize = size; - initialIP = ptr; - interpreter = anInterpreter; - - return [super init]; + if ((self = [super init]) != nil) + { + homeContext = RETAIN(context); + argCount = count; + stackSize = size; + initialIP = ptr; + interpreter = RETAIN(anInterpreter); + } + return self; } +- (void)dealloc +{ + RELEASE(homeContext); + RELEASE(interpreter); + [super dealloc]; +} - (unsigned)argumentCount { return argCount; diff --git a/Languages/Smalltalk/STBlockContext.m b/Languages/Smalltalk/STBlockContext.m index 97cd679..ee2cab1 100644 --- a/Languages/Smalltalk/STBlockContext.m +++ b/Languages/Smalltalk/STBlockContext.m @@ -39,13 +39,13 @@ initialIP:(unsigned)pointer stackSize:(unsigned)size { - [super initWithStackSize:size]; - - interpreter = RETAIN(anInterpreter); - initialIP = pointer; + if ((self = [super initWithStackSize:size]) != nil) + { + interpreter = RETAIN(anInterpreter); + initialIP = pointer; - instructionPointer = initialIP; - + instructionPointer = initialIP; + } return self; } - (void)dealloc diff --git a/Languages/Smalltalk/STBytecodeInterpreter.m b/Languages/Smalltalk/STBytecodeInterpreter.m index 6596f67..1de35cc 100644 --- a/Languages/Smalltalk/STBytecodeInterpreter.m +++ b/Languages/Smalltalk/STBytecodeInterpreter.m @@ -93,10 +93,15 @@ static Class NSInvocation_class = nil; - initWithEnvironment:(STEnvironment *)env { - self = [super init]; - environment = RETAIN(env); + if ((self = [super init]) != nil) + environment = RETAIN(env); return self; } +- (void)delloc +{ + RELEASE(environment); + [super dealloc]; +} - (void)setEnvironment:(STEnvironment *)env { ASSIGN(environment,env); @@ -300,13 +305,12 @@ static Class NSInvocation_class = nil; stackSize, ptr); - block = [STBlock alloc]; - - [block initWithInterpreter:self - homeContext:[activeContext homeContext] - initialIP:ptr - argumentCount:argCount - stackSize:stackSize]; + block = + [[STBlock alloc] initWithInterpreter:self + homeContext:[activeContext homeContext] + initialIP:ptr + argumentCount:argCount + stackSize:stackSize]; [stack push:AUTORELEASE(block)]; } diff --git a/Languages/Smalltalk/STBytecodes.m b/Languages/Smalltalk/STBytecodes.m index 2591894..00815ac 100644 --- a/Languages/Smalltalk/STBytecodes.m +++ b/Languages/Smalltalk/STBytecodes.m @@ -170,9 +170,12 @@ NSString *STDissasembleBytecode(STBytecode bytecode) length: (unsigned)length fromZone: (NSZone*)zone { - bytes = [[NSData alloc] initWithBytesNoCopy:someBytes - length:length - fromZone:zone]; + if ((self = [super init]) != nil) + { + bytes = [[NSData alloc] initWithBytesNoCopy:someBytes + length:length + fromZone:zone]; + } return self; } */ @@ -181,10 +184,10 @@ NSString *STDissasembleBytecode(STBytecode bytecode) - (id) initWithData: (NSData *)data { - self = [super init]; - - bytes = RETAIN(data); - + if ((self = [super init]) != nil) + { + bytes = RETAIN(data); + } return self; } @@ -294,10 +297,10 @@ NSString *STDissasembleBytecode(STBytecode bytecode) - initWithCoder:(NSCoder *)decoder { - self = [super init]; // super initWithCoder: decoder]; - - [decoder decodeValueOfObjCType: @encode(id) at: &bytes]; - + if ((self = [super init] /*[super initWithCoder: decoder]*/) != nil) + { + [decoder decodeValueOfObjCType: @encode(id) at: &bytes]; + } return self; } diff --git a/Languages/Smalltalk/STCompiledCode.m b/Languages/Smalltalk/STCompiledCode.m index a4bc2b0..cdffe10 100644 --- a/Languages/Smalltalk/STCompiledCode.m +++ b/Languages/Smalltalk/STCompiledCode.m @@ -38,13 +38,14 @@ stackSize:(unsigned)size namedReferences:(NSMutableArray *)refs { - [super init]; - - bytecodes = [[STBytecodes alloc] initWithData:data]; - literals = [[NSArray alloc] initWithArray:anArray]; - tempCount = count; - stackSize = size; - namedRefs = [[NSArray alloc] initWithArray:refs]; + if ((self = [super init]) != nil) + { + bytecodes = [[STBytecodes alloc] initWithData:data]; + literals = [[NSArray alloc] initWithArray:anArray]; + tempCount = count; + stackSize = size; + namedRefs = [[NSArray alloc] initWithArray:refs]; + } return self; } - (void)dealloc @@ -94,14 +95,14 @@ - initWithCoder:(NSCoder *)decoder { - self = [super init]; // [super initWithCoder: decoder]; - - [decoder decodeValueOfObjCType: @encode(id) at: &bytecodes]; - [decoder decodeValueOfObjCType: @encode(id) at: &literals]; - [decoder decodeValueOfObjCType: @encode(id) at: &namedRefs]; - [decoder decodeValueOfObjCType: @encode(short) at: &tempCount]; - [decoder decodeValueOfObjCType: @encode(short) at: &stackSize]; - + if ((self = [super init] /*[super initWithCoder: decoder]*/) != nil) + { + [decoder decodeValueOfObjCType: @encode(id) at: &bytecodes]; + [decoder decodeValueOfObjCType: @encode(id) at: &literals]; + [decoder decodeValueOfObjCType: @encode(id) at: &namedRefs]; + [decoder decodeValueOfObjCType: @encode(short) at: &tempCount]; + [decoder decodeValueOfObjCType: @encode(short) at: &stackSize]; + } return self; } diff --git a/Languages/Smalltalk/STCompiledMethod.m b/Languages/Smalltalk/STCompiledMethod.m index 9d2998c..93907a5 100644 --- a/Languages/Smalltalk/STCompiledMethod.m +++ b/Languages/Smalltalk/STCompiledMethod.m @@ -41,15 +41,14 @@ { STCompiledMethod *method; - method = [STCompiledMethod alloc]; - - [method initWithSelector:[pattern selector] - argumentCount:[[pattern arguments] count] - bytecodesData:[[code bytecodes] data] - literals:[code literals] - temporariesCount:[code temporariesCount] - stackSize:[code stackSize] - namedReferences:[code namedReferences]]; + method = + [[STCompiledMethod alloc] initWithSelector:[pattern selector] + argumentCount:[[pattern arguments] count] + bytecodesData:[[code bytecodes] data] + literals:[code literals] + temporariesCount:[code temporariesCount] + stackSize:[code stackSize] + namedReferences:[code namedReferences]]; return AUTORELEASE(method); @@ -63,15 +62,15 @@ stackSize:(unsigned)size namedReferences:(NSMutableArray *)refs; { - self = [super initWithBytecodesData:data - literals:anArray - temporariesCount:tCount - stackSize:size - namedReferences:refs]; - - selector = RETAIN(sel); - argCount = aCount; - + if ((self = [super initWithBytecodesData:data + literals:anArray + temporariesCount:tCount + stackSize:size + namedReferences:refs]) != nil) + { + selector = RETAIN(sel); + argCount = aCount; + } return self; } - (void)dealloc @@ -132,11 +131,11 @@ - initWithCoder:(NSCoder *)decoder { - self = [super initWithCoder: decoder]; - - [decoder decodeValueOfObjCType: @encode(id) at: &selector]; - [decoder decodeValueOfObjCType: @encode(short) at: &argCount]; - + if ((self = [super initWithCoder: decoder]) != nil) + { + [decoder decodeValueOfObjCType: @encode(id) at: &selector]; + [decoder decodeValueOfObjCType: @encode(short) at: &argCount]; + } return self; } - (NSString *)source diff --git a/Languages/Smalltalk/STCompiledScript.m b/Languages/Smalltalk/STCompiledScript.m index eff6e59..f24722c 100644 --- a/Languages/Smalltalk/STCompiledScript.m +++ b/Languages/Smalltalk/STCompiledScript.m @@ -51,10 +51,10 @@ static SEL finalizeSelector; - initWithVariableNames:(NSArray *)array; { - [super init]; - - variableNames = RETAIN(array); - + if ((self = [super init]) != nil) + { + variableNames = RETAIN(array); + } return self; } - (void)dealloc diff --git a/Languages/Smalltalk/STCompiler.m b/Languages/Smalltalk/STCompiler.m index 2337327..c058b2b 100644 --- a/Languages/Smalltalk/STCompiler.m +++ b/Languages/Smalltalk/STCompiler.m @@ -125,27 +125,27 @@ extern int STCparse(void *context); } - initWithEnvironment:(STEnvironment *)env { - self = [self init]; - [self setEnvironment:env]; + if ((self = [self init]) != nil) + [self setEnvironment:env]; return self; } - init { - [super init]; - - arrayLiteralClass = [NSMutableArray class]; - stringLiteralClass = [NSMutableString class]; - /* bytesLiteralClass = [NSMutableData class]; */ - intNumberLiteralClass = [NSNumber class]; - realNumberLiteralClass = [NSNumber class]; - symbolLiteralClass = [STSelector class]; - characterLiteralClass = [NSString class]; - - receiverVars = [[NSMutableArray alloc] init]; - namedReferences = [[NSMutableArray alloc] init]; + if ((self = [super init]) != nil) + { + arrayLiteralClass = [NSMutableArray class]; + stringLiteralClass = [NSMutableString class]; + /* bytesLiteralClass = [NSMutableData class]; */ + intNumberLiteralClass = [NSNumber class]; + realNumberLiteralClass = [NSNumber class]; + symbolLiteralClass = [STSelector class]; + characterLiteralClass = [NSString class]; + receiverVars = [[NSMutableArray alloc] init]; + namedReferences = [[NSMutableArray alloc] init]; + } return self; } - (void)dealloc diff --git a/Languages/Smalltalk/STCompilerUtils.m b/Languages/Smalltalk/STCompilerUtils.m index 3847538..b2d3dcf 100644 --- a/Languages/Smalltalk/STCompilerUtils.m +++ b/Languages/Smalltalk/STCompilerUtils.m @@ -42,9 +42,11 @@ } - initWithPattern:(STCMessage *)patt statements:(STCStatements *)stats { - [super init]; - messagePattern = RETAIN(patt); - statements = RETAIN(stats); + if ((self = [super init]) != nil) + { + messagePattern = RETAIN(patt); + statements = RETAIN(stats); + } return self; } - (void)dealloc @@ -120,11 +122,11 @@ } - init { - [super init]; - - selector = [[NSMutableString alloc] init]; - args = [[NSMutableArray alloc] init]; - + if ((self = [super init]) != nil) + { + selector = [[NSMutableString alloc] init]; + args = [[NSMutableArray alloc] init]; + } return self; } - (void)dealloc @@ -218,11 +220,11 @@ @implementation STCMessageExpression:STCExpression - initWithTarget:(id)anObject message:(STCMessage *)aMessage; { - [super init]; - - target = RETAIN(anObject); - message = RETAIN(aMessage); - + if ((self = [super init]) != nil) + { + target = RETAIN(anObject); + message = RETAIN(aMessage); + } return self; } - (void)dealloc @@ -253,8 +255,8 @@ } - initWithObject:(id)anObject { - [super init]; - object = RETAIN(anObject); + if ((self = [super init]) != nil) + object = RETAIN(anObject); return self; } @@ -276,36 +278,39 @@ + primaryWithVariable:(id) anObject { STCPrimary *primary; - primary = [STCPrimary alloc]; - [primary initWithType:STCVariablePrimaryType object:anObject]; + primary = [[STCPrimary alloc] initWithType:STCVariablePrimaryType + object:anObject]; return AUTORELEASE(primary); } + primaryWithLiteral:(id) anObject { STCPrimary *primary; - primary = [STCPrimary alloc]; - [primary initWithType:STCLiteralPrimaryType object:anObject]; + primary = [[STCPrimary alloc] initWithType:STCLiteralPrimaryType + object:anObject]; return AUTORELEASE(primary); } + primaryWithBlock:(id) anObject { STCPrimary *primary; - primary = [STCPrimary alloc]; - [primary initWithType:STCBlockPrimaryType object:anObject]; + primary = [[STCPrimary alloc] initWithType:STCBlockPrimaryType + object:anObject]; return AUTORELEASE(primary); } + primaryWithExpression:(id) anObject { STCPrimary *primary; - primary = [STCPrimary alloc]; - [primary initWithType:STCExpressionPrimaryType object:anObject]; + primary = [[STCPrimary alloc] initWithType:STCExpressionPrimaryType + object:anObject]; return AUTORELEASE(primary); } - initWithType:(int)newType object:obj { - type = newType; - object = RETAIN(obj); - return [super init]; + if ((self = [super init]) != nil) + { + type = newType; + object = RETAIN(obj); + } + return self; } - (void)dealloc { diff --git a/Languages/Smalltalk/STExecutionContext.m b/Languages/Smalltalk/STExecutionContext.m index f193419..6a1641d 100644 --- a/Languages/Smalltalk/STExecutionContext.m +++ b/Languages/Smalltalk/STExecutionContext.m @@ -25,11 +25,11 @@ static unsigned nextId = 1; @implementation STExecutionContext - initWithStackSize:(unsigned)stackSize { - [super init]; - - stack = [[STStack alloc] initWithSize:stackSize]; - contextId = nextId ++; - + if ((self = [super init]) != nil) + { + stack = [[STStack alloc] initWithSize:stackSize]; + contextId = nextId ++; + } return self; } - (void)dealloc diff --git a/Languages/Smalltalk/STLiterals.m b/Languages/Smalltalk/STLiterals.m index 87a37ce..f1965fe 100644 --- a/Languages/Smalltalk/STLiterals.m +++ b/Languages/Smalltalk/STLiterals.m @@ -32,9 +32,12 @@ @implementation STObjectReferenceLiteral - initWithObjectName:(NSString *)anObject poolName:(NSString *)aPool { - objectName = RETAIN(anObject); - poolName = RETAIN(aPool); - return [super init]; + if ((self = [super init]) != nil) + { + objectName = RETAIN(anObject); + poolName = RETAIN(aPool); + } + return self; } #if 0 - copyWithZone:(NSZone *)zone @@ -68,8 +71,9 @@ @implementation STBlockLiteral - initWithArgumentCount:(unsigned)count { - argCount = count; - return [super init]; + if ((self = [super init]) != nil) + argCount = count; + return self; } - (void)setStackSize:(unsigned)size { diff --git a/Languages/Smalltalk/STMessage.m b/Languages/Smalltalk/STMessage.m index c86d35d..a47628c 100644 --- a/Languages/Smalltalk/STMessage.m +++ b/Languages/Smalltalk/STMessage.m @@ -44,10 +44,11 @@ - initWithSelector:(NSString *)aString arguments:(NSArray *)anArray { - [super init]; - selector = RETAIN(aString); - args = RETAIN(anArray); - + if ((self = [super init]) != nil) + { + selector = RETAIN(aString); + args = RETAIN(anArray); + } return self; } diff --git a/Languages/Smalltalk/STMethodContext.m b/Languages/Smalltalk/STMethodContext.m index e0fafd7..f8dd2aa 100644 --- a/Languages/Smalltalk/STMethodContext.m +++ b/Languages/Smalltalk/STMethodContext.m @@ -51,20 +51,22 @@ - initWithMethod:(STCompiledMethod *)newMethod environment:(STEnvironment *)env { - unsigned int tempCount; - unsigned int i; - - method = RETAIN(newMethod); - - tempCount = [method temporariesCount]; - temporaries = [[NSMutableArray alloc] initWithCapacity:tempCount]; - - for(i=0;i