From 97841dad2ba5a79acb0b22db9a01ae45f7b2e80b Mon Sep 17 00:00:00 2001 From: Ruslan Baratov Date: Fri, 5 Dec 2014 17:18:11 +0300 Subject: file: Use 'long' to represent the parsed LOCK TIMEOUT value Convert the StringToInt helper into a StringToLong helper with a 'long' result type. This will make the helper more useful to other callers that want to use strtol. While at it, also check errno after calling strtol in case the conversion fails with a range error. --- Source/cmFileCommand.cxx | 9 +++++---- Source/cmFileLock.cxx | 4 ++-- Source/cmFileLock.h | 4 ++-- Source/cmFileLockPool.cxx | 8 ++++---- Source/cmFileLockPool.h | 10 ++++++---- Source/cmFileLockUnix.cxx | 2 +- Source/cmFileLockWin32.cxx | 2 +- Source/cmSystemTools.cxx | 7 ++++--- Source/cmSystemTools.h | 4 ++-- 9 files changed, 27 insertions(+), 23 deletions(-) diff --git a/Source/cmFileCommand.cxx b/Source/cmFileCommand.cxx index a6eb8c4..3c2dfa5 100644 --- a/Source/cmFileCommand.cxx +++ b/Source/cmFileCommand.cxx @@ -3521,7 +3521,7 @@ bool cmFileCommand::HandleLockCommand( }; Guard guard = GUARD_PROCESS; std::string resultVariable; - unsigned timeout = static_cast(-1); + unsigned long timeout = static_cast(-1); // Parse arguments if(args.size() < 2) @@ -3597,15 +3597,16 @@ bool cmFileCommand::HandleLockCommand( "expected timeout value after TIMEOUT"); return false; } - int scanned; - if(!cmSystemTools::StringToInt(args[i].c_str(), &scanned) || scanned < 0) + long scanned; + if(!cmSystemTools::StringToLong(args[i].c_str(), &scanned) + || scanned < 0) { cmOStringStream e; e << "TIMEOUT value \"" << args[i] << "\" is not an unsigned integer."; this->Makefile->IssueMessage(cmake::FATAL_ERROR, e.str()); return false; } - timeout = static_cast(scanned); + timeout = static_cast(scanned); } else { diff --git a/Source/cmFileLock.cxx b/Source/cmFileLock.cxx index 5f75637..e6aa5f4 100644 --- a/Source/cmFileLock.cxx +++ b/Source/cmFileLock.cxx @@ -28,7 +28,7 @@ cmFileLock::~cmFileLock() } cmFileLockResult cmFileLock::Lock( - const std::string& filename, unsigned timeout) + const std::string& filename, unsigned long timeout) { if (filename.empty()) { @@ -48,7 +48,7 @@ cmFileLockResult cmFileLock::Lock( cmFileLockResult result = this->OpenFile(); if (result.IsOk()) { - if (timeout == static_cast(-1)) + if (timeout == static_cast(-1)) { result = this->LockWithoutTimeout(); } diff --git a/Source/cmFileLock.h b/Source/cmFileLock.h index 4d922a0..dd959a7 100644 --- a/Source/cmFileLock.h +++ b/Source/cmFileLock.h @@ -37,7 +37,7 @@ class cmFileLock * @brief Lock the file. * @param timeoutSec Lock timeout. If -1 try until success or fatal error. */ - cmFileLockResult Lock(const std::string& filename, unsigned timeoutSec); + cmFileLockResult Lock(const std::string& filename, unsigned long timeoutSec); /** * @brief Unlock the file. @@ -57,7 +57,7 @@ class cmFileLock cmFileLockResult OpenFile(); cmFileLockResult LockWithoutTimeout(); - cmFileLockResult LockWithTimeout(unsigned timeoutSec); + cmFileLockResult LockWithTimeout(unsigned long timeoutSec); #if defined(_WIN32) typedef HANDLE FileId; diff --git a/Source/cmFileLockPool.cxx b/Source/cmFileLockPool.cxx index e84e71a..551a75a 100644 --- a/Source/cmFileLockPool.cxx +++ b/Source/cmFileLockPool.cxx @@ -60,7 +60,7 @@ void cmFileLockPool::PopFileScope() } cmFileLockResult cmFileLockPool::LockFunctionScope( - const std::string& filename, unsigned timeoutSec) + const std::string& filename, unsigned long timeoutSec) { if (this->IsAlreadyLocked(filename)) { @@ -74,7 +74,7 @@ cmFileLockResult cmFileLockPool::LockFunctionScope( } cmFileLockResult cmFileLockPool::LockFileScope( - const std::string& filename, unsigned timeoutSec) + const std::string& filename, unsigned long timeoutSec) { if (this->IsAlreadyLocked(filename)) { @@ -85,7 +85,7 @@ cmFileLockResult cmFileLockPool::LockFileScope( } cmFileLockResult cmFileLockPool::LockProcessScope( - const std::string& filename, unsigned timeoutSec) + const std::string& filename, unsigned long timeoutSec) { if (this->IsAlreadyLocked(filename)) { @@ -155,7 +155,7 @@ cmFileLockPool::ScopePool::~ScopePool() } cmFileLockResult cmFileLockPool::ScopePool::Lock( - const std::string& filename, unsigned timeoutSec) + const std::string& filename, unsigned long timeoutSec) { cmFileLock *lock = new cmFileLock(); const cmFileLockResult result = lock->Lock(filename, timeoutSec); diff --git a/Source/cmFileLockPool.h b/Source/cmFileLockPool.h index a63540c..baef310 100644 --- a/Source/cmFileLockPool.h +++ b/Source/cmFileLockPool.h @@ -45,13 +45,13 @@ class cmFileLockPool * @param timeoutSec Lock timeout. If -1 try until success or fatal error. */ cmFileLockResult LockFunctionScope( - const std::string& filename, unsigned timeoutSec + const std::string& filename, unsigned long timeoutSec ); cmFileLockResult LockFileScope( - const std::string& filename, unsigned timeoutSec + const std::string& filename, unsigned long timeoutSec ); cmFileLockResult LockProcessScope( - const std::string& filename, unsigned timeoutSec + const std::string& filename, unsigned long timeoutSec ); //@} @@ -72,7 +72,9 @@ class cmFileLockPool ScopePool(); ~ScopePool(); - cmFileLockResult Lock(const std::string& filename, unsigned timeoutSec); + cmFileLockResult Lock( + const std::string& filename, unsigned long timeoutSec + ); cmFileLockResult Release(const std::string& filename); bool IsAlreadyLocked(const std::string& filename) const; diff --git a/Source/cmFileLockUnix.cxx b/Source/cmFileLockUnix.cxx index 038011e..fc18a64 100644 --- a/Source/cmFileLockUnix.cxx +++ b/Source/cmFileLockUnix.cxx @@ -66,7 +66,7 @@ cmFileLockResult cmFileLock::LockWithoutTimeout() } } -cmFileLockResult cmFileLock::LockWithTimeout(unsigned seconds) +cmFileLockResult cmFileLock::LockWithTimeout(unsigned long seconds) { while (true) { diff --git a/Source/cmFileLockWin32.cxx b/Source/cmFileLockWin32.cxx index 17231ea..4691689 100644 --- a/Source/cmFileLockWin32.cxx +++ b/Source/cmFileLockWin32.cxx @@ -86,7 +86,7 @@ cmFileLockResult cmFileLock::LockWithoutTimeout() } } -cmFileLockResult cmFileLock::LockWithTimeout(unsigned seconds) +cmFileLockResult cmFileLock::LockWithTimeout(unsigned long seconds) { const DWORD flags = LOCKFILE_EXCLUSIVE_LOCK | LOCKFILE_FAIL_IMMEDIATELY; while (true) diff --git a/Source/cmSystemTools.cxx b/Source/cmSystemTools.cxx index 9852dd6..c83dc2a 100644 --- a/Source/cmSystemTools.cxx +++ b/Source/cmSystemTools.cxx @@ -2925,9 +2925,10 @@ std::vector cmSystemTools::tokenize(const std::string& str, } //---------------------------------------------------------------------------- -bool cmSystemTools::StringToInt(const char* str, int* value) +bool cmSystemTools::StringToLong(const char* str, long* value) { + errno = 0; char *endp; - *value = static_cast(strtol(str, &endp, 10)); - return (*endp == '\0') && (endp != str); + *value = strtol(str, &endp, 10); + return (*endp == '\0') && (endp != str) && (errno == 0); } diff --git a/Source/cmSystemTools.h b/Source/cmSystemTools.h index 763389b..d49af74 100644 --- a/Source/cmSystemTools.h +++ b/Source/cmSystemTools.h @@ -458,8 +458,8 @@ public: static std::vector tokenize(const std::string& str, const std::string& sep); - /** Convert string to int. Expected that the whole string is an integer */ - static bool StringToInt(const char* str, int* value); + /** Convert string to long. Expected that the whole string is an integer */ + static bool StringToLong(const char* str, long* value); #ifdef _WIN32 struct WindowsFileRetry -- cgit v0.12