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
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.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: anlutro at gmail dot com
New email:
PHP Version: OS:

 

 [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: Wed Apr 08 12:01:25 2020 UTC