From f896a108dda995ea7d2e7c0edc4fc8516b1fa7d6 Mon Sep 17 00:00:00 2001 From: Kyle Isom Date: Thu, 19 Oct 2023 21:43:38 -0700 Subject: [PATCH] Coverity fixes. Primarily using std::move in more places. --- CMakeLists.txt | 7 ++++--- include/scsl/{Flag.h => Flags.h} | 0 include/scsl/StringUtil.h | 2 +- include/scsl/scsl.h | 2 +- src/sl/{Flag.cc => Flags.cc} | 13 +++++++------ src/sl/StringUtil.cc | 6 +++--- src/test/Assert.cc | 2 +- test/{flag.cc => flags.cc} | 2 +- test/orientation.cc | 2 +- 9 files changed, 19 insertions(+), 17 deletions(-) rename include/scsl/{Flag.h => Flags.h} (100%) rename src/sl/{Flag.cc => Flags.cc} (95%) rename test/{flag.cc => flags.cc} (98%) diff --git a/CMakeLists.txt b/CMakeLists.txt index f98220c..ca4b7cb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,6 +24,7 @@ add_compile_options( "-g" "$<$:-O2>" ) +#add_link_options("-fsanitize=address") if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") add_compile_options("-stdlib=libc++") else () @@ -40,7 +41,7 @@ set(HEADER_FILES include/scsl/Commander.h include/scsl/Dictionary.h include/sctest/Exceptions.h - include/scsl/Flag.h + include/scsl/Flags.h include/scsl/StringUtil.h include/scsl/TLV.h @@ -65,7 +66,7 @@ set(SOURCE_FILES src/sl/Commander.cc src/sl/Dictionary.cc src/test/Exceptions.cc - src/sl/Flag.cc + src/sl/Flags.cc src/sl/StringUtil.cc src/sl/TLV.cc @@ -108,7 +109,7 @@ endmacro() generate_test(buffer) generate_test(tlv) generate_test(dictionary) -generate_test(flag) +generate_test(flags) generate_test(stringutil) # math and physics diff --git a/include/scsl/Flag.h b/include/scsl/Flags.h similarity index 100% rename from include/scsl/Flag.h rename to include/scsl/Flags.h diff --git a/include/scsl/StringUtil.h b/include/scsl/StringUtil.h index 083cd50..2cdd414 100644 --- a/include/scsl/StringUtil.h +++ b/include/scsl/StringUtil.h @@ -94,7 +94,7 @@ std::vector SplitN(std::string, std::string delimiter, size_t maxCo /// WrapText is a very simple wrapping function that breaks the line into /// lines of at most lineLength characters. It does this by breaking the /// line into individual words (splitting on whitespace). -std::vector WrapText(std::string line, size_t lineLength); +std::vector WrapText(std::string& line, size_t lineLength); /// Write out a vector of lines indented with tabs. /// diff --git a/include/scsl/scsl.h b/include/scsl/scsl.h index 948bda2..edb8771 100644 --- a/include/scsl/scsl.h +++ b/include/scsl/scsl.h @@ -33,7 +33,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/sl/Flag.cc b/src/sl/Flags.cc similarity index 95% rename from src/sl/Flag.cc rename to src/sl/Flags.cc index dab14f9..8f879c5 100644 --- a/src/sl/Flag.cc +++ b/src/sl/Flags.cc @@ -23,9 +23,10 @@ #include #include +#include #include -#include +#include #include @@ -33,7 +34,7 @@ namespace scsl { static const std::regex isFlag("^--?[a-zA-Z0-9][a-zA-Z0-9-_]*$", - std::regex_constants::nosubs); + std::regex_constants::nosubs|std::regex_constants::optimize); std::string Flags::ParseStatusToString(ParseStatus status) @@ -60,7 +61,7 @@ NewFlag(std::string fName, FlagType fType, std::string fDescription) flag->Type = fType; flag->WasSet = false; - flag->Name = fName; + flag->Name = std::move(fName); flag->Description = fDescription; flag->Value = FlagValue{}; @@ -69,7 +70,7 @@ NewFlag(std::string fName, FlagType fType, std::string fDescription) Flags::Flags(std::string fName) - : name(fName), description("") + : name(std::move(fName)), description("") { } @@ -135,7 +136,7 @@ Flags::Register(std::string fName, int defaultValue, std::string fDescription) bool Flags::Register(std::string fName, unsigned int defaultValue, std::string fDescription) { - if (!this->Register(fName, FlagType::UnsignedInteger, fDescription)) { + if (!this->Register(fName, FlagType::UnsignedInteger, std::move(fDescription))) { return false; } @@ -147,7 +148,7 @@ Flags::Register(std::string fName, unsigned int defaultValue, std::string fDescr bool Flags::Register(std::string fName, size_t defaultValue, std::string fDescription) { - if (!this->Register(fName, FlagType::SizeT, fDescription)) { + if (!this->Register(fName, FlagType::SizeT, std::move(fDescription))) { return false; } diff --git a/src/sl/StringUtil.cc b/src/sl/StringUtil.cc index a0c56c2..3666532 100644 --- a/src/sl/StringUtil.cc +++ b/src/sl/StringUtil.cc @@ -38,7 +38,7 @@ namespace S { std::vector SplitKeyValuePair(std::string line, std::string delimiter) { - auto pair = SplitN(line, delimiter, 2); + auto pair = SplitN(std::move(line), delimiter, 2); if (pair.size() == 0) { return {"", ""}; @@ -61,7 +61,7 @@ SplitKeyValuePair(std::string line, char delimiter) { std::string sDelim; - sDelim.push_back(delimiter); + sDelim.push_back(std::move(delimiter)); return SplitKeyValuePair(line, sDelim); } @@ -143,7 +143,7 @@ SplitN(std::string s, std::string delim, size_t maxCount) std::vector -WrapText(std::string line, size_t lineLength) +WrapText(std::string& line, size_t lineLength) { std::vector wrapped; auto parts = SplitN(line, " ", 0); diff --git a/src/test/Assert.cc b/src/test/Assert.cc index 501b690..20c3ccb 100644 --- a/src/test/Assert.cc +++ b/src/test/Assert.cc @@ -35,7 +35,7 @@ Assert(bool condition, std::string message) { #if defined(NDEBUG) || defined(SCSL_NOEXCEPT) if (!condition) { - throw AssertionFailed(message); + throw AssertionFailed(std::move(message)); } #else if (!condition) { diff --git a/test/flag.cc b/test/flags.cc similarity index 98% rename from test/flag.cc rename to test/flags.cc index fc71857..a360393 100644 --- a/test/flag.cc +++ b/test/flags.cc @@ -4,7 +4,7 @@ #include -#include +#include #include diff --git a/test/orientation.cc b/test/orientation.cc index 1ae6d19..dd0beca 100644 --- a/test/orientation.cc +++ b/test/orientation.cc @@ -1,4 +1,4 @@ -#include +#include #include #include