Skip to content

Pointer return value of a fnc without 'return' #419

Open
@ghost

Description

SdkVersion: v3.1-dev-239-g1c3dd23f-dirty
Arduino IDE 1.8.5 with BT sources from esp32-snippets/cpp_utils/BLE*.*
Example:ESP32_BLE_Arduino/examples/BLE_client/

Bug in the methods: getService() and getCharacteristic() in BLEClient.cpp

calls in BLE_Client:

// Obtain a reference to the service we are after in the remote BLE server.
  pRemoteService = pClient->getService(serviceUUID);
  if (pRemoteService == nullptr) // never zero <<<<<<<<<<<<<<<<<<<<<<<<<
  {
    logmsg = "Failed to find our service UUID ";
  }

and:

// Obtain a reference to the characteristic in the service of the remote BLE server.
  pRemoteCharacteristic= pRemoteService->getCharacteristic(charUUID);
  if (pRemoteCharacteristic == nullptr) // never zero <<<<<<<<<<<<<<<<<<<<<<<<<
  {
    logmsg = "Failed to find our characteristic UUID ";
  }

Undepending if the requested Service/Characteristic was found or not, the getService and getCharacteristic returns not 'nullptr'.
In the found case it's the ptr to the Service or Characteristic (thats OK),
in the notFound case it's an uninitialized pointer to mem (not zero or nullptr - NOT OK). The calling fnc reports 'found' and requests the Characteristic from an not existing Service object. The ESP crashes

Fix:
add a return nullptr; to the end of both methods.

method:

BLERemoteService* BLEClient::getService(BLEUUID uuid) {
	ESP_LOGD(LOG_TAG, ">> getService: uuid: %s", uuid.toString().c_str());

	if (!m_haveServices) {
		getServices();
	}
	std::string uuidStr = uuid.toString();
	for (auto &myPair : m_servicesMap) {
		if (myPair.first == uuidStr) {
			ESP_LOGD(LOG_TAG, "<< getService: found the service with uuid: %s", uuid.toString().c_str());
			return myPair.second;
		}
	} // End of each of the services.
	ESP_LOGD(LOG_TAG, "<< getService: not found");
return nullptr; // ++ add this <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
} // getService

Same for getCharacteristic() !

PLEASE CHECK ALL YOUR CODE, THX

-Pit

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions