From 1b9147d7db08a94dd00532bc611ac322c8ed3e57 Mon Sep 17 00:00:00 2001 From: Christopher Gilbert Date: Tue, 30 Aug 2016 12:29:37 +0100 Subject: Fixed a bug causing solc to crash on startup due to invalid environment settings for locale --- lllc/main.cpp | 11 +++++++++++ scripts/install_deps.sh | 22 ++++++---------------- solc/main.cpp | 11 +++++++++++ 3 files changed, 28 insertions(+), 16 deletions(-) diff --git a/lllc/main.cpp b/lllc/main.cpp index a4c92d67..46416365 100644 --- a/lllc/main.cpp +++ b/lllc/main.cpp @@ -22,6 +22,7 @@ #include #include +#include #include #include #include @@ -52,10 +53,20 @@ void version() exit(0); } +void setEnv() { + std::setlocale(LC_ALL, "C"); +#if !defined(WIN32) && !defined(MAC_OSX) && !defined(__FreeBSD__) && !defined(__OpenBSD__) + if (!std::setlocale(LC_ALL, "")) { + setenv("LC_ALL", "C", 1); + } +#endif +} + enum Mode { Binary, Hex, Assembly, ParseTree, Disassemble }; int main(int argc, char** argv) { + setEnv(); unsigned optimise = 1; string infile; Mode mode = Hex; diff --git a/scripts/install_deps.sh b/scripts/install_deps.sh index 4fb948ed..a67fea82 100755 --- a/scripts/install_deps.sh +++ b/scripts/install_deps.sh @@ -98,7 +98,7 @@ case $(uname -s) in brew update brew upgrade - + brew install boost brew install cmake brew install jsoncpp @@ -127,14 +127,14 @@ case $(uname -s) in #------------------------------------------------------------------------------ # Linux #------------------------------------------------------------------------------ - + Linux) case $(detect_linux_distro) in #------------------------------------------------------------------------------ # Arch Linux #------------------------------------------------------------------------------ - + Arch) #Arch echo "Installing solidity dependencies on Arch Linux." @@ -143,7 +143,7 @@ case $(uname -s) in # See https://wiki.archlinux.org/index.php/Official_repositories sudo pacman -Sy \ base-devel \ - boost \ + boost \ cmake \ git \ ;; @@ -158,7 +158,7 @@ case $(uname -s) in # All our dependencies can be found in the Alpine Linux official repositories. # See https://pkgs.alpinelinux.org/ - + apk update apk add boost-dev build-base cmake jsoncpp-dev @@ -231,7 +231,7 @@ case $(uname -s) in # Install "normal packages" # See https://fedoraproject.org/wiki/Package_management_system. dnf install \ - autoconf \ + autoconf \ automake \ boost-devel \ cmake \ @@ -326,16 +326,6 @@ case $(uname -s) in sudo apt-get -y update sudo apt-get -y install eth - # And install the English language package and reconfigure the locales. - # We really shouldn't need to do this, and should instead force our locales to "C" - # within our application runtimes, because this issue shows up on multiple Linux distros, - # and each will need fixing in the install steps, where we should really just fix it once - # in the code. - # - # See https://github.com/ethereum/webthree-umbrella/issues/169 - sudo apt-get -y install language-pack-en-base - sudo dpkg-reconfigure locales - ;; *) diff --git a/solc/main.cpp b/solc/main.cpp index eaada1c4..bc8bdc81 100644 --- a/solc/main.cpp +++ b/solc/main.cpp @@ -21,13 +21,24 @@ */ #include "CommandLineInterface.h" +#include #include #include using namespace std; +void setEnv() { + std::setlocale(LC_ALL, "C"); +#if !defined(WIN32) && !defined(MAC_OSX) && !defined(__FreeBSD__) && !defined(__OpenBSD__) + if (!std::setlocale(LC_ALL, "")) { + setenv("LC_ALL", "C", 1); + } +#endif +} + int main(int argc, char** argv) { + setEnv(); dev::solidity::CommandLineInterface cli; if (!cli.parseArguments(argc, argv)) return 1; -- cgit v1.2.3 From a91bca7937e7f6ffdc84c998eade6d38fb08cbb9 Mon Sep 17 00:00:00 2001 From: Christopher Gilbert Date: Tue, 30 Aug 2016 16:13:21 +0100 Subject: Code review changes: stylistic changes, and removed redundant call to set locale. --- lllc/main.cpp | 7 ++++--- solc/main.cpp | 7 ++++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/lllc/main.cpp b/lllc/main.cpp index 46416365..ed91c0a2 100644 --- a/lllc/main.cpp +++ b/lllc/main.cpp @@ -53,10 +53,11 @@ void version() exit(0); } -void setEnv() { - std::setlocale(LC_ALL, "C"); +void setEnv() +{ #if !defined(WIN32) && !defined(MAC_OSX) && !defined(__FreeBSD__) && !defined(__OpenBSD__) - if (!std::setlocale(LC_ALL, "")) { + if (!std::setlocale(LC_ALL, "")) + { setenv("LC_ALL", "C", 1); } #endif diff --git a/solc/main.cpp b/solc/main.cpp index bc8bdc81..ce787574 100644 --- a/solc/main.cpp +++ b/solc/main.cpp @@ -27,10 +27,11 @@ using namespace std; -void setEnv() { - std::setlocale(LC_ALL, "C"); +void setEnv() +{ #if !defined(WIN32) && !defined(MAC_OSX) && !defined(__FreeBSD__) && !defined(__OpenBSD__) - if (!std::setlocale(LC_ALL, "")) { + if (!std::setlocale(LC_ALL, "")) + { setenv("LC_ALL", "C", 1); } #endif -- cgit v1.2.3 From 68bd463bea5d5a17b90e76ee7f1bff0593775ca8 Mon Sep 17 00:00:00 2001 From: Christopher Gilbert Date: Tue, 30 Aug 2016 20:32:30 +0100 Subject: Code review changes: Renamed function and added some documentation about what the function does and why --- lllc/main.cpp | 19 ++++++++++++++++--- solc/main.cpp | 19 ++++++++++++++++--- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/lllc/main.cpp b/lllc/main.cpp index ed91c0a2..ecd0de99 100644 --- a/lllc/main.cpp +++ b/lllc/main.cpp @@ -53,9 +53,22 @@ void version() exit(0); } -void setEnv() +/* +The equivalent of setlocale(LC_ALL, ā€œCā€) is called before any user code is run. +If the user has an invalid environment setting then it is possible for the call +to set locale to fail, so there are only two possible actions, the first is to +throw a runtime exception and cause the program to quit (default behaviour), +or the second is to modify the environment to something sensible (least +surprising behaviour). + +The follow code produces the least surprising behaviour. It will use the user +specified default locale if it is valid, and if not then it will modify the +environment the process is running in to use a sensible default. This also means +that users do not need to install language packs for their OS. +*/ +void setDefaultOrCLocale() { -#if !defined(WIN32) && !defined(MAC_OSX) && !defined(__FreeBSD__) && !defined(__OpenBSD__) +#if __unix__ if (!std::setlocale(LC_ALL, "")) { setenv("LC_ALL", "C", 1); @@ -67,7 +80,7 @@ enum Mode { Binary, Hex, Assembly, ParseTree, Disassemble }; int main(int argc, char** argv) { - setEnv(); + setDefaultOrCLocale(); unsigned optimise = 1; string infile; Mode mode = Hex; diff --git a/solc/main.cpp b/solc/main.cpp index ce787574..11facfa6 100644 --- a/solc/main.cpp +++ b/solc/main.cpp @@ -27,9 +27,22 @@ using namespace std; -void setEnv() +/* +The equivalent of setlocale(LC_ALL, ā€œCā€) is called before any user code is run. +If the user has an invalid environment setting then it is possible for the call +to set locale to fail, so there are only two possible actions, the first is to +throw a runtime exception and cause the program to quit (default behaviour), +or the second is to modify the environment to something sensible (least +surprising behaviour). + +The follow code produces the least surprising behaviour. It will use the user +specified default locale if it is valid, and if not then it will modify the +environment the process is running in to use a sensible default. This also means +that users do not need to install language packs for their OS. +*/ +void setDefaultOrCLocale() { -#if !defined(WIN32) && !defined(MAC_OSX) && !defined(__FreeBSD__) && !defined(__OpenBSD__) +#if __unix__ if (!std::setlocale(LC_ALL, "")) { setenv("LC_ALL", "C", 1); @@ -39,7 +52,7 @@ void setEnv() int main(int argc, char** argv) { - setEnv(); + setDefaultOrCLocale(); dev::solidity::CommandLineInterface cli; if (!cli.parseArguments(argc, argv)) return 1; -- cgit v1.2.3