Subject: Re: bug#41563: Possible bug with 'sort -Vr' version sorting

From: Kamil Dudka <[email-masked]>
Date: Thu, 28 May 2020 13:01:05 +0200

On Thursday, May 28, 2020 11:02:43 AM CEST Erik Auerswald wrote:
> On Thu, May 28, 2020 at 08:48:16AM +0200, Kamil Dudka wrote:
> > It is the underscore in the .x86_64 suffix what breaks the version compare
> > algorithm.  If you replace the underscore by an alphabetic character, it
> > sorts as you expect:
> > 
> > # ls -t /boot/vmlinuz-* | sed "s/\/boot\/vmlinuz-//g" | grep -v rescue | \
> > 
> >     sed 's/x86_64/x86X64/' | sort -Vr | sed 's/x86X64/x86_64/'
> > 
> > 3.10.0-1127.8.2.el7.x86_64
> > 3.10.0-1127.el7.x86_64
> > 3.10.0-1062.18.1.el7.x86_64
> 
> That is interesting.  The underscore can be replaced by a digit or even
> removed as well.  Replacing it with a dot (.)  does not help.

If there is no underscore, the .el7.x86X64 suffix is recognized as file
extension.  See the corresponding documentation:

https://www.gnu.org/software/coreutils/manual/html_node/Special-handling-of-file-extensions.html

> This differs from Debian's "dpkg --compare-versions", where the results
> of the comparison do not change by replacing the underscore with a
> digit or character, or by removing it (the underscore is identified as
> problematic, though):

The problem is that `dpkg --compare-versions` expects version numbers only.
It does not work well if you feed it with file names including extensions:

$ dpkg --compare-versions 3.10.0-1127.8.2 '>>' 3.10.0-1127 && echo '>>' || echo '<='
$ dpkg --compare-versions 3.10.0-1127.8.2.bz2 '>>' 3.10.0-1127.bz2 && echo '>>' || echo '<='
<=

>     $ dpkg --compare-versions 3.10.0-1127.8.2.el7.x86_64 lt
> 3.10.0-1127.el7.x86_64 && echo less dpkg: warning: version
> '3.10.0-1127.8.2.el7.x86_64' has bad syntax: invalid character in revision
> number dpkg: warning: version '3.10.0-1127.el7.x86_64' has bad syntax:
> invalid character in revision number less
>     $ dpkg --compare-versions 3.10.0-1127.8.2.el7.x86.64 lt
> 3.10.0-1127.el7.x86.64 && echo less less
>     $ dpkg --compare-versions 3.10.0-1127.8.2.el7.x86X64 lt
> 3.10.0-1127.el7.x86X64 && echo less less
>     $ dpkg --compare-versions 3.10.0-1127.8.2.el7.x86164 lt
> 3.10.0-1127.el7.x86164 && echo less less
>     $ dpkg --compare-versions 3.10.0-1127.8.2.el7.x8664 lt
> 3.10.0-1127.el7.x8664 && echo less less
> 
> The way I read the GNU Coreutils documentation, removing the underscore
> should not affect the version sort comparison result.

Not really.  See the link above to the documentation that covers this part.

Kamil

> Thanks,
> Erik