From 0450e5ca087b46bd614421df4a9b073cb1f2ab2c Mon Sep 17 00:00:00 2001 From: Rosen Penev Date: Thu, 21 Sep 2023 21:15:14 -0700 Subject: [PATCH] simplify function and avoid strtok The latter is not thread safe. Signed-off-by: Rosen Penev --- app/exiv2.cpp | 40 ++++++++++------------------------------ 1 file changed, 10 insertions(+), 30 deletions(-) diff --git a/app/exiv2.cpp b/app/exiv2.cpp index f197800b90..591dcb96b0 100644 --- a/app/exiv2.cpp +++ b/app/exiv2.cpp @@ -1097,52 +1097,32 @@ int Params::getopt(int argc, char* const Argv[]) { // local implementations namespace { bool parseTime(const std::string& ts, int64_t& time) { - std::string hstr; - std::string mstr; - std::string sstr; - auto cts = new char[ts.length() + 1]; - strcpy(cts, ts.c_str()); - auto tmp = ::strtok(cts, ":"); - if (tmp) - hstr = tmp; - tmp = ::strtok(nullptr, ":"); - if (tmp) - mstr = tmp; - tmp = ::strtok(nullptr, ":"); - if (tmp) - sstr = tmp; - delete[] cts; - + std::istringstream sts(ts); int sign = 1; int64_t hh = 0; int64_t mm = 0; int64_t ss = 0; + // [-]HH part - if (!Util::strtol(hstr.c_str(), hh)) + if (!(sts >> hh)) return false; if (hh < 0) { sign = -1; hh *= -1; } // check for the -0 special case - if (hh == 0 && Exiv2::Internal::contains(hstr, '-')) + if (hh == 0 && Exiv2::Internal::contains(ts, '-')) sign = -1; // MM part, if there is one - if (!mstr.empty()) { - if (!Util::strtol(mstr.c_str(), mm)) - return false; - if (mm > 59) - return false; - if (mm < 0) + if (sts.peek() == ':') { + sts.ignore(); + if (!(sts >> mm) || mm > 59 || mm < 0) return false; } // SS part, if there is one - if (!sstr.empty()) { - if (!Util::strtol(sstr.c_str(), ss)) - return false; - if (ss > 59) - return false; - if (ss < 0) + if (sts.peek() == ':') { + sts.ignore(); + if (!(sts >> ss) || ss > 59 || ss < 0) return false; }