diff options
author | Brad King <brad.king@kitware.com> | 2022-09-21 14:12:02 (GMT) |
---|---|---|
committer | Kitware Robot <kwrobot@kitware.com> | 2022-09-21 14:12:11 (GMT) |
commit | ef1d34b20d88e99de7a8e2f93feb247332cdafda (patch) | |
tree | 0d86f4245193d71fbd1d1d14f7a3816f83e5e613 /Source | |
parent | a50346c1d0ebf355ba27177817bc4b45259b0810 (diff) | |
parent | 8fc822e13a8bf8695e475655f647d5d69f99c414 (diff) | |
download | CMake-ef1d34b20d88e99de7a8e2f93feb247332cdafda.zip CMake-ef1d34b20d88e99de7a8e2f93feb247332cdafda.tar.gz CMake-ef1d34b20d88e99de7a8e2f93feb247332cdafda.tar.bz2 |
Merge topic 'parse-large-int'
8fc822e13a file: Avoid strange istringstream crash in cmake.org binaries on Alpine Linux
31f158e4c8 cmStringAlgorithms: Add functions to parse strings to long long integers
Acked-by: Kitware Robot <kwrobot@kitware.com>
Acked-by: buildbot <buildbot@kitware.com>
Merge-request: !7698
Diffstat (limited to 'Source')
-rw-r--r-- | Source/cmFileCommand.cxx | 10 | ||||
-rw-r--r-- | Source/cmStringAlgorithms.cxx | 32 | ||||
-rw-r--r-- | Source/cmStringAlgorithms.h | 10 |
3 files changed, 50 insertions, 2 deletions
diff --git a/Source/cmFileCommand.cxx b/Source/cmFileCommand.cxx index 7810040..bec9fb2 100644 --- a/Source/cmFileCommand.cxx +++ b/Source/cmFileCommand.cxx @@ -201,13 +201,19 @@ bool HandleReadCommand(std::vector<std::string> const& args, // is there a limit? std::string::size_type sizeLimit = std::string::npos; if (!arguments.Limit.empty()) { - std::istringstream(arguments.Limit) >> sizeLimit; + unsigned long long limit; + if (cmStrToULongLong(arguments.Limit, &limit)) { + sizeLimit = static_cast<std::string::size_type>(limit); + } } // is there an offset? cmsys::ifstream::off_type offset = 0; if (!arguments.Offset.empty()) { - std::istringstream(arguments.Offset) >> offset; + long long off; + if (cmStrToLongLong(arguments.Offset, &off)) { + offset = static_cast<cmsys::ifstream::off_type>(off); + } } file.seekg(offset, std::ios::beg); // explicit ios::beg for IBM VisualAge 6 diff --git a/Source/cmStringAlgorithms.cxx b/Source/cmStringAlgorithms.cxx index 1bb6808..f73c854 100644 --- a/Source/cmStringAlgorithms.cxx +++ b/Source/cmStringAlgorithms.cxx @@ -250,6 +250,38 @@ bool cmStrToULong(std::string const& str, unsigned long* value) return cmStrToULong(str.c_str(), value); } +bool cmStrToLongLong(const char* str, long long* value) +{ + errno = 0; + char* endp; + *value = strtoll(str, &endp, 10); + return (*endp == '\0') && (endp != str) && (errno == 0); +} + +bool cmStrToLongLong(std::string const& str, long long* value) +{ + return cmStrToLongLong(str.c_str(), value); +} + +bool cmStrToULongLong(const char* str, unsigned long long* value) +{ + errno = 0; + char* endp; + while (cmIsSpace(*str)) { + ++str; + } + if (*str == '-') { + return false; + } + *value = strtoull(str, &endp, 10); + return (*endp == '\0') && (endp != str) && (errno == 0); +} + +bool cmStrToULongLong(std::string const& str, unsigned long long* value) +{ + return cmStrToULongLong(str.c_str(), value); +} + template <typename Range> std::size_t getJoinedLength(Range const& rng, cm::string_view separator) { diff --git a/Source/cmStringAlgorithms.h b/Source/cmStringAlgorithms.h index 492e588..83938bc 100644 --- a/Source/cmStringAlgorithms.h +++ b/Source/cmStringAlgorithms.h @@ -303,3 +303,13 @@ bool cmStrToLong(std::string const& str, long* value); * integer */ bool cmStrToULong(const char* str, unsigned long* value); bool cmStrToULong(std::string const& str, unsigned long* value); + +/** Converts a string to long long. Expects that the whole string + * is an integer */ +bool cmStrToLongLong(const char* str, long long* value); +bool cmStrToLongLong(std::string const& str, long long* value); + +/** Converts a string to unsigned long long. Expects that the whole string + * is an integer */ +bool cmStrToULongLong(const char* str, unsigned long long* value); +bool cmStrToULongLong(std::string const& str, unsigned long long* value); |