php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #69268 version_compare returns wrongly when "special" characters are given
Submitted: 2015-03-20 09:35 UTC Modified: -
From: anlutro at gmail dot com Assigned:
Status: Open Package: *General Issues
PHP Version: Irrelevant OS: Linux (Debian)
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2015-03-20 09:35 UTC] anlutro at gmail dot com
Description:
------------
When doing version_compare on strings that contain "special" characters like parenthesis or brackets, it returns entirely the wrong result.

While this isn't an issue for 99% of version strings, one of our applications has the option of either providing a regular version string OR a datetime string with the git commit hash at the end (mostly for the developers' sake). We "solved" this by telling all developers submitting packages to our system to remove paranthesis or brackets from the git hash suffix.

This behaviour is consistent across all PHP versions: http://3v4l.org/DaQQY

Test script:
---------------
<?php
$v = '1.2.3 (59eda4e)';
var_dump(version_compare($v, $v));
var_dump(version_compare($v, $v, '<'));

$v = '1.2.3 [59eda4e]';
var_dump(version_compare($v, $v));
var_dump(version_compare($v, $v, '<'));

$v = '1.2.3 59eda4e';
var_dump(version_compare($v, $v));
var_dump(version_compare($v, $v, '<'));

Expected result:
----------------
int(0)
bool(false)
int(0)
bool(false)
int(0)
bool(false)

Actual result:
--------------
int(-1)
bool(true)
int(-1)
bool(true)
int(0)
bool(false)

Patches

Add a Patch

Pull Requests

Add a Pull Request

 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Tue Apr 07 17:01:26 2020 UTC