- diff --git a/Base/CMakeLists.txt b/Base/CMakeLists.txt
- index e5fd783..b3922e1 100644
- --- a/Base/CMakeLists.txt
- +++ b/Base/CMakeLists.txt
- @@ -34,13 +34,15 @@ else(WIN32)
- if(RDE_BUILD)
- set(PROJECT_SOURCE_FILES ${PROJECT_SOURCE_FILES}
- RecursiveSemaphor.cpp
- - PosixSystem.cpp )
- + PosixSystem.cpp
- + RovLocale.cpp )
- else(RDE_BUILD)
- set(PROJECT_SOURCE_FILES ${PROJECT_SOURCE_FILES}
- TraceBack.cpp
- RovFileSystem.cpp
- RecursiveSemaphor.cpp
- - PosixSystem.cpp )
- + PosixSystem.cpp
- + RovLocale.cpp )
- endif(RDE_BUILD)
- endif(WIN32)
- diff --git a/Base/PosixSystem.hpp b/Base/PosixSystem.hpp
- index 9ec356a..cf7e2c7 100755
- --- a/Base/PosixSystem.hpp
- +++ b/Base/PosixSystem.hpp
- @@ -17,6 +17,7 @@
- #endif
- #include <time.h>
- #include <pthread.h>
- +#include <locale.h>
- #include <sys/socket.h>
- #include <sys/types.h>
- #include <sys/ipc.h>
- @@ -40,6 +41,8 @@
- #undef TEST_REALTIME
- +extern locale_t rov_get_posix_localeobj(void);
- +
- extern void dumpBackTrace( ostream & _outStr );
- //-DEFINES/MACROS--------------------------------------------------------------
- diff --git a/Base/RovLocale.cpp b/Base/RovLocale.cpp
- new file mode 100644
- index 0000000..90e735b
- --- /dev/null
- +++ b/Base/RovLocale.cpp
- @@ -0,0 +1,18 @@
- +
- +#include "PosixSystem.hpp"
- +
- +pthread_once_t posix_localeobj_init = PTHREAD_ONCE_INIT;
- +
- +static locale_t posix_localeobj = 0;
- +
- +static void init_posix_localeobj(void)
- +{
- + posix_localeobj = newlocale(LC_ALL_MASK, "POSIX", (locale_t)0);
- +}
- +
- +locale_t rov_get_posix_localeobj(void)
- +{
- + (void)pthread_once(&posix_localeobj_init, init_posix_localeobj);
- +
- + return posix_localeobj;
- +}
- diff --git a/Rdc/RdsTypes.cpp b/Rdc/RdsTypes.cpp
- index 213cabd..6b6861c 100755
- --- a/Rdc/RdsTypes.cpp
- +++ b/Rdc/RdsTypes.cpp
- @@ -619,8 +619,15 @@ RINLINE T_Bool g_ReadNumericalValueFromString (
- )
- {
- #ifdef JNA
- - std::locale actualLocal; //automatically initialized
- - std::setlocale(LC_NUMERIC, "en_US.utf8");
- + locale_t saved_locale;
- + locale_t posix_locale;
- +
- + /*
- + * Use POSIX locale because the code below uses functions which
- + * parse floating-poing values using locale-specific functions
- + */
- + posix_locale = rov_get_posix_localeobj();
- + saved_locale = uselocale(posix_locale);
- #endif
- T_Bool bOk = true;
- const string sToken = C_RovTool::strParse (_rsString, RDF_SEPARATOR);
- @@ -718,7 +725,7 @@ RINLINE T_Bool g_ReadNumericalValueFromString (
- } // end if (!bOk)
- #ifdef JNA
- - locale::global(actualLocal);
- + (void)uselocale(saved_locale);
- #endif
- return(bOk);
- } // end of readNumericalValueFromString
- @@ -764,8 +771,15 @@ RINLINE T_Bool writeNumericalValueToString (
- )
- {
- #ifdef JNA
- - std::locale actualLocal; //automatically initialized
- - std::setlocale(LC_NUMERIC, "en_US.utf8");
- + locale_t saved_locale;
- + locale_t posix_locale;
- +
- + /*
- + * Use POSIX locale because the code below uses functions which
- + * parse floating-poing values using locale-specific functions
- + */
- + posix_locale = rov_get_posix_localeobj();
- + saved_locale = uselocale(posix_locale);
- #endif
- register T_Bool bOk = true;
- if ( _pValue != NULL )
- @@ -827,7 +841,7 @@ RINLINE T_Bool writeNumericalValueToString (
- } // end if (_pValue != NULL)
- _rsString += RDF_SEPARATOR;
- #ifdef JNA
- - locale::global(actualLocal);
- + (void)uselocale(saved_locale);
- #endif
- return(bOk);
- } // end of writeNumericalValueToString
- diff --git a/Rdc/RdsUnit.h b/Rdc/RdsUnit.h
- index 11c8e38..1e3f903 100755
- --- a/Rdc/RdsUnit.h
- +++ b/Rdc/RdsUnit.h
- @@ -559,14 +559,21 @@ public:
- //******************************************************************************
- RINLINE T_Bool C_RdsUnitSize::convertString2Double(const string &_anyStr, T_Double64 & _anyDouble)
- {
- - #ifdef JNA
- - std::locale actualLocal; //automatically initialized
- - std::setlocale(LC_NUMERIC, "en_US.utf8");
- +#ifdef JNA
- + locale_t saved_locale;
- + locale_t posix_locale;
- +
- + /*
- + * Use POSIX locale because the code below uses functions which
- + * parse floating-poing values using locale-specific functions
- + */
- + posix_locale = rov_get_posix_localeobj();
- + saved_locale = uselocale(posix_locale);
- #endif
- T_String stopStr = (T_String) _anyStr.c_str();
- _anyDouble = strtod(stopStr, & stopStr);
- #ifdef JNA
- - locale::global(actualLocal);
- + (void)uselocale(saved_locale);
- #endif
- return true;
- } // end C_RdsUnitSize::convertString2Double
- diff --git a/Rds/RdsLib/RdsDataStream.cpp b/Rds/RdsLib/RdsDataStream.cpp
- index fedff82..b91d504 100755
- --- a/Rds/RdsLib/RdsDataStream.cpp
- +++ b/Rds/RdsLib/RdsDataStream.cpp
- @@ -847,10 +847,15 @@ RINLINE T_Bool C_RdsDataStream::readStream(C_RdsLoadFlags &_roLoadFlags,
- const T_Bool _bAutoNvRamInit /* = false */,
- const T_Name _SRamFileName /* = NULL */) {
- T_Bool bOk = true;
- + locale_t saved_locale;
- + locale_t posix_locale;
- - // Set LC_NUMERIC to C because the real values are written as 123.456
- - std::string oldLocale(setlocale(LC_NUMERIC, nullptr));
- - setlocale(LC_NUMERIC, "C");
- + /*
- + * Use POSIX locale because the code below uses functions which
- + * parse floating-poing values using locale-specific functions
- + */
- + posix_locale = rov_get_posix_localeobj();
- + saved_locale = uselocale(posix_locale);
- if (0 == _pRdsAbsDirectory)
- _pRdsAbsDirectory = this;
- @@ -975,7 +980,7 @@ RINLINE T_Bool C_RdsDataStream::readStream(C_RdsLoadFlags &_roLoadFlags,
- m_poRdsLocalClientID, _fileName,
- onlyOnRdf.c_str(),
- m_sActFileName.c_str());
- - setlocale(LC_NUMERIC, oldLocale.c_str());
- + (void)uselocale(saved_locale);
- bOk = false;
- return (bOk);
- }
- @@ -1024,7 +1029,7 @@ RINLINE T_Bool C_RdsDataStream::readStream(C_RdsLoadFlags &_roLoadFlags,
- poRdsError->m_iLine);
- assert(checkTree(_pRdsAbsDirectory));
- // delete[] szBuffer;
- - setlocale(LC_NUMERIC, oldLocale.c_str());
- + (void)uselocale(saved_locale);
- return (bOk);
- } // end of C_RdsDataStream::readStream
- diff --git a/Rds/RdsLib/RdsReal.cpp b/Rds/RdsLib/RdsReal.cpp
- index c8f28b2..8f5e72e 100755
- --- a/Rds/RdsLib/RdsReal.cpp
- +++ b/Rds/RdsLib/RdsReal.cpp
- @@ -44,6 +44,8 @@
- #include "RdsClientLocal.h"
- #include "DExtMath.hpp"
- +#include <locale.h>
- +
- #define RINLINE
- @@ -1539,10 +1541,15 @@ RINLINE T_Bool C_RdsUnit::readUnitSizeFile(const string &_path)
- {
- T_Bool isOK = true;
- - char savedLocale[32];
- - /* Get the name of the current locale. */
- - strncpy(savedLocale, setlocale (LC_NUMERIC, NULL), 32);
- - setlocale(LC_NUMERIC, "C");
- + locale_t saved_locale;
- + locale_t posix_locale;
- +
- + /*
- + * Use POSIX locale because the code below uses functions which
- + * parse floating-poing values using locale-specific functions
- + */
- + posix_locale = rov_get_posix_localeobj();
- + saved_locale = uselocale(posix_locale);
- string completeFileName(SIZE_AND_UNIT_DIR);
- if ( !_path.empty() )
- @@ -1617,7 +1624,9 @@ RINLINE T_Bool C_RdsUnit::readUnitSizeFile(const string &_path)
- poSuRdsError->perrorLog (errorMsg.str().c_str());
- isOK = false;
- } // end else !inStream
- - setlocale(LC_NUMERIC, savedLocale);
- +
- + (void)uselocale(saved_locale);
- +
- return(isOK);
- } // end of method C_RdsUnit::readUnitSizeFile
RDE: Fix hang in RDS code due to setlocale() abuse in multithreaded application
Posted by Anonymous on Fri 28th Apr 2023 14:55
raw | new post
view followups (newest first): RDE: Fix hang in RDS code due to setlocale() abuse in multithreaded application by Anonymous
modification of post by Anonymous (view diff)
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.