From ed68792e765411a994d8ac79e4d047bbafc25582 Mon Sep 17 00:00:00 2001 From: David Steele Date: Thu, 7 Oct 2021 19:57:28 -0400 Subject: [PATCH] Rename strNewN() to strNewZN(). Make the function name consistent with other functions that accept zero-terminated strings, e.g. strNewZ() and strCatZN(). --- src/command/archive/common.c | 2 +- src/command/control/stop.c | 2 +- src/command/help/help.c | 4 ++-- src/common/ini.c | 8 ++++---- src/common/io/read.c | 4 ++-- src/common/io/tls/client.c | 2 +- src/common/lock.c | 2 +- src/common/regExp.c | 2 +- src/common/type/json.c | 4 ++-- src/common/type/string.c | 6 +++--- src/common/type/string.h | 10 +++++----- src/common/type/stringList.c | 2 +- src/common/type/variant.h | 2 +- src/config/parse.c | 6 +++--- src/storage/posix/storage.c | 2 +- src/storage/storage.c | 2 +- test/src/module/common/ioTlsTest.c | 2 +- test/src/module/common/logTest.c | 2 +- test/src/module/common/typeStringTest.c | 2 +- 19 files changed, 33 insertions(+), 33 deletions(-) diff --git a/src/command/archive/common.c b/src/command/archive/common.c index acbebc4ee..71fea04b0 100644 --- a/src/command/archive/common.c +++ b/src/command/archive/common.c @@ -136,7 +136,7 @@ archiveAsyncStatus(ArchiveMode archiveMode, const String *walSegment, bool throw THROW_FMT(FormatError, "%s message must be > 0", strZ(statusFile)); // Get contents - code = varIntForce(VARSTR(strNewN(strZ(content), (size_t)(linefeedPtr - strZ(content))))); + code = varIntForce(VARSTR(strNewZN(strZ(content), (size_t)(linefeedPtr - strZ(content))))); message = strTrim(strNewZ(linefeedPtr + 1)); } diff --git a/src/command/control/stop.c b/src/command/control/stop.c index 9efaaa557..8f6ecd92a 100644 --- a/src/command/control/stop.c +++ b/src/command/control/stop.c @@ -79,7 +79,7 @@ cmdStop(void) char contents[LOCK_BUFFER_SIZE]; ssize_t actualBytes = read(fd, contents, sizeof(contents)); String *processId = actualBytes > 0 ? - strTrim(strLstGet(strLstNewSplitZ(strNewN(contents, (size_t)actualBytes), LF_Z), 0)) : NULL; + strTrim(strLstGet(strLstNewSplitZ(strNewZN(contents, (size_t)actualBytes), LF_Z), 0)) : NULL; // If the process id is defined then assume this is a valid lock file if (processId != NULL && strSize(processId) > 0) diff --git a/src/command/help/help.c b/src/command/help/help.c index 6075372bb..aad3b52bc 100644 --- a/src/command/help/help.c +++ b/src/command/help/help.c @@ -67,7 +67,7 @@ helpRenderSplitSize(const String *string, const char *delimiter, size_t size) if (stringMatchLast != NULL) stringMatch = stringMatchLast - strlen(delimiter); - strLstAdd(this, strNewN(stringBase, (size_t)(stringMatch - stringBase))); + strLstAdd(this, strNewZN(stringBase, (size_t)(stringMatch - stringBase))); stringBase = stringMatch + strlen(delimiter); stringMatchLast = NULL; } @@ -79,7 +79,7 @@ helpRenderSplitSize(const String *string, const char *delimiter, size_t size) { if (stringMatchLast != NULL && strlen(stringBase) - strlen(delimiter) >= size) { - strLstAdd(this, strNewN(stringBase, (size_t)((stringMatchLast - strlen(delimiter)) - stringBase))); + strLstAdd(this, strNewZN(stringBase, (size_t)((stringMatchLast - strlen(delimiter)) - stringBase))); stringBase = stringMatchLast; } diff --git a/src/common/ini.c b/src/common/ini.c index cb97feb7b..f8348d5a4 100644 --- a/src/common/ini.c +++ b/src/common/ini.c @@ -257,7 +257,7 @@ iniParse(Ini *this, const String *content) THROW_FMT(FormatError, "ini section should end with ] at line %u: %s", lineIdx + 1, linePtr); // Assign section - section = strNewN(linePtr + 1, strSize(line) - 2); + section = strNewZN(linePtr + 1, strSize(line) - 2); } // Else it should be a key/value else @@ -272,7 +272,7 @@ iniParse(Ini *this, const String *content) THROW_FMT(FormatError, "missing '=' in key/value at line %u: %s", lineIdx + 1, linePtr); // Extract the key - String *key = strTrim(strNewN(linePtr, (size_t)(lineEqual - linePtr))); + String *key = strTrim(strNewZN(linePtr, (size_t)(lineEqual - linePtr))); if (strSize(key) == 0) THROW_FMT(FormatError, "key is zero-length at line %u: %s", lineIdx++, linePtr); @@ -363,7 +363,7 @@ iniLoad( // Assign section MEM_CONTEXT_PRIOR_BEGIN() { - section = strNewN(linePtr + 1, strSize(line) - 2); + section = strNewZN(linePtr + 1, strSize(line) - 2); } MEM_CONTEXT_PRIOR_END(); } @@ -394,7 +394,7 @@ iniLoad( retry = false; // Get key/value - key = strNewN(linePtr, (size_t)(lineEqual - linePtr)); + key = strNewZN(linePtr, (size_t)(lineEqual - linePtr)); value = strNewZ(lineEqual + 1); // Check that the value is valid JSON diff --git a/src/common/io/read.c b/src/common/io/read.c index ba8bff174..0c7b1001f 100644 --- a/src/common/io/read.c +++ b/src/common/io/read.c @@ -315,7 +315,7 @@ ioReadLineParam(IoRead *this, bool allowEof) size_t size = (size_t)(linefeed - outputPtr); // Create the string - result = strNewN(outputPtr, size); + result = strNewZN(outputPtr, size); this->outputPos += size + 1; } } @@ -342,7 +342,7 @@ ioReadLineParam(IoRead *this, bool allowEof) if (ioReadEof(this)) { if (allowEof) - result = strNewN((char *)bufPtr(this->output), bufUsed(this->output)); + result = strNewZN((char *)bufPtr(this->output), bufUsed(this->output)); else THROW(FileReadError, "unexpected eof while reading line"); } diff --git a/src/common/io/tls/client.c b/src/common/io/tls/client.c index 29a973026..211f0ba9e 100644 --- a/src/common/io/tls/client.c +++ b/src/common/io/tls/client.c @@ -92,7 +92,7 @@ asn1ToStr(ASN1_STRING *nameAsn1) THROW(CryptoError, "TLS certificate name entry is missing"); FUNCTION_TEST_RETURN( // {vm_covered} - strNewN( + strNewZN( #if OPENSSL_VERSION_NUMBER < 0x10100000L (const char *)ASN1_STRING_data(nameAsn1), #else diff --git a/src/common/lock.c b/src/common/lock.c index 73e2541b5..ec6a21f54 100644 --- a/src/common/lock.c +++ b/src/common/lock.c @@ -108,7 +108,7 @@ lockAcquireFile(const String *lockFile, const String *execId, TimeMSec lockTimeo THROW_ON_SYS_ERROR_FMT(actualBytes == -1, FileReadError, "unable to read '%s", strZ(lockFile)); // Parse the file and see if the exec id matches - const StringList *parse = strLstNewSplitZ(strNewN(buffer, (size_t)actualBytes), LF_Z); + const StringList *parse = strLstNewSplitZ(strNewZN(buffer, (size_t)actualBytes), LF_Z); if (strLstSize(parse) == 3 && strEq(strLstGet(parse, 1), execId)) result = LOCK_ON_EXEC_ID; diff --git a/src/common/regExp.c b/src/common/regExp.c index 2d874d4bc..00996547e 100644 --- a/src/common/regExp.c +++ b/src/common/regExp.c @@ -164,7 +164,7 @@ regExpMatchStr(RegExp *this) ASSERT(this != NULL); - FUNCTION_TEST_RETURN(this->matchPtr == NULL ? NULL : strNewN(regExpMatchPtr(this), regExpMatchSize(this))); + FUNCTION_TEST_RETURN(this->matchPtr == NULL ? NULL : strNewZN(regExpMatchPtr(this), regExpMatchSize(this))); } /**********************************************************************************************************************************/ diff --git a/src/common/type/json.c b/src/common/type/json.c index 52a9baa10..c8ab9250a 100644 --- a/src/common/type/json.c +++ b/src/common/type/json.c @@ -111,7 +111,7 @@ jsonToNumberInternal(const char *json, unsigned int *jsonPos) MEM_CONTEXT_TEMP_BEGIN() { // Extract the numeric as a string - String *resultStr = strNewN(json + beginPos, *jsonPos - beginPos); + String *resultStr = strNewZN(json + beginPos, *jsonPos - beginPos); // Convert the string to a integer variant MEM_CONTEXT_PRIOR_BEGIN() @@ -300,7 +300,7 @@ jsonToStrInternal(const char *json, unsigned int *jsonPos) // Decode char (*jsonPos) += 2; - strCatChr(result, (char)cvtZToUIntBase(strZ(strNewN(json + *jsonPos, 2)), 16)); + strCatChr(result, (char)cvtZToUIntBase(strZ(strNewZN(json + *jsonPos, 2)), 16)); (*jsonPos) += 1; break; diff --git a/src/common/type/string.c b/src/common/type/string.c index fd514364c..85d94afd0 100644 --- a/src/common/type/string.c +++ b/src/common/type/string.c @@ -225,7 +225,7 @@ strNewFmt(const char *format, ...) /**********************************************************************************************************************************/ String * -strNewN(const char *string, size_t size) +strNewZN(const char *string, size_t size) { FUNCTION_TEST_BEGIN(); FUNCTION_TEST_PARAM_P(CHARDATA, string); @@ -739,7 +739,7 @@ strPath(const String *this) end--; FUNCTION_TEST_RETURN( - strNewN( + strNewZN( this->pub.buffer, end - this->pub.buffer <= 1 ? (size_t)(end - this->pub.buffer) : (size_t)(end - this->pub.buffer - 1))); } @@ -918,7 +918,7 @@ strSubN(const String *this, size_t start, size_t size) ASSERT(start <= strSize(this)); ASSERT(start + size <= strSize(this)); - FUNCTION_TEST_RETURN(strNewN(strZ(this) + start, size)); + FUNCTION_TEST_RETURN(strNewZN(strZ(this) + start, size)); } /**********************************************************************************************************************************/ diff --git a/src/common/type/string.h b/src/common/type/string.h index b4b3d479e..a7a2c5188 100644 --- a/src/common/type/string.h +++ b/src/common/type/string.h @@ -8,7 +8,7 @@ old context and then back. Below is a simplified example: String *result = NULL; <--- is created in the current memory context (referred to as "old context" below) MEM_CONTEXT_TEMP_BEGIN() <--- begins a new temporary context { - String *resultStr = strNewN("myNewStr"); <--- creates a string in the temporary memory context + String *resultStr = strNewZN("myNewStr"); <--- creates a string in the temporary memory context MEM_CONTEXT_PRIOR_BEGIN() <--- switch to the old context so the duplication of the string is in that context { @@ -55,6 +55,10 @@ String *strNew(void); // Create a new fixed length string from a zero-terminated string String *strNewZ(const char *const string); +// Create a new fixed length string from a zero-terminated string with a specific length. The string may or may not be +// zero-terminated but we'll use that nomenclature since we're not concerned about the end of the string. +String *strNewZN(const char *string, size_t size); + // Create a new fixed length string from a buffer. If the buffer has a NULL character this may not work as expected. All the data // will be copied but only the data before the NULL character will be used as a string. String *strNewBuf(const Buffer *buffer); @@ -68,10 +72,6 @@ String *strNewEncode(EncodeType type, const Buffer *buffer); // Create a new fixed length string from a format string with parameters (i.e. sprintf) String *strNewFmt(const char *format, ...) __attribute__((format(printf, 1, 2))); -// Create a new fixed length string from a zero-terminated string with a specific length. The string may or may not be -// zero-terminated but we'll use that nomenclature since we're not concerned about the end of the string. -String *strNewN(const char *string, size_t size); - // Create a new fixed length string from a string String *strDup(const String *this); diff --git a/src/common/type/stringList.c b/src/common/type/stringList.c index 6caa5735e..febf58a29 100644 --- a/src/common/type/stringList.c +++ b/src/common/type/stringList.c @@ -61,7 +61,7 @@ strLstNewSplitZ(const String *string, const char *delimiter) // If a match was found then add the string if (stringMatch != NULL) { - strLstAddInternal(this, strNewN(stringBase, (size_t)(stringMatch - stringBase))); + strLstAddInternal(this, strNewZN(stringBase, (size_t)(stringMatch - stringBase))); stringBase = stringMatch + strlen(delimiter); } // Else make whatever is left the last string diff --git a/src/common/type/variant.h b/src/common/type/variant.h index c222721c6..e7c1fccd9 100644 --- a/src/common/type/variant.h +++ b/src/common/type/variant.h @@ -8,7 +8,7 @@ old context and then back. Below is a simplified example: Variant *result = NULL; <--- is created in the current memory context (referred to as "old context" below) MEM_CONTEXT_TEMP_BEGIN() <--- begins a new temporary context { - String *resultStr = strNewN("myNewStr"); <--- creates a string in the temporary memory context + String *resultStr = strNewZN("myNewStr"); <--- creates a string in the temporary memory context MEM_CONTEXT_PRIOR_BEGIN() <--- switch to old context so creation of the variant from the string is in old context { diff --git a/src/config/parse.c b/src/config/parse.c index 28c524c55..5f468506e 100644 --- a/src/config/parse.c +++ b/src/config/parse.c @@ -1214,7 +1214,7 @@ configParse(const Storage *storage, unsigned int argListSize, const char *argLis if (equalPtr) { - optionName = strNewN(arg, (size_t)(equalPtr - arg)); + optionName = strNewZN(arg, (size_t)(equalPtr - arg)); optionArg = strNewZ(equalPtr + 1); } else @@ -1435,7 +1435,7 @@ configParse(const Storage *storage, unsigned int argListSize, const char *argLis // Get key and value const String *key = strReplaceChr( - strLower(strNewN(keyValue + PGBACKREST_ENV_SIZE, (size_t)(equalPtr - (keyValue + PGBACKREST_ENV_SIZE)))), + strLower(strNewZN(keyValue + PGBACKREST_ENV_SIZE, (size_t)(equalPtr - (keyValue + PGBACKREST_ENV_SIZE)))), '_', '-'); const String *value = STR(equalPtr + 1); @@ -1944,7 +1944,7 @@ configParse(const Storage *storage, unsigned int argListSize, const char *argLis cfgParseOptionKeyIdxName(optionId, optionKeyIdx)); } - kvPut(keyValue, VARSTR(strNewN(pair, (size_t)(equal - pair))), VARSTRZ(equal + 1)); + kvPut(keyValue, VARSTR(strNewZN(pair, (size_t)(equal - pair))), VARSTRZ(equal + 1)); } configOptionValue->value = value; diff --git a/src/storage/posix/storage.c b/src/storage/posix/storage.c index ce4a8461d..a5ceed97d 100644 --- a/src/storage/posix/storage.c +++ b/src/storage/posix/storage.c @@ -104,7 +104,7 @@ storagePosixInfo(THIS_VOID, const String *file, StorageInfoLevel level, StorageI (linkDestinationSize = readlink(strZ(file), linkDestination, sizeof(linkDestination) - 1)) == -1, FileReadError, "unable to get destination for link '%s'", strZ(file)); - result.linkDestination = strNewN(linkDestination, (size_t)linkDestinationSize); + result.linkDestination = strNewZN(linkDestination, (size_t)linkDestinationSize); } } } diff --git a/src/storage/storage.c b/src/storage/storage.c index 61c270903..d6a386354 100644 --- a/src/storage/storage.c +++ b/src/storage/storage.c @@ -727,7 +727,7 @@ storagePath(const Storage *this, const String *pathExp, StoragePathParam param) THROW_FMT(AssertError, "end > not found in path expression '%s'", strZ(pathExp)); // Create a string from the expression - String *expression = strNewN(strZ(pathExp), (size_t)(end - strZ(pathExp) + 1)); + String *expression = strNewZN(strZ(pathExp), (size_t)(end - strZ(pathExp) + 1)); // Create a string from the path if there is anything left after the expression String *path = NULL; diff --git a/test/src/module/common/ioTlsTest.c b/test/src/module/common/ioTlsTest.c index f3ce45f98..96ef0b840 100644 --- a/test/src/module/common/ioTlsTest.c +++ b/test/src/module/common/ioTlsTest.c @@ -197,7 +197,7 @@ testRun(void) TEST_ERROR( tlsClientHostVerifyName( - STRDEF("host"), strNewN("ab\0cd", 5)), CryptoError, "TLS certificate name contains embedded null"); + STRDEF("host"), strNewZN("ab\0cd", 5)), CryptoError, "TLS certificate name contains embedded null"); TEST_ERROR(tlsClientHostVerify(STRDEF("host"), NULL), CryptoError, "No certificate presented by the TLS server"); diff --git a/test/src/module/common/logTest.c b/test/src/module/common/logTest.c index a8d4bfa12..f9949ecc7 100644 --- a/test/src/module/common/logTest.c +++ b/test/src/module/common/logTest.c @@ -178,7 +178,7 @@ testRun(void) TEST_RESULT_VOID( logInternal(logLevelWarn, LOG_LEVEL_MIN, LOG_LEVEL_MAX, 0, "file", "function", 0, "TEST"), "log timestamp"); - String *logTime = strNewN(logBuffer, 23); + String *logTime = strNewZN(logBuffer, 23); TEST_RESULT_BOOL( regExpMatchOne( STRDEF("^20[0-9]{2}\\-[0-1][0-9]\\-[0-3][0-9] [0-2][0-9]\\:[0-5][0-9]\\:[0-5][0-9]\\.[0-9]{3}$"), logTime), diff --git a/test/src/module/common/typeStringTest.c b/test/src/module/common/typeStringTest.c index 88a87cb79..527d26022 100644 --- a/test/src/module/common/typeStringTest.c +++ b/test/src/module/common/typeStringTest.c @@ -52,7 +52,7 @@ testRun(void) TEST_RESULT_VOID(strFree(string), "free string"); // ------------------------------------------------------------------------------------------------------------------------- - TEST_RESULT_STR_Z(strNewN("testmorestring", 4), "test", "new string with size limit"); + TEST_RESULT_STR_Z(strNewZN("testmorestring", 4), "test", "new string with size limit"); // ------------------------------------------------------------------------------------------------------------------------- Buffer *buffer = bufNew(8);