From 8b526b1d573ca89cb2472df48b669533b8346b0a Mon Sep 17 00:00:00 2001
From: Julian Andres Klode <julian.klode@canonical.com>
Date: Tue, 4 Sep 2018 18:13:23 +0200
Subject: [PATCH] aptcc: Fix invalid version dereference in
 AptInf::providesCodec()

ver.Arch() was accessed even if ver.End() was true and ver thus
pointed to invalid memory, causing undefined behavior and likely
crashes, such as the Ubuntu bug below.

Bug-Ubuntu: https://bugs.launchpad.net/bugs/1790671
---
 backends/aptcc/apt-intf.cpp | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

--- a/backends/aptcc/apt-intf.cpp
+++ b/backends/aptcc/apt-intf.cpp
@@ -525,13 +525,14 @@ void AptIntf::providesCodec(PkgList &out
         // TODO search in updates packages
         // Ignore virtual packages
         pkgCache::VerIterator ver = m_cache->findVer(pkg);
-        arch = string(ver.Arch());
         if (ver.end() == true) {
             ver = m_cache->findCandidateVer(pkg);
-            if (ver.end() == true) {
-                continue;
-            }
         }
+        if (ver.end() == true) {
+            continue;
+        }
+
+        arch = string(ver.Arch());
 
         pkgCache::VerFileIterator vf = ver.FileList();
         pkgRecords::Parser &rec = m_cache->GetPkgRecords()->Lookup(vf);
