summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTakuto Komazaki <takuto.komazaki@gree.net>2017-11-02 14:30:36 +0900
committerTakuto Komazaki <takuto.komazaki@gree.net>2017-11-02 15:01:41 +0900
commit2b60a984cf817b5a554a749e1cdfa16f4952eedf (patch)
tree936a375d23b69442c70f01ed9403762eb7c229ed
parent7f0b5150c32994b4ad593505172c5834a984b087 (diff)
downloadchef-2b60a984cf817b5a554a749e1cdfa16f4952eedf.tar.gz
Fix the invalid version comparison of apt packages.pr/6554
The strings of dpkg versions are more complex than the Gem::Version and not compatible with it. Using Gem::Version to compare versions of dpkg packages will cause mistakes or raise the exeption of 'Malformed version number..'. please see a simple example: https://gist.github.com/komazarari/c6b33b29a0e31e9c62bbc2f0b2091647 Prefer to use the dpkg cli. https://debian-handbook.info/browse/en-US/stable/sect.manipulating-packages-with-dpkg.html Signed-off-by: Takuto Komazaki <komazarari@gmail.com>
-rw-r--r--lib/chef/provider/package/apt.rb13
1 files changed, 9 insertions, 4 deletions
diff --git a/lib/chef/provider/package/apt.rb b/lib/chef/provider/package/apt.rb
index da86016621..605bbc70eb 100644
--- a/lib/chef/provider/package/apt.rb
+++ b/lib/chef/provider/package/apt.rb
@@ -128,10 +128,15 @@ class Chef
private
def version_compare(v1, v2)
- gem_v1 = v1.gsub(/[_+]/, "+" => "-", "_" => "-") unless v1.nil?
- gem_v2 = v2.gsub(/[_+]/, "+" => "-", "_" => "-") unless v2.nil?
-
- Gem::Version.new(gem_v1) <=> Gem::Version.new(gem_v2)
+ dpkg_v1 = v1 || '0'
+ dpkg_v2 = v2 || '0'
+ if shell_out_compact_timeout("dpkg", "--compare-versions", dpkg_v1, "gt", dpkg_v2).status.success?
+ 1
+ elsif shell_out_compact_timeout("dpkg", "--compare-versions", dpkg_v1, "eq", dpkg_v2).status.success?
+ 0
+ else
+ -1
+ end
end
# Runs command via shell_out with magic environment to disable