@@ -775,29 +775,30 @@ PHP_FUNCTION(getenv)
775
775
}
776
776
777
777
SetLastError (0 );
778
- /*If the given buffer is not large enough to hold the data, the return value is
778
+ /* If the given buffer is not large enough to hold the data, the return value is
779
779
the buffer size, in characters, required to hold the string and its terminating
780
780
null character. We use this return value to alloc the final buffer. */
781
781
size = GetEnvironmentVariableW (keyw , & dummybuf , 0 );
782
- if (GetLastError () == ERROR_ENVVAR_NOT_FOUND ) {
782
+ switch (size ) {
783
+ case 0 :
783
784
/* The environment variable doesn't exist. */
785
+ ZEND_ASSERT (GetLastError () == ERROR_ENVVAR_NOT_FOUND );
784
786
free (keyw );
785
787
RETURN_FALSE ;
786
- }
787
-
788
- if (size == 0 ) {
788
+ case 1 :
789
789
/* env exists, but it is empty */
790
790
free (keyw );
791
791
RETURN_EMPTY_STRING ();
792
792
}
793
793
794
- valw = emalloc (( size + 1 ) * sizeof (wchar_t ));
794
+ valw = emalloc (size * sizeof (wchar_t ));
795
795
size = GetEnvironmentVariableW (keyw , valw , size );
796
796
if (size == 0 ) {
797
- /* has been removed between the two calls */
798
- free (keyw );
799
- efree (valw );
800
- RETURN_EMPTY_STRING ();
797
+ /* has been removed between the two calls */
798
+ ZEND_ASSERT (GetLastError () == ERROR_ENVVAR_NOT_FOUND );
799
+ free (keyw );
800
+ efree (valw );
801
+ RETURN_FALSE ;
801
802
} else {
802
803
ptr = php_win32_cp_w_to_any (valw );
803
804
RETVAL_STRING (ptr );
@@ -841,7 +842,6 @@ PHP_FUNCTION(putenv)
841
842
#ifdef PHP_WIN32
842
843
char * value = NULL ;
843
844
int equals = 0 ;
844
- int error_code ;
845
845
#endif
846
846
847
847
ZEND_PARSE_PARAMETERS_START (1 , 1 )
@@ -896,38 +896,33 @@ PHP_FUNCTION(putenv)
896
896
unsetenv (pe .putenv_string );
897
897
}
898
898
if (!p || putenv (pe .putenv_string ) == 0 ) { /* success */
899
- #else
900
- # ifndef PHP_WIN32
899
+ #elif !defined(PHP_WIN32)
901
900
if (putenv (pe .putenv_string ) == 0 ) { /* success */
902
- # else
903
- wchar_t * keyw , * valw = NULL ;
904
-
905
- keyw = php_win32_cp_any_to_w (pe .key );
906
- if (value ) {
907
- valw = php_win32_cp_any_to_w (value );
908
- }
909
- /* valw may be NULL, but the failed conversion still needs to be checked. */
910
- if (!keyw || !valw && value ) {
911
- efree (pe .putenv_string );
912
- efree (pe .key );
913
- free (keyw );
914
- free (valw );
915
- RETURN_FALSE ;
916
- }
901
+ #else
902
+ wchar_t * keyw , * valw = NULL ;
917
903
918
- error_code = SetEnvironmentVariableW (keyw , valw );
904
+ keyw = php_win32_cp_any_to_w (pe .key );
905
+ if (value ) {
906
+ valw = php_win32_cp_any_to_w (value );
907
+ }
908
+ /* valw may be NULL, but the failed conversion still needs to be checked. */
909
+ if (!keyw || !valw && value ) {
910
+ efree (pe .putenv_string );
911
+ efree (pe .key );
912
+ free (keyw );
913
+ free (valw );
914
+ RETURN_FALSE ;
915
+ }
919
916
920
- if (error_code != 0
921
- # ifndef ZTS
922
- /* We need both SetEnvironmentVariable and _putenv here as some
923
- dependency lib could use either way to read the environment.
924
- Obviously the CRT version will be useful more often. But
925
- generally, doing both brings us on the safe track at least
926
- in NTS build. */
927
- && _wputenv_s (keyw , valw ? valw : L"" ) == 0
928
- # endif
917
+ /* While we prefer SetEnvironmentVariable, we also call putenv here as
918
+ some dependency lib could use getenv to read the environment, and there
919
+ is at least a chance that the lib sees the putenv changes.
920
+ For best compatibility with other systems, we have to call _putenv first,
921
+ because that cannot assign an empty string, while SetEnvironmentVariable
922
+ can, so at least GetEnvironmentVariable will return the proper result. */
923
+ if (_wputenv_s (keyw , valw ? valw : L"" ) == 0
924
+ && SetEnvironmentVariableW (keyw , valw ) != 0
929
925
) { /* success */
930
- # endif
931
926
#endif
932
927
zend_hash_str_add_mem (& BG (putenv_ht ), pe .key , pe .key_len , & pe , sizeof (putenv_entry ));
933
928
#ifdef HAVE_TZSET
0 commit comments