Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

Zend/Mvc/Router/Http/Segment::encode caches wrongly #5516

Closed
rtreffler opened this issue Nov 21, 2013 · 0 comments · Fixed by #5546
Closed

Zend/Mvc/Router/Http/Segment::encode caches wrongly #5516

rtreffler opened this issue Nov 21, 2013 · 0 comments · Fixed by #5546

Comments

@rtreffler
Copy link
Contributor

Zend/Mvc/Router/Http/Segment::encode#L429

Whenever $value is of float type it's cast into integer when used as a key in static::$cacheEncode[$value]

Why it is important ? See case:

Building url on page,
$this->url('route_name1', array('param1' => 6.123, 'param2' => 7);
static::$cacheEncode content is array(6 => 6.123, 7 => 7)
ends with url /route-name1/6.123/7

building second url on page
$this->url('route_name2', array('param1' => 6, 'param2' => 'test');
static::$cacheEncode content is array(6 => 6.123, 7 => 7, 'test' => 'test')
ends with url /route-name2/6.123/test since cached encoded value is used.

this happens for PHP 5.3.10 and most likely for newer versions,
see PHP documentation

The key can either be an integer or a string. The value can be of any type.
...
Floats are also cast to integers, which means that the fractional part will be truncated. E.g. the key 8.7 will actually be stored under 8.

Fix proposal:

    protected function encode($value)
    {
        $key = (string) $value;
        if (!isset(static::$cacheEncode[$key])) {
            static::$cacheEncode[$key] = rawurlencode($value);
            static::$cacheEncode[$key] = strtr(static::$cacheEncode[$key], static::$urlencodeCorrectionMap);
        }
        return static::$cacheEncode[$key];
    }
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant