#752699 coreutils: sort --version-sort doesnt do comparisons correctly

Package:
coreutils
Source:
coreutils
Description:
GNU core utilities
Submitter:
Johannes Schauer
Date:
2015-02-04 09:52:13 UTC
Severity:
normal
#752699#5
Date:
2014-06-25 18:50:45 UTC
From:
To:
Hi,

if I read this correctly:

http://sources.debian.net/src/coreutils/8.21-1.2/src/sort.c#L2553

then sort uses this function (filevercmp) to sort version numbers with
--version-sort:

http://sources.debian.net/src/coreutils/8.21-1.2/lib/filevercmp.c?hl=73#L125

a comment in that file claims:

   This implements the algorithm for comparison of version strings
   specified by Debian and now widely adopted.  The detailed
   specification can be found in the Debian Policy Manual in the
   section on the 'Version' control field.  This version of the code
   implements that from s5.6.12 of Debian Policy v3.8.0.1
http://www.debian.org/doc/debian-policy/ch-controlfields.html#s-f-Version

consider the following shell function as presented in [1]

version_lt () {
    newest=$( ( echo "$1"; echo "$2" ) | sort -V | tail -n1)
    [ "$1" != "$newest" ]
}

[1] http://www.df7cb.de/blog/2014/Comparing_Version_Numbers_in_Shell.html

Here an example for when `sort -V` does the wrong thing:

    $ dpkg --compare-versions 0.04-1-5 "<" 0.4-3 && echo yes || echo no
    no
    $ version_lt 0.04-1-5 0.4-3 && echo yes || echo no
    yes

here another:

    $ dpkg --compare-versions 00004-1 "<" 1:3.8.3-7 && echo yes || echo no
    yes
    $ version_lt 00004-1 1:3.8.3-7 && echo yes || echo no
    no

And here some more version combinations for which this is done wrongly:

    0.04-1-5             0.4-3
    0.1+20080921-2       0.1-1+b8
    0.1+dfsg-4           000.001-4
    0.1.0+20071012-1.2   0.1.0-1.1
    0.3.0+20091229-1     0.3.0-7.2
    0.6.0+git20130305-5  0.6.0-5+b1
    0:2009.10.04-1       1.0pre11-1
    0:2009.10.04-1       1.240-1
    0:2009.10.04-1       1.7.2.4-4.1
    0:2009.10.04-1       2.3-12
    0:2009.10.04-1       8.0.184.15484+dfsg-2
    1.0+dfsg-1           1.00-6

If you want to generate more test cases, consider the code for testing sort at

https://github.com/josch/debversioncomp

thanks!

cheers, josch