diff --git a/interfaces/innerkits/native/include/usb_interface.h b/interfaces/innerkits/native/include/usb_interface.h index 6f898727f5cfc592332c3ac8d138c519f24ff8e2..dedc6c440f6c189aa38f5e01585ae69cfc38fd18 100644 --- a/interfaces/innerkits/native/include/usb_interface.h +++ b/interfaces/innerkits/native/include/usb_interface.h @@ -223,6 +223,8 @@ public: cJSON* endpoints = cJSON_CreateArray(); if (!endpoints) { USB_HILOGE(MODULE_USB_SERVICE, "Create endpoints error"); + cJSON_Delete(interface); + return ""; } for (size_t i = 0; i < endpoints_.size(); ++i) { const USBEndpoint &ep = endpoints_[i]; diff --git a/services/native/src/usb_descriptor_parser.cpp b/services/native/src/usb_descriptor_parser.cpp index cb8e3c8312cca55b6297140485b32f4619d0540f..12c8d1a3941d5ef3a1f4de1e4eb16f03b5760f68 100644 --- a/services/native/src/usb_descriptor_parser.cpp +++ b/services/native/src/usb_descriptor_parser.cpp @@ -82,15 +82,11 @@ int32_t UsbDescriptorParser::ParseConfigDescriptor( USB_HILOGD(MODULE_USB_SERVICE, "parse begin length=%{public}u, cursor=%{public}u", length, cursor); uint32_t configDescriptorSize = sizeof(UsbdConfigDescriptor); - if (length < configDescriptorSize) { - USB_HILOGE(MODULE_USB_SERVICE, "buffer size error"); - return UEC_SERVICE_INVALID_VALUE; - } - UsbdConfigDescriptor configDescriptor = *(reinterpret_cast(buffer)); cursor += configDescriptorSize; - if (configDescriptor.bLength != configDescriptorSize) { - USB_HILOGE(MODULE_USB_SERVICE, "UsbdDeviceDescriptor size error"); + if (length < configDescriptorSize || configDescriptor.bLength != configDescriptorSize) { + USB_HILOGE(MODULE_USB_SERVICE, "size error length=%{public}u, configDescriptor.bLength=%{public}d", + length, configDescriptor.bLength); return UEC_SERVICE_INVALID_VALUE; } @@ -103,8 +99,12 @@ int32_t UsbDescriptorParser::ParseConfigDescriptor( for (int32_t i = 0; (i < configDescriptor.bNumInterfaces) && (cursor < length); ++i) { uint32_t interfaceCursor = 0; UsbInterface interface; - ParseInterfaceDescriptor( + int32_t ret = ParseInterfaceDescriptor( buffer + cursor + interfaceCursor, length - cursor - interfaceCursor, interfaceCursor, interface); + if (ret != UEC_OK) { + USB_HILOGE(MODULE_USB_SERVICE, "ParseInterfaceDescriptor failed, ret=%{public}d", ret); + return UEC_SERVICE_INVALID_VALUE; + } bool isRepeat = false; auto iter = interfaces.begin(); while (iter != interfaces.end()) { diff --git a/services/native/src/usb_device_manager.cpp b/services/native/src/usb_device_manager.cpp index a6c96c1ba69eb1cdd03cf4ae61ca01bf31811ad9..b5f7ceecbbf631dd8df408cc330840a21c14072e 100644 --- a/services/native/src/usb_device_manager.cpp +++ b/services/native/src/usb_device_manager.cpp @@ -269,6 +269,10 @@ void UsbDeviceManager::DumpSetFunc(int32_t fd, const std::string &args) { int32_t currentFunction; int32_t ret; + if (usbd_ == nullptr) { + USB_HILOGE(MODULE_USB_SERVICE, "UsbDeviceManager::DumpSetFunc usbd_ is nullptr"); + return; + } if (args.compare("Q") == 0) { ret = usbd_->GetCurrentFunctions(currentFunction); if (ret != UEC_OK) { diff --git a/services/native/src/usb_host_manager.cpp b/services/native/src/usb_host_manager.cpp index 3fe7e147df7c9514f7e9777e87e5eac32c9dc6ca..78f81476bd194a6a0c7293133f891b9f354dc72a 100644 --- a/services/native/src/usb_host_manager.cpp +++ b/services/native/src/usb_host_manager.cpp @@ -37,7 +37,13 @@ UsbHostManager::UsbHostManager(SystemAbility *systemAbility) systemAbility_ = systemAbility; } -UsbHostManager::~UsbHostManager() {} +UsbHostManager::~UsbHostManager() +{ + for (auto &pair : devices_) { + delete pair.second; + } + devices_.clear(); +} void UsbHostManager::GetDevices(MAP_STR_DEVICE &devices) { @@ -110,7 +116,9 @@ bool UsbHostManager::AddDevice(UsbDevice *dev) USB_HILOGF(MODULE_SERVICE, "device:%{public}s bus:%{public}hhu dev:%{public}hhu already exist", name.c_str(), busNum, devNum); UsbDevice *devOld = iter->second; - delete devOld; + if (devOld != nullptr && devOld != dev) { + delete devOld; + } devices_.erase(iter); } USB_HILOGI( diff --git a/services/native/src/usb_mass_storage_notification.cpp b/services/native/src/usb_mass_storage_notification.cpp index 318ad65a115ffeda3ad929bc8a46b49eccbc4c68..983f2bbd2b1564e7d6af8bfe058044cce1b63fd1 100644 --- a/services/native/src/usb_mass_storage_notification.cpp +++ b/services/native/src/usb_mass_storage_notification.cpp @@ -188,12 +188,12 @@ void UsbMassStorageNotification::PublishUsbNotification() normalContent->SetText(notificationMap[MASS_STORAGE_NOTIFICATION_TEXT_KEY]); std::shared_ptr content = std::make_shared(normalContent); - if (content == nullptr) { - USB_HILOGE(MODULE_USB_SERVICE, "notification content nullptr"); + auto want = std::make_shared(); + if (content == nullptr || want == nullptr) { + USB_HILOGE(MODULE_USB_SERVICE, "notification content is nullptr or want is nulllptr"); return; } GetFilemanagerBundleName(); - auto want = std::make_shared(); want->SetElementName(filemanagerBundleName, filemanagerAbilityName); std::vector> wants; wants.push_back(want); diff --git a/services/native/src/usb_port_manager.cpp b/services/native/src/usb_port_manager.cpp index 58a5ba6de331994f6f63a98c634c31a8108d4e8e..4d3204c35cd1821a6f7374b2ba57b7d0c08c736c 100644 --- a/services/native/src/usb_port_manager.cpp +++ b/services/native/src/usb_port_manager.cpp @@ -239,7 +239,10 @@ void UsbPortManager::DumpSetPortRoles(int32_t fd, const std::string &args) GetPortsInfo(fd); return; } - + if (usbd_ == nullptr) { + USB_HILOGE(MODULE_USB_SERVICE, "UsbPortManager::DumpSetPortRoles usbd_ is nullptr"); + return; + } int32_t mode = stoi(args); switch (mode) { case DEFAULT_ROLE_HOST: