php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #81312 mb_convert_variables() corrupts reference of array element
Submitted: 2021-07-30 03:18 UTC Modified: 2021-08-02 14:33 UTC
From: heguangyu5 at qq dot com Assigned:
Status: Open Package: mbstring related
PHP Version: 7.4.22 OS: ubuntu
Private report: No CVE-ID: None
 [2021-07-30 03:18 UTC] heguangyu5 at qq dot com
Description:
------------
mb_convert_variables() should only convert enconding of array elements but not change the array structure.

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

$a = "‚ ‚¢‚€‚Š‚š";
$b = array(&$a);
$c = $b;

var_dump($a, $b, $c);

$c[0] .= "  abc";

var_dump($a, $b, $c);

mb_convert_variables("EUC-JP", "Shift_JIS", $c);
var_dump($a, $b, $c);

$c[0] .= "  def";

var_dump($a, $b, $c);

Expected result:
----------------
string(10) "‚ ‚¢‚€‚Š‚š"
array(1) {
  [0]=>
  &string(10) "‚ ‚¢‚€‚Š‚š"
}
array(1) {
  [0]=>
  &string(10) "‚ ‚¢‚€‚Š‚š"
}
string(15) "‚ ‚¢‚€‚Š‚š  abc"
array(1) {
  [0]=>
  &string(15) "‚ ‚¢‚€‚Š‚š  abc"
}
array(1) {
  [0]=>
  &string(15) "‚ ‚¢‚€‚Š‚š  abc"
}
string(15) "€¢€€€Š€š€ª  abc"
array(1) {
  [0]=>
  &string(15) "€¢€€€Š€š€ª  abc"
}
array(1) {
  [0]=>
  &string(15) "€¢€€€Š€š€ª  abc"
}
string(20) "€¢€€€Š€š€ª  abc  def"
array(1) {
  [0]=>
  &string(20) "€¢€€€Š€š€ª  abc  def"
}
array(1) {
  [0]=>
  &string(20) "€¢€€€Š€š€ª  abc  def"
}

Actual result:
--------------
string(10) "‚ ‚¢‚€‚Š‚š"
array(1) {
  [0]=>
  &string(10) "‚ ‚¢‚€‚Š‚š"
}
array(1) {
  [0]=>
  &string(10) "‚ ‚¢‚€‚Š‚š"
}
string(15) "‚ ‚¢‚€‚Š‚š  abc"
array(1) {
  [0]=>
  &string(15) "‚ ‚¢‚€‚Š‚š  abc"
}
array(1) {
  [0]=>
  &string(15) "‚ ‚¢‚€‚Š‚š  abc"
}
string(15) "‚ ‚¢‚€‚Š‚š  abc"
array(1) {
  [0]=>
  &string(15) "‚ ‚¢‚€‚Š‚š  abc"
}
array(1) {
  [0]=>
  string(15) "€¢€€€Š€š€ª  abc"
}
string(15) "‚ ‚¢‚€‚Š‚š  abc"
array(1) {
  [0]=>
  &string(15) "‚ ‚¢‚€‚Š‚š  abc"
}
array(1) {
  [0]=>
  string(20) "€¢€€€Š€š€ª  abc  def"
}

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-08-02 14:33 UTC] nikic@php.net
The current behavior is tested in https://github.com/php/php-src/blob/master/ext/mbstring/tests/bug26639.phpt.

Though the original bug report (https://bugs.php.net/bug.php?id=26639) doesn't talk about references, but only missing copy-on-write.

I'm includes to agree that the current behavior is incorrect.
 [2024-05-29 09:02 UTC] cassie2698bratt at outlook dot com
Hello,

The mb_convert_variables function is supposed to convert the encoding of elements within an array but unintentionally modifies the array structure when references are involved.

Expected Behavior:

The function should change the character encoding of the string within the array element ($a) but not modify the reference itself ($b).
Observed Behavior:

Converting the encoding with references ($c) corrupts the reference, causing subsequent modifications to $c[0] to also affect $a and $b.
Example:

The provided test script demonstrates the unexpected behavior. Initially, the array ($b) holds a reference to the string ($a). However, after using mb_convert_variables with references, appending to $c[0] also modifies $a and $b.

Impact:

This bug can lead to unintended side effects when working with arrays and character encoding conversions, especially when references are involved.

Current Status: (https://github.com)(https://www.marykayin-touch.com)

The bug report was submitted in July 2021 and appears to be open as of today (May 29, 2024). This suggests it might not be fixed in the current PHP versions.

Possible Solutions:

Avoid references: If possible, work with a copy of the array instead of references when using mb_convert_variables.
Manual conversion: If references are necessary, consider manually converting the encoding of each element within the array after creating a copy.

I hope the information may helps you.
 [2024-07-03 07:40 UTC] marykayintouch53 at gmail dot com
Your insights are extremely valuable and remarkable. Thank you for sharing!
(https://github.com)(https://marykayintouch.cloud/marykayintouch-login/)
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 11:01:30 2024 UTC