From 34e2ca0566ab4cf5ed66f242a16878d6bb2579d4 Mon Sep 17 00:00:00 2001 From: Brad Arant Date: Wed, 16 Oct 2024 11:15:54 -0700 Subject: [PATCH] Fixed NULL assignment to ZString. --- ZString.cpp | 14 ++++++++++++-- testing/zstring_test.cpp | 5 ++++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/ZString.cpp b/ZString.cpp index 5002c64..132b545 100644 --- a/ZString.cpp +++ b/ZString.cpp @@ -33,8 +33,18 @@ namespace coreutils { cursor = data; } - ZString::ZString(const char *data) : data((char *)data), length(strlen(data)), cursor((char *)data) {} - + ZString::ZString(const char *data) { + if((char *)data != NULL) { + this->data = (char *)data; + this->length = strlen(data); + this->cursor = (char *)data; + } else { + this->data = NULL; + this->length = 0; + this->cursor = NULL; + } + } + ZString::ZString(char *data, size_t length) : data(data), length(length), cursor(data) {} ZString::ZString(unsigned char *data, size_t length) : data((char *)data), length(length), cursor((char *)data) {} diff --git a/testing/zstring_test.cpp b/testing/zstring_test.cpp index 7ebb697..b4d9d66 100644 --- a/testing/zstring_test.cpp +++ b/testing/zstring_test.cpp @@ -84,7 +84,7 @@ int main(int argc, char **argv) { coreutils::ZString test14("25.5"); coreutils::ZString test15("xyx"); - std::cout << test14.startsWithDouble() << ":" << test15.startsWithDouble() << std::endl; + std::cout << test14.startsWithNumber() << ":" << test15.startsWithNumber() << std::endl; coreutils::ZString test16("This is a test of substring"); std::cout << test16.substring(10) << std::endl; @@ -102,5 +102,8 @@ int main(int argc, char **argv) { coreutils::ZString test19("1,"); std::cout << test19.asDouble() << ":" << test19.unparsed() << std::endl; + + std::cout << "Null assignment" << std::endl; + coreutils::ZString test20(NULL); }