php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #58055 "enchant dict" resource variable may loose type information
Submitted: 2008-02-23 03:23 UTC Modified: 2008-04-16 05:55 UTC
From: mfischer@php.net Assigned: pajoye (profile)
Status: Closed Package: enchant (PECL)
PHP Version: 5.2.5 OS: Linux Debian
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: mfischer@php.net
New email:
PHP Version: OS:

 

 [2008-02-23 03:23 UTC] mfischer@php.net
Description:
------------
Under certain circumstances the "enchant dict" resource variable may loose it's type information. In my testing this seems to be related to whether the "enchant broker" resource is in the same scope or not.

(I've tested with xdebug disable too, there was no change, just to be sure).

The example shows the problem:
<?php
class foo {
    private $enchant;
    private $dict;
    public function __construct() {
        $enchant = enchant_broker_init();
        $this->dict = enchant_broker_request_dict($enchant, 'en');
        var_dump($this->enchant, $this->dict);
    }
    public function getSuggestions($sWord) {
        var_dump($this->dict);
        return enchant_dict_suggest($this->dict, $sWord);
    }
}
$f = new foo;
$f->getSuggestions("soong");
?>

:!php problem_oop.php
NULL
resource(5) of type (enchant dict)
resource(5) of type (Unknown)

Warning: enchant_dict_suggest(): 5 is not a valid enchant dict resource in /data/users/markus/src/enchant/problem_oop.php on line 12

Call Stack:
    0.0002      58452   1. {main}() /data/users/markus/src/enchant/problem_oop.php:0
    0.0081      59444   2. foo->getSuggestions() /data/users/markus/src/enchant/problem_oop.php:16
    0.0081      59444   3. enchant_dict_suggest() /data/users/markus/src/enchant/problem_oop.php:12

When I change the broker resource to be a property of the object, the dict resource survives and it works.

<?php
class foo {
    private $enchant;
    private $dict;
    public function __construct() {
        $this->enchant = enchant_broker_init();
        $this->dict = enchant_broker_request_dict($this->enchant, 'en');
        var_dump($this->enchant, $this->dict);
    }
    public function getSuggestions($sWord) {
        var_dump($this->dict);
        return enchant_dict_suggest($this->dict, $sWord);
    }
}
$f = new foo;
$f->getSuggestions("soong");
?>

:!php problem_oop.php
resource(4) of type (enchant broker)
resource(5) of type (enchant dict)
resource(5) of type (enchant dict)


It is not related to OOP, this is just how I discovered the bug, see this example too:
<?php
function get_dict() {
    $rBroker = enchant_broker_init();
    return enchant_broker_request_dict($rBroker, 'en');
}
$rDict = get_dict();
enchant_dict_suggest($rDict, "soong");
?>

:!php problem.php

Warning: enchant_dict_suggest(): 5 is not a valid enchant dict resource in /data/users/markus/src/enchant/problem.php on line 7

Call Stack:
    0.0002      53708   1. {main}() /data/users/markus/src/enchant/problem.php:0
    0.0080      54084   2. enchant_dict_suggest() /data/users/markus/src/enchant/problem.php:7



Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-02-23 03:24 UTC] mfischer@php.net
Corrected PHP Version
 [2008-02-23 07:45 UTC] pierre dot php at gmail dot com
Good catch and I wonder where is the problem. Given that the following code:

function get_broker() {
    $t = enchant_broker_init();
    var_dump($t);
    return $t;
}

$rbroker = get_broker();
var_dump($rbroker);

function get_dict($broker) {
    $t = enchant_broker_request_dict($broker, 'en');
    var_dump($t);
    return $t;
}
$rDict = get_dict($rbroker);
var_dump($rDict);

simply works, it looks like something really not obvious.

I will run some more tests and trace the execution.

Ilia? Maybe you have an idea?
 [2008-02-23 07:53 UTC] pierre dot php at gmail dot com
ok, second thought and supposition #1:

The broker resource is used in the dictionary resource (to know to which broker it belongs). The resource itself is used only in the function scope, that means it is destroy once we left this function.

We will need to change slightly the argument fetching and use zval instead of resources. Doing so will let us use add_ref and dec_ref accordingly.

That's only a supposition as I can't compile anything right now, will try the later this weekend or feel free to give it a try too.
 [2008-02-23 10:27 UTC] pierre dot php at gmail dot com
Please try using this patch:

http://blog.thepimp.net/patches/enchant_res_ref.txt

It should fix the problem.

I have to write tests (with many dicts and references) to valid it before a commit + release :)
 [2008-02-23 21:58 UTC] mfischer@php.net
Confirmed, it works, thanks!
 [2008-04-16 05:55 UTC] pierre dot php at gmail dot com
Thank you for your bug report. This issue has been fixed
in the latest released version of the package, which you can download at
http://pecl.php.net/get/enchant


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Apr 24 16:01:31 2024 UTC