From c4ad8beaa890b931031a5cf8a1d2d38550fca3af Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Wed, 29 Apr 2020 11:51:50 +0200 Subject: [PATCH] Do not inherit LC_CTYPE locale from environment Treatment of locales in PHP is currently inconsistent: The LC_ALL locale is set to "C", as is standard behavior on program startup. The LC_CTYPE locale is set to "", which will inherit it from the environment. However, the inherited LC_CTYPE locale will only be used in some cases, while in other cases it is necessary to perform an explicit setlocale() call in PHP first. This is the case for the locale-sensitive handling in the PCRE extension. Make things consistent by *never* inheriting any locales from the environment. LC_ALL, including LC_CTYPE will be "C" on startup. A locale can be set or inherited through an explicit setlocale() call, at which point the behavior will be fully consistent and predictable. Closes GH-5488. --- UPGRADING | 6 +++++ Zend/tests/lc_ctype_inheritance.phpt | 25 ++++++++++++++++++ ext/snmp/snmp.c | 3 +++ ext/standard/basic_functions.c | 1 - .../tests/strings/strtolower-win32.phpt | Bin 4406 -> 4455 bytes .../tests/strings/strtoupper1-win32.phpt | Bin 4415 -> 4464 bytes main/main.c | 1 - 7 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 Zend/tests/lc_ctype_inheritance.phpt diff --git a/UPGRADING b/UPGRADING index 96b9745d4c..982ebe635c 100644 --- a/UPGRADING +++ b/UPGRADING @@ -397,6 +397,12 @@ PHP 8.0 UPGRADE NOTES . If the array returned by __sleep() contains non-existing properties, these are now silently ignored. Previously, such properties would have been serialized as if they had the value NULL. + . The default locale on startup is now always "C". No locales are inherited + from the environment by default. Previously, LC_ALL was set to "C", while + LC_CTYPE was inherited from the environment. However, some functions did not + respect the inherited locale without an explicit setlocale() call. An + explicit setlocale() call is now always required if you wish to change any + locale component from the default. - tidy: . The $use_include_path parameter, which was not used internally, has been diff --git a/Zend/tests/lc_ctype_inheritance.phpt b/Zend/tests/lc_ctype_inheritance.phpt new file mode 100644 index 0000000000..8971ff1969 --- /dev/null +++ b/Zend/tests/lc_ctype_inheritance.phpt @@ -0,0 +1,25 @@ +--TEST-- +Do not inherit LC_CTYPE from environment +--SKIPIF-- + +--ENV-- +LC_CTYPE=de_DE +--FILE-- + +--EXPECT-- +string(1) "C" +string(2) "e4" +int(0) +bool(true) +string(2) "c4" +int(1) diff --git a/ext/snmp/snmp.c b/ext/snmp/snmp.c index 8464e74aad..d5cf7cce04 100644 --- a/ext/snmp/snmp.c +++ b/ext/snmp/snmp.c @@ -49,6 +49,7 @@ #ifdef HAVE_UNISTD_H #include #endif +#include #ifndef __P #ifdef __GNUC__ @@ -1971,6 +1972,8 @@ PHP_MINIT_FUNCTION(snmp) le_snmp_session = zend_register_list_destructors_ex(php_snmp_session_destructor, NULL, PHP_SNMP_SESSION_RES_NAME, module_number); init_snmp("snmpapp"); + /* net-snmp corrupts the CTYPE locale during initialization. */ + setlocale(LC_CTYPE, "C"); #ifdef NETSNMP_DS_LIB_DONT_PERSIST_STATE /* Prevent update of the snmpapp.conf file */ diff --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c index 32a82aae26..5fa7be86db 100755 --- a/ext/standard/basic_functions.c +++ b/ext/standard/basic_functions.c @@ -516,7 +516,6 @@ PHP_RSHUTDOWN_FUNCTION(basic) /* {{{ */ * to the value in startup environment */ if (BG(locale_changed)) { setlocale(LC_ALL, "C"); - setlocale(LC_CTYPE, ""); zend_update_current_locale(); if (BG(locale_string)) { zend_string_release_ex(BG(locale_string), 0); diff --git a/ext/standard/tests/strings/strtolower-win32.phpt b/ext/standard/tests/strings/strtolower-win32.phpt index ff631754df6a89026cf0da44c630ad5580a2ccb2..2fc693641dbb38171e652a85e6e792731b65d8ba 100644 GIT binary patch delta 87 zcmdm{^jv9z`9uqO35A*(1;ygjlAQeH#GF(OALn@IkjMa6or&dg6Q}7-tdQYSNGwat Y%t=hjNuBt3wJ>(2jMkfb7+3KF0A5NT00000 delta 66 zcmaE^v`uM(Iitozb9n_#g_;@#jpEdjoc!d(oKy`T=XmFk$N*Oz1tld-1x4G5d2);j W6MOWS5{oD1$xeK*dh>q975o6Wj}@^1 diff --git a/ext/standard/tests/strings/strtoupper1-win32.phpt b/ext/standard/tests/strings/strtoupper1-win32.phpt index 7240e5c021dc89381c0d70f4f82e4328b1a806fb..619ab51d860273686c8af0d325f206a5cdd06984 100644 GIT binary patch delta 87 zcmdn5^g(HY`9uqO35A*(1;ygjlAQeH#GF(OALn@IkjMa6or&dg6Q}7-tdQYSNGwat Y%t=hjNuBt3tuS__jMkg`7&r0*0A#8k9RL6T delta 68 zcmeyMv|nk0Iitozb9n_#g_;@#jpEdjoc!d(oKy`T=XmFk$N*Oz1tld-1x4G*3mD}X X6(;uRF(noQ*|HNKtlfN&aXmi(