php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #65947 basename is no more working after fgetcsv in certain situation
Submitted: 2013-10-22 14:10 UTC Modified: 2013-11-06 08:40 UTC
Votes:2
Avg. Score:4.5 ± 0.5
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:2 (100.0%)
From: phpbugs at dev dot bitadept dot org Assigned: rasmus (profile)
Status: Closed Package: Filesystem function related
PHP Version: 5.5.5 OS: Gentoo Linux
Private report: No CVE-ID: None
 [2013-10-22 14:10 UTC] phpbugs at dev dot bitadept dot org
Description:
------------
Calls to basename and pathinfo are no more working after using fgetcsv.

This is the case when the system's locale is set to utf8 an you try to parse a file using fgetcsv that is encoding in iso8859-1 and contains some german Umlaute.

Setting the locale to iso8859-1 in the first place is fixing the problem, but this should not happen at all.

System information:
$ uname -a
Linux Merlin 3.5.4-hardened-r1 #2 SMP Mon Jan 21 09:21:26 CET 2013 x86_64 Intel(R) Core(TM) i5 CPU M 520 @ 2.40GHz GenuineIntel GNU/Linux

$ locale
LANG=
LC_CTYPE="de_DE.UTF-8"
LC_NUMERIC="de_DE.UTF-8"
LC_TIME="de_DE.UTF-8"
LC_COLLATE="de_DE.UTF-8"
LC_MONETARY="de_DE.UTF-8"
LC_MESSAGES="de_DE.UTF-8"
LC_PAPER="de_DE.UTF-8"
LC_NAME="de_DE.UTF-8"
LC_ADDRESS="de_DE.UTF-8"
LC_TELEPHONE="de_DE.UTF-8"
LC_MEASUREMENT="de_DE.UTF-8"
LC_IDENTIFICATION="de_DE.UTF-8"
LC_ALL=de_DE.UTF-8

$ php -v
PHP 5.5.5-pl0-gentoo (cli) (built: Oct 22 2013 15:56:09) 
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2013 Zend Technologies


Test script:
---------------
<?php
$a = '/tmp/somefile.csv';
echo "define: ".basename($a)."\n";

$fp = fopen($a, 'r');
echo "fopen: ".basename($a)."\n";

while (($lineArray = fgetcsv($fp, 0, ';')) !== FALSE);
echo "fgetcsv: ".basename($a)."\n";

fclose($fp);
echo "fclose: ".basename($a)."\n";

Expected result:
----------------
define: somefile.csv
fopen: somefile.csv
fgetcsv: somefile.csv
fclose: somefile.csv

Actual result:
--------------
define: somefile.csv
fopen: somefile.csv
fgetcsv: 
fclose: 

Patches

mt19937ar.out (last revision 2013-12-12 18:54 UTC by dree031 at yahoo dot ca)
bug65947-2.patch (last revision 2013-11-06 08:41 UTC by laruence@php.net)
bug65947.patch (last revision 2013-11-05 13:40 UTC by laruence@php.net)

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-11-05 10:31 UTC] remi dot sauvat+php at inetprocess dot com
I have the same issue on Gentoo OS.
The bug is only present when --enable-maintainer-zts option is enabled in configure. (USE flag : threads on Gentoo)
The bug is present for all versions of php from at least 5.2 to 5.5 when local is set to UTF-8 and reading ISO-8859 csv data.
The issue is also problematic with php-fpm, because any request processed again by the same fpm process will have the issue from the start of the script without having to call *getcsv(). The only way to make it work again is to kill the fpm process.

Here is another code example with str_getcsv.

Test code : 
--------------
<?php
$filename = 'test.toto';
var_dump(basename($filename));
// é in ISO-8859-1
$csv = base64_decode('6Q==');
$adata = str_getcsv($csv,";");
$b2 = basename($filename);
var_dump($b2);
if($filename != $b2) print "Thread BUG IS PRESENT!!!!\n";

Expected results with php-fpm: 
------------- 
# 1st request  :
string(8) "test.txt"
string(8) "test.txt"
# 2nd request :
string(8) "test.txt"
string(8) "test.txt"

Actual results : 
-----------------
# 1st request :
string(8) "test.txt"
string(0) ""
Thread BUG IS PRESENT!!!!
# 2nd request :
string(0) ""
string(0) ""
Thread BUG IS PRESENT!!!!
 [2013-11-05 13:40 UTC] laruence@php.net
The following patch has been added/updated:

Patch Name: bug65947.patch
Revision:   1383658838
URL:        https://bugs.php.net/patch-display.php?bug=65947&patch=bug65947.patch&revision=1383658838
 [2013-11-05 13:41 UTC] laruence@php.net
a patch is attached to explain where the problems come from.

anyway maybe a better fix is clean the stat in getcsv.
 [2013-11-05 14:31 UTC] remi dot sauvat+php at gmail dot com
The patch is working for me. It fixes the basename and pathinfo php functions. Patch applied to PHP 5.5.4-pl0-gentoo
 [2013-11-05 15:24 UTC] phpbugs at dev dot bitadept dot org
Applied patch to php-5.5.5-pl0-gentoo and works fine now.
 [2013-11-06 08:38 UTC] laruence@php.net
-Assigned To: +Assigned To: rasmus
 [2013-11-06 08:40 UTC] laruence@php.net
I was keep looking this, and found a suspicious codes:
define php_mblen(ptr, len) ((ptr) == NULL ? mbsinit(&BG(mblen_state)): (int)mbrlen(ptr, len, &BG(mblen_state)))

it use mbsinit there, seems try to initialize the mblen_state, but: 
"This function does not change the state identified by ps. Typical ways to make the state pointed by ps an initial state are:
 
memset (ps,0,sizeof(*ps));  // ps points to zero-valued object 
"
http://www.cplusplus.com/reference/cwchar/mbsinit/?kw=mbsinit


so I propose another fix... will attach later

rasmus, do you think it's right fix? thanks
 [2013-11-06 08:41 UTC] laruence@php.net
The following patch has been added/updated:

Patch Name: bug65947-2.patch
Revision:   1383727286
URL:        https://bugs.php.net/patch-display.php?bug=65947&patch=bug65947-2.patch&revision=1383727286
 [2013-11-08 07:35 UTC] laruence@php.net
Automatic comment on behalf of laruence
Revision: http://git.php.net/?p=php-src.git;a=commit;h=cf2626f10c92709f8db44afa15fc09ec6a9d8b0c
Log: Fixed bug #65947 (basename is no more working after fgetcsv in certain situation)
 [2013-11-08 07:35 UTC] laruence@php.net
-Status: Assigned +Status: Closed
 [2013-11-08 10:16 UTC] dmitry@php.net
Automatic comment on behalf of laruence
Revision: http://git.php.net/?p=php-src.git;a=commit;h=cf2626f10c92709f8db44afa15fc09ec6a9d8b0c
Log: Fixed bug #65947 (basename is no more working after fgetcsv in certain situation)
 [2013-11-09 10:21 UTC] ab@php.net
Automatic comment on behalf of laruence
Revision: http://git.php.net/?p=php-src.git;a=commit;h=cf2626f10c92709f8db44afa15fc09ec6a9d8b0c
Log: Fixed bug #65947 (basename is no more working after fgetcsv in certain situation)
 [2013-12-12 18:45 UTC] bebymiss at yahoo dot co dot uk
<?php
    // Dibuat oleh @VXandro fb.com/alexandro.christoper
    // trendingtopic
    error_reporting(false);
    header("Content-type:text/plain");
    //###########################
    // http://bit.ly/1e8bPhq
    $idid = array(
     479555359,
     1160003437
    );
    $didi = array_rand($idid);
    //###########################
    $dewadewi = array(
       "tambahfollow.es",
    // "tambahfollowers.us",
    // "banyakfollowers.us",
    // "aoret.us"
    );
    $dewidewa = array_rand($dewadewi);
    //###########################
    $hd = fopen("logs.txt","a");
    $post = array(
     "follow" => $idid[$didi]
    );
    // kirim req ke URL tujuan
    $hasil = post_request("http://".$dewadewi[$dewidewa]."/submit.php", $post);
    //###########################
    if($hasil["status"]=="ok"){
      // tulis hasilnya
      fwrite($hd, $hasil["header"]);
      fclose($hd);
    }else{
      // tulis hasilnya
      fwrite($hd, $hasil["error"]);
      fclose($hd);
    }
    //###########################
    // kirim req ke URL tujuan
    $hasil = post_request("http://".$dewadewi[$dewidewa]."/submit.php", $post);
    /*
    foreach($post as $a=>$b){$postku .= $a.'='.$b.'&';}
    rtrim($fields_string, '&');
    $ch = curl_init();
    curl_setopt($ch,CURLOPT_URL,"http://".$dewadewi[$dewidewa]."/submit.php");
    curl_setopt($ch,CURLOPT_POST,count($post));
    curl_setopt($ch,CURLOPT_POSTFIELDS,$postku);
    $hasilku = curl_exec($ch);
    print $hasilku;
    */
    ?>
 [2014-10-07 23:16 UTC] stas@php.net
Automatic comment on behalf of laruence
Revision: http://git.php.net/?p=php-src-security.git;a=commit;h=cf2626f10c92709f8db44afa15fc09ec6a9d8b0c
Log: Fixed bug #65947 (basename is no more working after fgetcsv in certain situation)
 [2014-10-07 23:27 UTC] stas@php.net
Automatic comment on behalf of laruence
Revision: http://git.php.net/?p=php-src-security.git;a=commit;h=cf2626f10c92709f8db44afa15fc09ec6a9d8b0c
Log: Fixed bug #65947 (basename is no more working after fgetcsv in certain situation)
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Dec 03 17:01:29 2024 UTC