namecoin / namecoin-legacy

Legacy client. New version here: https://github.com/namecoin/namecoin-core Note the release branch! - Official website:
https://namecoin.org
MIT License
448 stars 177 forks source link

Locale should default to C / Posix Environment #72

Closed itoffshore closed 1 year ago

itoffshore commented 10 years ago

Namecoin should catch the following error in init.cpp when locales are unknown:

EXCEPTION: St13runtime_error       
locale::facet::_S_create_c_locale name not valid       
namecoin in AppInit()       

terminate called after throwing an instance of 'std::runtime_error'
  what():  locale::facet::_S_create_c_locale name not valid

rfc1123Time() in bitcoinrpc.cpp sets a default C environment but this can be overridden by misconfigured or unknown locales.

Namecoin should default to a C / POSIX environment if the locales are unknown / do not exist (e.g Busybox / Embedded).

itoffshore commented 10 years ago

This issue can be fixed with the following patch:

see https://github.com/namecoin/namecoin/issues/72#issuecomment-42764353

domob1812 commented 10 years ago

Looks good to me. I've had similar issues (with a different project, though), which could be fixed in the same way, already. But please everyone on different systems (@phelix, for instance) make sure that this doesn't introduce compilation problems.

phelixbtc commented 10 years ago

init.cpp: In function 'int main(int, char**)': init.cpp:102:28: error: 'setenv' was not declared in this scope Should maybe use #ifndef WIN32 or something.

itoffshore commented 10 years ago

This revised patch works on Linux

see https://github.com/namecoin/namecoin/issues/72#issuecomment-42764353

phelixbtc commented 10 years ago

Compiles on Windows. ACK

itoffshore commented 10 years ago

After a bit more investigation the error was caused by my ~/,profile setting $LANG (with no language files existing) - the bitcoin developers may be adding some code to ignore unknown locales. https://github.com/bitcoin/bitcoin/issues/4147#issuecomment-42518271

itoffshore commented 10 years ago

Revised patch - the fix is now moved into SetUnixEnvironment()

--- namecoin-vQ.3.72/src/init.cpp
+++ namecoin-vQ.3.72/src.new/init.cpp
@@ -16,6 +16,8 @@

 void rescanfornames();

+void SetUnixEnvironment();
+
 CWallet* pwalletMain;

 //////////////////////////////////////////////////////////////////////////////
@@ -98,6 +100,10 @@
 #ifndef GUI
 int main(int argc, char* argv[])
 {
+    #ifndef WIN32
+       SetUnixEnvironment();
+    #endif
+
     bool fRet = false;
     fRet = AppInit(argc, argv);

--- namecoin-vQ.3.72/src/qt/bitcoin.cpp
+++ namecoin-vQ.3.72/src.new/qt/bitcoin.cpp
@@ -38,6 +38,8 @@
 Q_IMPORT_PLUGIN(qtaccessiblewidgets)
 #endif

+void SetUnixEnvironment();
+
 // Declare meta types used for QMetaObject::invokeMethod
 Q_DECLARE_METATYPE(bool*)

@@ -122,6 +124,10 @@
 #ifndef BITCOIN_QT_TEST
 int main(int argc, char *argv[])
 {
+    #ifndef WIN32
+       SetUnixEnvironment();
+    #endif
+
     // Command-line options take precedence:
     ParseParameters(argc, argv);

--- namecoin-vQ.3.72/src/util.cpp
+++ namecoin-vQ.3.72/src.new/util.cpp
@@ -1101,3 +1101,14 @@
     else
         return SoftSetArg(strArg, std::string("0"));
 }
+
+void SetUnixEnvironment()
+{
+    try
+     {
+         boost::filesystem::path::codecvt(); // Raises runtime error if current    locale is invalid
+      } catch(std::runtime_error &e)
+     {
+         setenv("LC_ALL", "C", 1); // Force C locale
+     }
+}
itoffshore commented 10 years ago

Am going through automated testing on Bitcoin - will send a pull request for Namecoin once the function is ready.

phelixbtc commented 10 years ago

Any news?

itoffshore commented 10 years ago

Created Pull Request https://github.com/namecoin/namecoin/pull/176

itoffshore commented 1 year ago

merged in https://github.com/namecoin/namecoin-legacy/pull/176