From 50352f1687ab2f02685a380668c889111cdeeee9 Mon Sep 17 00:00:00 2001 From: Felipe Gasper Date: Fri, 9 Apr 2021 23:08:39 -0400 Subject: Set %ENV keys using the same byte-string logic as setting %ENV values. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Issue #18636: This extends the work from 613c63b465f01af4e535fdc6c1c17e7470be5aad to %ENV keys. Previously if you assigned an upgraded string as a key in %ENV, the string’s internal PV representation was sent to the OS. Now the string is “soft downgraded” before being given to the OS; if the downgrade fails--i.e., if the string contains code points above 255--then a warning is printed, and the string’s utf8 is assigned to the environment, as happens with %ENV values. A new internal macro, MgSV, is created to facilitate this work. --- mg.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'mg.c') diff --git a/mg.c b/mg.c index b1a036ef7b..3355df1b4b 100644 --- a/mg.c +++ b/mg.c @@ -1269,9 +1269,24 @@ int Perl_magic_setenv(pTHX_ SV *sv, MAGIC *mg) { STRLEN len = 0, klen; - const char * const key = MgPV_const(mg,klen); + + const char *key; const char *s = ""; + SV *keysv = MgSV(mg); + + if (keysv == NULL) { + key = mg->mg_ptr; + klen = mg->mg_len; + } + else { + if (!sv_utf8_downgrade(keysv, /* fail_ok */ TRUE)) { + Perl_ck_warner_d(aTHX_ packWARN(WARN_UTF8), "Wide character in %s", "setenv key (encoding to utf8)"); + } + + key = SvPV_const(keysv,klen); + } + PERL_ARGS_ASSERT_MAGIC_SETENV; SvGETMAGIC(sv); -- cgit v1.2.1