php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #50104 $dbidx is not being closed in /include/ip-to-country.inc
Submitted: 2009-11-06 19:55 UTC Modified: 2009-11-14 01:00 UTC
From: ignas2526 at gmail dot com Assigned:
Status: No Feedback Package: Website problem
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: ignas2526 at gmail dot com
New email:
PHP Version: OS:

 

 [2009-11-06 19:55 UTC] ignas2526 at gmail dot com
Description:
------------
The fopened /backend/ip-to-country.idx in i2c_search_in_index function isn't fclosed anywhere.

Reproduce code:
---------------
http://lt2.php.net/source.php?url=/include/ip-to-country.inc
i2c_search_in_index function.

Expected result:
----------------
function i2c_search_in_index($ip)
{
    // Indexed part and record number to jump to
    $idxpart = 0; $recnum = 0;
   
    // Open the index file for reading
    $dbidx = fopen(
        $_SERVER['DOCUMENT_ROOT'] . "/backend/ip-to-country.idx",
        "r"
    );
    if (!$dbidx) { return FALSE; }

    // Read in granularity from index file and
    // convert current IP to something useful
    $granularity = intval(fgets($dbidx, 64));
    $ip_chunk = intval($ip / $granularity);

    // Loop till we can read the file
    while (!feof($dbidx)) {

        // Get CSV data from index file
        $data = fgetcsv($dbidx, 100);

        // Compare current index part with our IP
        if ($ip_chunk >= $idxpart && $ip_chunk < (int) $data[0]) {
            return array($recnum, (int) $data[1]);
        }

        // Store for next compare
        $idxpart = (int) $data[0];
        $recnum  = (int) $data[1];
    }

    // Close indexfile
    fclose($dbidx);

    // Return record number found
    return array($recnum, -1);
}

Actual result:
--------------
function i2c_search_in_index($ip)
{
    // Indexed part and record number to jump to
    $idxpart = 0; $recnum = 0;
   
    // Open the index file for reading
    $dbidx = fopen(
        $_SERVER['DOCUMENT_ROOT'] . "/backend/ip-to-country.idx",
        "r"
    );
    if (!$dbidx) { return FALSE; }

    // Read in granularity from index file and
    // convert current IP to something useful
    $granularity = intval(fgets($dbidx, 64));
    $ip_chunk = intval($ip / $granularity);

    // Loop till we can read the file
    while (!feof($dbidx)) {

        // Get CSV data from index file
        $data = fgetcsv($dbidx, 100);

        // Compare current index part with our IP
        if ($ip_chunk >= $idxpart && $ip_chunk < (int) $data[0]) {
            return array($recnum, (int) $data[1]);
        }

        // Store for next compare
        $idxpart = (int) $data[0];
        $recnum  = (int) $data[1];
    }

    // Return record number found
    return array($recnum, -1);
}

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-11-06 20:20 UTC] bjori@php.net
Does it matter?
$dbidx will get closed during RSHUTDOWN anyway...?
 [2009-11-06 20:39 UTC] ignas2526 at gmail dot com
If its so then why in i2c_search_in_db function datafile is being closed?
 [2009-11-14 01:00 UTC] php-webmaster at lists dot php dot net
No feedback was provided for this bug for over a week, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Jul 16 09:01:33 2025 UTC