From e352ee23e593641fbb22f181b7a48535230768e6 Mon Sep 17 00:00:00 2001 From: howar6hill Date: Thu, 20 Feb 2020 11:09:20 +0800 Subject: [PATCH] Userland: Fix nullptr dereference if we fail to open the PCIDB In the code below, db could be null, and would cause UB. Instead of crashing, let's simply skip symbolicating names. Fixes #1247 --- Userland/lspci.cpp | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/Userland/lspci.cpp b/Userland/lspci.cpp index ab0b527568..8b80be2a54 100644 --- a/Userland/lspci.cpp +++ b/Userland/lspci.cpp @@ -24,9 +24,9 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include #include #include +#include #include #include #include @@ -81,20 +81,22 @@ int main(int argc, char** argv) auto revision_id = dev.get("revision_id").to_u32(); auto class_id = dev.get("class").to_u32(); - String vendor_name = String::format("%02x", vendor_id); - auto vendor = db->get_vendor(vendor_id); - if (vendor != "") - vendor_name = vendor; + String vendor_name; + String device_name; + String class_name; - String device_name = String::format("%02x", device_id); - auto device = db->get_device(vendor_id, device_id); - if (device != "") - device_name = device; + if (db) { + vendor_name = db->get_vendor(vendor_id); + device_name = db->get_device(vendor_id, device_id); + class_name = db->get_class(class_id); + } - String class_name = String::format("%04x", class_id); - auto class_ptr = db->get_class(class_id); - if (class_ptr != "") - class_name = class_ptr; + if (vendor_name.is_empty()) + vendor_name = String::format("%02x", vendor_id); + if (device_name.is_empty()) + device_name = String::format("%02x", device_id); + if (class_name.is_empty()) + class_name = String::format("%04x", class_id); printf("%04x:%02x:%02x.%d %s: %s %s (rev %02x)\n", seg, bus, slot, function,