From 4fa5b413ee2ece583793cfb321459879b9f8ac44 Mon Sep 17 00:00:00 2001 From: Adam Honse Date: Thu, 28 Jan 2021 18:03:52 -0600 Subject: [PATCH] Check the validity of the PCI device string in AMD ADL I2C driver to prevent crash if the string is malformed --- i2c_smbus/i2c_smbus_amdadl.cpp | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/i2c_smbus/i2c_smbus_amdadl.cpp b/i2c_smbus/i2c_smbus_amdadl.cpp index 10e63057..c8acc04b 100644 --- a/i2c_smbus/i2c_smbus_amdadl.cpp +++ b/i2c_smbus/i2c_smbus_amdadl.cpp @@ -84,21 +84,24 @@ i2c_smbus_amdadl::i2c_smbus_amdadl(ADL_CONTEXT_HANDLE context) std::size_t dev_loc = pnp_str.find("DEV_"); std::size_t sub_loc = pnp_str.find("SUBSYS_"); - std::string ven_str = pnp_str.substr(ven_loc + 4, 4); - std::string dev_str = pnp_str.substr(dev_loc + 4, 4); - std::string sbv_str = pnp_str.substr(sub_loc + 11, 4); - std::string sbd_str = pnp_str.substr(sub_loc + 7, 4); + if((ven_loc != std::string::npos) && (dev_loc != std::string::npos) && (sub_loc != std::string::npos)) + { + std::string ven_str = pnp_str.substr(ven_loc + 4, 4); + std::string dev_str = pnp_str.substr(dev_loc + 4, 4); + std::string sbv_str = pnp_str.substr(sub_loc + 11, 4); + std::string sbd_str = pnp_str.substr(sub_loc + 7, 4); - int ven_id = (int)std::stoul(ven_str, nullptr, 16); - int dev_id = (int)std::stoul(dev_str, nullptr, 16); - int sbv_id = (int)std::stoul(sbv_str, nullptr, 16); - int sbd_id = (int)std::stoul(sbd_str, nullptr, 16); + int ven_id = (int)std::stoul(ven_str, nullptr, 16); + int dev_id = (int)std::stoul(dev_str, nullptr, 16); + int sbv_id = (int)std::stoul(sbv_str, nullptr, 16); + int sbd_id = (int)std::stoul(sbd_str, nullptr, 16); - this->pci_vendor = ven_id; - this->pci_device = dev_id; - this->pci_subsystem_vendor = sbv_id; - this->pci_subsystem_device = sbd_id; - strcpy(this->device_name, "AMD ADL"); + this->pci_vendor = ven_id; + this->pci_device = dev_id; + this->pci_subsystem_vendor = sbv_id; + this->pci_subsystem_device = sbd_id; + strcpy(this->device_name, "AMD ADL"); + } } }