[prev in list] [next in list] [prev in thread] [next in thread] 

List:       php-doc-bugs
Subject:    [DOC-BUGS] Doc #79875 [Com]: getenv() can't get the right binary string passed by putenv()
From:       "paulbalandan at gmail dot com" <php-bugs () lists ! php ! net>
Date:       2020-07-19 12:41:24
Message-ID: E1jx8dA-0001ck-V6 () bugs ! php ! net
[Download RAW message or body]

Edit report at https://bugs.php.net/bug.php?id=79875&edit=1

 ID:                 79875
 Comment by:         paulbalandan at gmail dot com
 Reported by:        paulbalandan at gmail dot com
 Summary:            getenv() can't get the right binary string passed by
                     putenv()
 Status:             Verified
 Type:               Documentation Problem
 Package:            *General Issues
 Operating System:   Windows 10
 PHP Version:        7.4.8
 Block user comment: N
 Private report:     N

 New Comment:

Thank you for that excellent explanation.

One interesting note to add is that once you did that, the binary string is now \
converted to UTF-8 and cannot be blindly compared against the raw binary string. You \
need to convert it back to ANSI codepage (1252).

<?php
$bin = hex2bin('f699c7fd18a8e082d0228932f3acd40e1ef5ef92efcedda32842a211d62f0aa6');
$bin1 = sapi_windows_cp_conv(1252, 65001, $bin); // converted to UTF-8
putenv("key=$bin1");
$bin2 = getenv('key', true);
$bin3 = sapi_windows_cp_conv(65001, 1252, $bin2); // converted back to ANSI

var_dump(bin2hex($bin1) === bin2hex($bin2)); // bool(true)
var_dump(bin2hex($bin) === bin2hex($bin2)); // bool(false)
var_dump(bin2hex($bin) === bin2hex($bin3)); // bool(true)

And yes, this definitely needs to be documented.


Previous Comments:
------------------------------------------------------------------------
[2020-07-18 20:13:59] cmb@php.net

As of PHP 7.1.0, PHP on Windows has transparent support for
Unicode file paths[1], and that also applies to environment
variables (the values and the keys).  Since environment variables
are usually character strings (and not general binary strings),
this increases interoperability overall.  I would suggest not to
store arbitrary binary strings as values of environment variables,
but as character strings instead (e.g. in this case you could
store the hexadecimal string in the first place, and hex2bin() it
after retrieval).  If you absolutely need to store such binary
strings in environment variables, you should convert them first to
Unicode (i.e. UTF-8, because UTF-16 and UTF-32 code pages are only
available to managed applications[2], and UTF-7 is … to be
avoided), e.g.

    <?php
    $bin = sapi_windows_cp_conv(
        sapi_windows_cp_get('ansi'),
        65001, // UTF-8 codepage
        hex2bin('f699c7fd18a8e082d0228932f3acd40e1ef5ef92efcedda32842a211d62f0aa6')
    );
    putenv("key=$bin");
    $bin2 = getenv('key', true);
    var_dump(bin2hex($bin) === bin2hex($bin2));
    ?>

So this is expected, but currently undocumented behavior.

[1] <https://www.php.net/manual/en/migration71.windows-support.php#migration71.windows-support.long-and-utf8-path>
 [2] <https://docs.microsoft.com/en-us/windows/win32/intl/code-page-identifiers>

------------------------------------------------------------------------
[2020-07-18 17:02:03] paulbalandan at gmail dot com

Description:
------------
In Windows when posting binary string to the environment via putenv and then getting \
it back via getenv, the results are different.

Test script:
---------------
<?php

$bin = hex2bin('f699c7fd18a8e082d0228932f3acd40e1ef5ef92efcedda32842a211d62f0aa6');

putenv("key=$bin");

$bin2 = getenv('key', true);

var_dump(bin2hex($bin) === bin2hex($bin2));


Expected result:
----------------
bool(true)

Actual result:
--------------
bool(false)


------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=79875&edit=1

-- 
PHP Documentation Bugs Mailing List (https://www.php.net/)
To unsubscribe, visit: https://www.php.net/unsub.php


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic