From 6b3903e783c091becda6b79a67896aee07f26c3d Mon Sep 17 00:00:00 2001 From: Daniel Lundqvist Date: Sun, 30 Mar 2025 16:09:07 +0200 Subject: [PATCH] Don't ask system to clear memory if it's going to be fully overwritten anyway. These are in principal strdups but using AllocVec. --- configfile.c | 12 ++++--- configmodel.c | 71 ++++++++++++++++++++++++---------------- containers/stringarray.c | 17 +++++----- 3 files changed, 60 insertions(+), 40 deletions(-) diff --git a/configfile.c b/configfile.c index 0989c79..c473af8 100644 --- a/configfile.c +++ b/configfile.c @@ -68,10 +68,12 @@ CONFIGFILEPTR ConfigFileRead(BPTR configFile) UBYTE buffer[256]; if(NameFromFH(configFile, buffer, 256))// if we opened with a filename this is a waste, if not its necessary { + int bufferlen = strlen(buffer); result = AllocVec(sizeof(struct ConfigFile), MEMF_CLEAR); result->sectionStore = SectionStoreNew(); - result->filename = AllocVec(strlen(buffer)+1, MEMF_CLEAR); - CopyMem(buffer, result->filename, strlen(buffer)); + result->filename = AllocVec(bufferlen+1, 0L); + CopyMem(buffer, result->filename, bufferlen); + result->filename[bufferlen] = '\0'; while( (line = configFileReadLine(configFile)) != NULL ) { SectionStoreAddLineToCurrentSection(result->sectionStore, line); @@ -133,8 +135,10 @@ STRPTR ConfigFileGet(CONFIGFILEPTR abstractConfigFile, CONST_STRPTR compoundKey) if( var != NULL ) { CONST_STRPTR value = VariableGetRawValue(var); - result = AllocVec(strlen(value)+1, MEMF_CLEAR); - CopyMem(value, result, strlen(value)); + int valuelen = strlen(value); + result = AllocVec(valuelen+1, 0L); + CopyMem(value, result, valuelen); + result[valuelen] = '\0'; } StringArrayFree(split, TRUE); } diff --git a/configmodel.c b/configmodel.c index e690a60..9bdbae4 100644 --- a/configmodel.c +++ b/configmodel.c @@ -42,9 +42,11 @@ LINEPTR LineNew(CONST_STRPTR buffer) struct Line* result = NULL; if( buffer != 0 ) { + int bufferlen = strlen(buffer); result = AllocVec(sizeof(struct Line), MEMF_CLEAR); - result->rawText = AllocVec(strlen(buffer)+1, MEMF_CLEAR); - CopyMem(buffer, result->rawText, strlen(buffer)); + result->rawText = AllocVec(bufferlen+1, 0L); + CopyMem(buffer, result->rawText, bufferlen); + result->rawText[bufferlen] = '\0'; } return result; } @@ -242,17 +244,19 @@ SECTIONPTR SectionCreateWithNameAndSubname(CONST_STRPTR primary, CONST_STRPTR se if( primary != NULL ) { - ULONG length = strlen(primary); + int primarylen = strlen(primary); + int secondarylen = secondary ? strlen(secondary) : 0; result = AllocVec(sizeof(struct Section), MEMF_CLEAR); result->insertPos = 0; - result->primary = AllocVec(length+1, MEMF_CLEAR); - CopyMem(primary, (STRPTR)result->primary, length); + result->primary = AllocVec(primarylen+1, 0L); + CopyMem(primary, (STRPTR)result->primary, primarylen); + ((STRPTR)result->primary)[primarylen] = '\0'; - if( secondary != NULL && strlen(secondary) > 0 ) + if( secondary != NULL && secondarylen > 0 ) { - ULONG length = strlen(secondary); - result->secondary = AllocVec(length+1, MEMF_CLEAR); - CopyMem(secondary, (STRPTR)result->secondary, length); + result->secondary = AllocVec(secondarylen+1, 0L); + CopyMem(secondary, (STRPTR)result->secondary, secondarylen); + ((STRPTR)result->secondary)[secondarylen] = '\0'; } result->lines = LineArrayNew(); @@ -327,8 +331,10 @@ VOID SectionCollectLinesForVariable(SECTIONPTR abstractSection, CONST_STRPTR var { ULONG lineCount = SizeOfArray(section->lines); ULONG index = 0; - STRPTR normalizedKey = AllocVec(strlen(varKey)+1, MEMF_CLEAR); - CopyMem(varKey, normalizedKey, strlen(varKey)); + int varKeylen = strlen(varKey); + STRPTR normalizedKey = AllocVec(varKeylen+1, 0L); + CopyMem(varKey, normalizedKey, varKeylen); + normalizedKey[varKeylen] = '\0'; downcaseString(normalizedKey); for( index = 0; index < lineCount; index++ ) @@ -356,8 +362,10 @@ VOID SectionRemoveLinesForVariable(SECTIONPTR abstractSection, CONST_STRPTR varK ULONG index = 0; ULONG newInsertPos = 0; LineArray newLineArray = LineArrayNew(); - STRPTR normalizedKey = AllocVec(strlen(varKey)+1, MEMF_CLEAR); - CopyMem(varKey, normalizedKey, strlen(varKey)); + int varKeylen = strlen(varKey); + STRPTR normalizedKey = AllocVec(varKeylen+1, 0L); + CopyMem(varKey, normalizedKey, varKeylen); + normalizedKey[varKeylen] = '\0'; downcaseString(normalizedKey); for( index = 0; index < lineCount; index++ ) @@ -423,7 +431,7 @@ CONST_STRPTR SectionSerialize(SECTIONPTR abstractSection) } size += 2; // ]\n - result = AllocVec(size+1, MEMF_CLEAR); + result = AllocVec(size+1, 0L); if( section->secondary == NULL ) { sprintf(result, "[%s]\n", section->primary); @@ -482,7 +490,7 @@ CONST_STRPTR SectionCanonicalName(SECTIONPTR abstractSection) totalLength += secondaryLength; } - result = AllocVec(totalLength+1, MEMF_CLEAR); + result = AllocVec(totalLength+1, 0L); CopyMem(section->primary, result, primaryLength); if( secondaryLength > 0 ) @@ -504,19 +512,22 @@ VARIABLEPTR VariableCreate(CONST_STRPTR key, CONST_STRPTR rawValue) struct Variable* result = NULL; if( key != NULL ) { - ULONG length = strlen(key); + int keylen = strlen(key); result = AllocVec(sizeof(struct Variable), MEMF_CLEAR); - result->key = AllocVec(length+1, MEMF_CLEAR); - CopyMem(key, (STRPTR)result->key, length); - result->normalizedKey = AllocVec(length+1, MEMF_CLEAR); - CopyMem(key, (STRPTR)result->normalizedKey, length); + result->key = AllocVec(keylen+1, 0L); + CopyMem(key, (STRPTR)result->key, keylen); + ((STRPTR)result->key)[keylen] = '\0'; + result->normalizedKey = AllocVec(keylen+1, 0L); + CopyMem(key, (STRPTR)result->normalizedKey, keylen); + ((STRPTR)result->normalizedKey)[keylen] = '\0'; downcaseString((STRPTR)result->normalizedKey); } if( rawValue != NULL ) { - ULONG length = strlen(rawValue); - result->stringValue = AllocVec(length+1, MEMF_CLEAR); - CopyMem(rawValue, (STRPTR)result->stringValue, length); + int rawValuelen = strlen(rawValue); + result->stringValue = AllocVec(rawValuelen+1, 0L); + CopyMem(rawValue, (STRPTR)result->stringValue, rawValuelen); + ((STRPTR)result->stringValue)[rawValuelen] = '\0'; } return result; } @@ -573,7 +584,7 @@ CONST_STRPTR VariableSerialize(VARIABLEPTR abstractVariable) if( variable != NULL && variable->normalizedKey != NULL && variable->stringValue != NULL ) { ULONG size = 1 + strlen(variable->normalizedKey) + 3 + strlen(variable->stringValue) + 1 + 1; - result = AllocVec(size, MEMF_CLEAR); + result = AllocVec(size, 0L); sprintf(result, "\t%s = %s\n", variable->normalizedKey, variable->stringValue); } return (CONST_STRPTR)result; @@ -592,8 +603,10 @@ StringArray CompoundKeySplitKeyCompletely(CONST_STRPTR key) STRPTR token = NULL; // we need to make a copy of the key because strtok modifies it. - STRPTR keyCopy = AllocVec(strlen(key)+1, MEMF_CLEAR); - CopyMem(key, keyCopy, strlen(key)); + int keylen = strlen(key); + STRPTR keyCopy = AllocVec(keylen+1, 0L); + CopyMem(key, keyCopy, keylen); + keyCopy[keylen] = '\0'; token = strtok(keyCopy, "."); while (token != NULL) @@ -650,8 +663,10 @@ StringArray CompoundKeySplitKeyForVar(CONST_STRPTR key) STRPTR token = NULL; // we need to make a copy of the key because strtok modifies it. - STRPTR keyCopy = AllocVec(strlen(key)+1, MEMF_CLEAR); - CopyMem(key, keyCopy, strlen(key)); + int keylen = strlen(key); + STRPTR keyCopy = AllocVec(keylen+1, 0L); + CopyMem(key, keyCopy, keylen); + keyCopy[keylen] = '\0'; token = strtok(keyCopy, "."); while (token != NULL) diff --git a/containers/stringarray.c b/containers/stringarray.c index 058463d..3ffed10 100644 --- a/containers/stringarray.c +++ b/containers/stringarray.c @@ -19,8 +19,10 @@ VOID StringArrayAppend(StringArray array, CONST_STRPTR value) VOID StringArrayAppendAndRetain(StringArray array, CONST_STRPTR value) { - STRPTR localCopy = AllocVec(strlen(value)+1, MEMF_CLEAR); - CopyMem(value, localCopy, strlen(value)); + int valuelen = strlen(value); + STRPTR localCopy = AllocVec(valuelen+1, 0L); + CopyMem(value, localCopy, valuelen); + localCopy[valuelen] = '\0'; StringArrayAppend(array, localCopy); } @@ -59,18 +61,17 @@ STRPTR StringArrayJoinedParts(StringArray array, BYTE linkCharacter, ULONG start } requiredSize += (maxParts == 0 ? 0 : maxParts - 1); // add the join characters - requiredSize += 1; // trailing NULL - - result = AllocVec(requiredSize, MEMF_CLEAR); + result = AllocVec(requiredSize+1, MEMF_CLEAR); for( index = startIndex; index < lastIndex; index++ ) { CONST_STRPTR entry = StringArrayValues(array)[index]; - CopyMem((STRPTR)entry, result+cursor, strlen(entry)); - cursor += strlen(entry); + int entrylen = strlen(entry); + CopyMem((STRPTR)entry, result+cursor, entrylen); + cursor += entrylen; result[cursor] = linkCharacter; cursor += 1; } - result[requiredSize-1] = '\0'; + result[requiredSize] = '\0'; return result; }