diff options
author | Piotr Kosko/Tizen API (PLT) /SRPOL/Engineer/Samsung Electronics <p.kosko@samsung.com> | 2021-05-19 08:23:11 +0200 |
---|---|---|
committer | Piotr Kosko/Tizen API (PLT) /SRPOL/Engineer/Samsung Electronics <p.kosko@samsung.com> | 2021-05-19 13:03:56 +0200 |
commit | 49ef34cee5e8cf7fb65cef0c0c2cf72f5af8442c (patch) | |
tree | 0230644d6e1d74b04f0ae7314daaab186106f48f | |
parent | c9edc16da7f5ec9b4a0d1159968a5f5792301710 (diff) | |
download | webapi-plugins-49ef34cee5e8cf7fb65cef0c0c2cf72f5af8442c.tar.gz webapi-plugins-49ef34cee5e8cf7fb65cef0c0c2cf72f5af8442c.tar.bz2 webapi-plugins-49ef34cee5e8cf7fb65cef0c0c2cf72f5af8442c.zip |
[nfc] Fixing memory management for messages and records
Should fix Coverity issues:
* 1227523
* 1227594
* 1228259
* 1228588
* 1229210
* 1229523
* 1229587
[Verification] Code compiles without errors.
Change-Id: I07813a855ce2d335fe783956a7af6f340a314602
-rw-r--r-- | src/nfc/nfc_adapter.cc | 8 | ||||
-rw-r--r-- | src/nfc/nfc_message_utils.cc | 117 |
2 files changed, 49 insertions, 76 deletions
diff --git a/src/nfc/nfc_adapter.cc b/src/nfc/nfc_adapter.cc index 65c87c41..05e06693 100644 --- a/src/nfc/nfc_adapter.cc +++ b/src/nfc/nfc_adapter.cc @@ -26,6 +26,7 @@ #include "common/extension.h" #include "common/logger.h" #include "common/platform_exception.h" +#include "common/scope_exit.h" #include "common/tools.h" #include "nfc/aid_data.h" #include "nfc/defs.h" @@ -1234,15 +1235,15 @@ PlatformResult NFCAdapter::TagTransceive(int tag_id, const picojson::value& args PlatformResult NFCAdapter::GetCachedMessage(picojson::object& out) { ScopeLogger(); nfc_ndef_message_h message_handle = NULL; + SCOPE_EXIT { NFCMessageUtils::RemoveMessageHandle(message_handle); }; int result = nfc_manager_get_cached_message(&message_handle); if (NFC_ERROR_INVALID_NDEF_MESSAGE == result || NFC_ERROR_NO_NDEF_MESSAGE == result) { LoggerE("Error: %d", result); - NFCMessageUtils::RemoveMessageHandle(message_handle); + return PlatformResult(ErrorCode::NO_ERROR); } if (NFC_ERROR_NONE != result) { LoggerE("Failed to get cached message: %d", result); - NFCMessageUtils::RemoveMessageHandle(message_handle); return NFCUtil::CodeToResult(result, "Failed to get cached message"); } unsigned char* raw_data = NULL; @@ -1250,17 +1251,14 @@ PlatformResult NFCAdapter::GetCachedMessage(picojson::object& out) { if (NFC_ERROR_NONE != nfc_ndef_message_get_rawdata(message_handle, &raw_data, &size)) { LoggerE("Unknown error while getting message."); free(raw_data); - NFCMessageUtils::RemoveMessageHandle(message_handle); return PlatformResult(ErrorCode::NO_ERROR); } PlatformResult ret = NFCMessageUtils::ReportNdefMessageFromData(raw_data, size, out); free(raw_data); if (ret.IsError()) { LoggerE("Error: %s", ret.message().c_str()); - NFCMessageUtils::RemoveMessageHandle(message_handle); return ret; } - NFCMessageUtils::RemoveMessageHandle(message_handle); return PlatformResult(ErrorCode::NO_ERROR); } diff --git a/src/nfc/nfc_message_utils.cc b/src/nfc/nfc_message_utils.cc index f231cf80..54235d05 100644 --- a/src/nfc/nfc_message_utils.cc +++ b/src/nfc/nfc_message_utils.cc @@ -21,6 +21,7 @@ #include "common/converter.h" #include "common/logger.h" #include "common/platform_exception.h" +#include "common/scope_exit.h" #include "nfc/nfc_util.h" using namespace common; @@ -80,14 +81,8 @@ static PlatformResult getTnfFromHandle(nfc_ndef_record_h handle, nfc_ndef_messag nfc_record_tnf_e record_tnf; int result = nfc_ndef_record_get_tnf(handle, &record_tnf); if (NFC_ERROR_NONE != result) { - LoggerE("Can't get record's tnf: %d, %s", result, NFCUtil::getNFCErrorMessage(result).c_str()); - // once record handle must be released - from inherited classes constructors - // once record handle cannot be released - from base class constructor - if (NULL == message_handle) { - removeRecordHandle(handle); - } else { - NFCMessageUtils::RemoveMessageHandle(message_handle); - } + LoggerE("Can't get record's tnf: %d, %s", result, + NFCUtil::getNFCErrorMessage(result).c_str()); return NFCUtil::CodeToResult(result, "Can't get record's tnf"); } @@ -104,14 +99,8 @@ static PlatformResult getTypeNameFromHandle(nfc_ndef_record_h handle, result = nfc_ndef_record_get_type(handle, &type_name, &type_size); if (NFC_ERROR_NONE != result) { - LoggerE("Can't get record's type: %d, %s", result, NFCUtil::getNFCErrorMessage(result).c_str()); - // once record handle must be released - from inherited classes constructors - // once record handle cannot be released - from base class constructor - if (NULL == message_handle) { - removeRecordHandle(handle); - } else { - NFCMessageUtils::RemoveMessageHandle(message_handle); - } + LoggerE("Can't get record's type: %d, %s", result, + NFCUtil::getNFCErrorMessage(result).c_str()); return NFCUtil::CodeToResult(result, "Can't get record's type"); } *type = NFCUtil::ToVector(type_name, type_size); @@ -126,14 +115,8 @@ static PlatformResult getIdFromHandle(nfc_ndef_record_h handle, nfc_ndef_message result = nfc_ndef_record_get_id(handle, &tmp_id, &id_size); if (NFC_ERROR_NONE != result) { - LoggerE("Can't get record's id: %d, %s", result, NFCUtil::getNFCErrorMessage(result).c_str()); - // once record handle must be released - from inherited classes constructors - // once record handle cannot be released - from base class constructor - if (NULL == message_handle) { - removeRecordHandle(handle); - } else { - NFCMessageUtils::RemoveMessageHandle(message_handle); - } + LoggerE("Can't get record's id: %d, %s", result, + NFCUtil::getNFCErrorMessage(result).c_str()); return NFCUtil::CodeToResult(result, "Can't get record's id"); } @@ -153,13 +136,6 @@ static PlatformResult getPayloadFromHandle(nfc_ndef_record_h handle, if (NFC_ERROR_NONE != result) { LoggerE("Can't get record's payload: %d, %s", result, NFCUtil::getNFCErrorMessage(result).c_str()); - // once record handle must be released - from inherited classes constructors - // once record handle cannot be released - from base class constructor - if (NULL == message_handle) { - removeRecordHandle(handle); - } else { - NFCMessageUtils::RemoveMessageHandle(message_handle); - } return NFCUtil::CodeToResult(result, "Can't get record's payload"); } @@ -195,19 +171,18 @@ PlatformResult NFCMessageUtils::ToNdefRecords(const nfc_ndef_message_h message, if (NFC_ERROR_NONE != result) { LoggerE("Can't get record count: %d, %s", result, NFCUtil::getNFCErrorMessage(result).c_str()); - RemoveMessageHandle(message); return NFCUtil::CodeToResult(result, "Can't get record count"); } for (int i = 0; i < count; ++i) { array.push_back(picojson::value(picojson::object())); picojson::object& record_obj = array.back().get<picojson::object>(); - nfc_ndef_record_h record_handle = NULL; + nfc_ndef_record_h record_handle = nullptr; + // record_handle is managed by framework, does not need to release it int result = nfc_ndef_message_get_record(message, i, &record_handle); if (NFC_ERROR_NONE != result) { LoggerE("Can't get Ndef Record: %d, %s", result, NFCUtil::getNFCErrorMessage(result).c_str()); - RemoveMessageHandle(message); return NFCUtil::CodeToResult(result, "Can't get Ndef Record"); } short tnf; @@ -268,6 +243,7 @@ PlatformResult NFCMessageUtils::ReportNdefMessageFromData(unsigned char* data, u picojson::object& out) { ScopeLogger(); nfc_ndef_message_h message = NULL; + SCOPE_EXIT { RemoveMessageHandle(message); }; int result = nfc_ndef_message_create_from_rawdata(&message, data, size); if (NFC_ERROR_NONE != result) { @@ -280,14 +256,11 @@ PlatformResult NFCMessageUtils::ReportNdefMessageFromData(unsigned char* data, u if (ret.IsError()) { LoggerE("Error: %s", ret.message().c_str()); - RemoveMessageHandle(message); return ret; } out.insert(std::make_pair("records", picojson::value(records_array))); - RemoveMessageHandle(message); - return PlatformResult(ErrorCode::NO_ERROR); } @@ -344,7 +317,6 @@ static PlatformResult NdefRecordGetHandle(picojson::value& record, &ndef_record_handle, tnf, type.get(), type_size > BYTE_ARRAY_MAX ? BYTE_ARRAY_MAX : type_size, id.get(), id_size > BYTE_ARRAY_MAX ? BYTE_ARRAY_MAX : id_size, payload.get(), payload_size); if (NFC_ERROR_NONE != result) { - removeRecordHandle(ndef_record_handle); LoggerE("Can't create Ndef Record: %d, %s", result, NFCUtil::getNFCErrorMessage(result).c_str()); return NFCUtil::CodeToResult(result, "Can't create Ndef Record"); @@ -361,35 +333,37 @@ PlatformResult NFCMessageUtils::NDEFMessageToStruct(const picojson::array& recor return PlatformResult(ErrorCode::NO_ERROR); } - nfc_ndef_message_h ndef_message; + nfc_ndef_message_h ndef_message = nullptr; int result = nfc_ndef_message_create(&ndef_message); - if (NFC_ERROR_NONE != result) { - RemoveMessageHandle(ndef_message); LoggerE("Can't create Ndef Message: %d, %s", result, NFCUtil::getNFCErrorMessage(result).c_str()); return NFCUtil::CodeToResult(result, "Can't create Ndef Message"); } + std::unique_ptr<std::remove_pointer<nfc_ndef_message_h>::type, + void (*)(nfc_ndef_message_h)> + ndef_message_ptr(ndef_message, &RemoveMessageHandle); for (picojson::value record : records_array) { - nfc_ndef_record_h record_handle; + nfc_ndef_record_h record_handle = nullptr; PlatformResult ret = NdefRecordGetHandle(record, &record_handle); if (ret.IsError()) { LoggerE("Error: %s", ret.message().c_str()); - RemoveMessageHandle(ndef_message); return ret; } result = nfc_ndef_message_append_record(ndef_message, record_handle); + // in case of success record_handle is handled by framework and does not need to be released if (NFC_ERROR_NONE != result) { LoggerE("record can't be inserted: %d, %s", result, NFCUtil::getNFCErrorMessage(result).c_str()); removeRecordHandle(record_handle); - RemoveMessageHandle(ndef_message); return NFCUtil::CodeToResult(result, "Invalid NDEF Message"); } } + // release ownership before returning a pointer + ndef_message_ptr.release(); *message = ndef_message; return PlatformResult(ErrorCode::NO_ERROR); } @@ -511,10 +485,12 @@ PlatformResult NFCMessageUtils::ReportNdefRecordFromMessage(nfc_ndef_message_h m picojson::object& out) { ScopeLogger(); nfc_ndef_record_h record_handle = NULL; - int result = nfc_ndef_message_get_record(message_handle, index, &record_handle); + // record_handle will be managed by framework, does not need to be released + int result = + nfc_ndef_message_get_record(message_handle, index, &record_handle); if (NFC_ERROR_NONE != result) { - LoggerE("Can't get NdefRecord: %d, %s", result, NFCUtil::getNFCErrorMessage(result).c_str()); - RemoveMessageHandle(message_handle); + LoggerE("Can't get NdefRecord: %d, %s", result, + NFCUtil::getNFCErrorMessage(result).c_str()); return NFCUtil::CodeToResult(result, "Can't get NdefRecord"); } @@ -533,7 +509,8 @@ PlatformResult NFCMessageUtils::ReportNDEFRecord(const picojson::value& args, for (ssize_t i = 0; i < size; i++) { data[i] = (int)raw_data[i].get<double>(); } - nfc_ndef_message_h message_handle = NULL; + nfc_ndef_message_h message_handle = nullptr; + SCOPE_EXIT { RemoveMessageHandle(message_handle); }; int result = nfc_ndef_message_create_from_rawdata(&message_handle, data.get(), size); if (NFC_ERROR_NONE != result) { @@ -545,15 +522,13 @@ PlatformResult NFCMessageUtils::ReportNDEFRecord(const picojson::value& args, int count; result = nfc_ndef_message_get_record_count(message_handle, &count); if (NFC_ERROR_NONE != result) { - LoggerE("Can't get record count: %d, %s", result, NFCUtil::getNFCErrorMessage(result).c_str()); - RemoveMessageHandle(message_handle); + LoggerE("Can't get record count: %d, %s", result, + NFCUtil::getNFCErrorMessage(result).c_str()); return NFCUtil::CodeToResult(result, "Can't get record count"); } PlatformResult ret = ReportNdefRecordFromMessage(message_handle, 0, out); - RemoveMessageHandle(message_handle); - if (ret.IsError()) { LoggerE("Error: %s", ret.message().c_str()); return ret; @@ -569,8 +544,8 @@ static PlatformResult getTextFromHandle(nfc_ndef_record_h handle, nfc_ndef_messa char* tmp_text = NULL; int result = nfc_ndef_record_get_text(handle, &tmp_text); if (NFC_ERROR_NONE != result) { - LoggerE("Can't get record's text: %d, %s", result, NFCUtil::getNFCErrorMessage(result).c_str()); - NFCMessageUtils::RemoveMessageHandle(message_handle); + LoggerE("Can't get record's text: %d, %s", result, + NFCUtil::getNFCErrorMessage(result).c_str()); return NFCUtil::CodeToResult(result, "Can't get record's text"); } @@ -590,7 +565,6 @@ static PlatformResult getLanguageCodeFromHandle(nfc_ndef_record_h handle, if (NFC_ERROR_NONE != result) { LoggerE("Can't get record's languageCode: %d, %s", result, NFCUtil::getNFCErrorMessage(result).c_str()); - NFCMessageUtils::RemoveMessageHandle(message_handle); return NFCUtil::CodeToResult(result, "Can't get record's languageCode"); } @@ -610,7 +584,6 @@ static PlatformResult getEncodingFromHandle(nfc_ndef_record_h handle, if (NFC_ERROR_NONE != result) { LoggerE("Can't get record's encoding: %d, %s", result, NFCUtil::getNFCErrorMessage(result).c_str()); - NFCMessageUtils::RemoveMessageHandle(message_handle); return NFCUtil::CodeToResult(result, "Can't get record's encoding"); } @@ -625,6 +598,7 @@ static PlatformResult ReportNDEFRecordTextFromText(const std::string& text, ScopeLogger(); nfc_encode_type_e encoding = convertToNfcEncodeUTF(encoding_str); nfc_ndef_record_h handle = NULL; + SCOPE_EXIT { removeRecordHandle(handle); }; int result = nfc_ndef_record_create_text(&handle, text.c_str(), language_code.c_str(), encoding); if (NFC_ERROR_NONE != result) { @@ -661,7 +635,6 @@ static PlatformResult ReportNDEFRecordTextFromText(const std::string& text, // constructing json ConstructRecordJson(_tnf, _type_name, _id, _payload, out); - removeRecordHandle(handle); return PlatformResult(ErrorCode::NO_ERROR); } @@ -670,11 +643,12 @@ PlatformResult NFCMessageUtils::ReportNdefRecordTextFromMessage(nfc_ndef_message picojson::object& out) { ScopeLogger(); nfc_ndef_record_h record_handle = NULL; - // This function just return the pointer of record. + // This function just return the pointer of record. do not release + // record_handle int result = nfc_ndef_message_get_record(message_handle, index, &record_handle); if (NFC_ERROR_NONE != result) { - LoggerE("Can't get Ndef Record: %d, %s", result, NFCUtil::getNFCErrorMessage(result).c_str()); - RemoveMessageHandle(message_handle); + LoggerE("Can't get Ndef Record: %d, %s", result, + NFCUtil::getNFCErrorMessage(result).c_str()); return NFCUtil::CodeToResult(result, "Can't get Ndef Record"); } @@ -729,8 +703,8 @@ static PlatformResult getURIFromHandle(nfc_ndef_record_h handle, nfc_ndef_messag char* uri = NULL; int result = nfc_ndef_record_get_uri(handle, &uri); if (NFC_ERROR_NONE != result || !uri) { - LoggerE("Can't get record's uri: %d, %s", result, NFCUtil::getNFCErrorMessage(result).c_str()); - NFCMessageUtils::RemoveMessageHandle(message_handle); + LoggerE("Can't get record's uri: %d, %s", result, + NFCUtil::getNFCErrorMessage(result).c_str()); return NFCUtil::CodeToResult(result, "Can't get record's uri"); } @@ -744,6 +718,7 @@ static PlatformResult getURIFromHandle(nfc_ndef_record_h handle, nfc_ndef_messag static PlatformResult ReportNDEFRecordURIFromURI(const std::string& uri, picojson::object& out) { ScopeLogger(); nfc_ndef_record_h handle = NULL; + SCOPE_EXIT { removeRecordHandle(handle); }; int result = nfc_ndef_record_create_uri(&handle, uri.c_str()); if (NFC_ERROR_NONE != result) { @@ -780,7 +755,6 @@ static PlatformResult ReportNDEFRecordURIFromURI(const std::string& uri, picojso // constructing json ConstructRecordJson(_tnf, _type_name, _id, _payload, out); - removeRecordHandle(handle); return PlatformResult(ErrorCode::NO_ERROR); } @@ -789,11 +763,12 @@ PlatformResult NFCMessageUtils::ReportNdefRecordURIFromMessage(nfc_ndef_message_ picojson::object& out) { ScopeLogger(); nfc_ndef_record_h record_handle = NULL; - // This function just return the pointer of record. + // This function just return the pointer of record. do not remove + // record_handle int result = nfc_ndef_message_get_record(message_handle, index, &record_handle); if (NFC_ERROR_NONE != result) { - LoggerE("Can't get Ndef Record: %d, %s", result, NFCUtil::getNFCErrorMessage(result).c_str()); - RemoveMessageHandle(message_handle); + LoggerE("Can't get Ndef Record: %d, %s", result, + NFCUtil::getNFCErrorMessage(result).c_str()); return NFCUtil::CodeToResult(result, "Can't get Ndef Record"); } @@ -829,7 +804,6 @@ static PlatformResult getMimeTypeFromHandle(nfc_ndef_record_h handle, if (NFC_ERROR_NONE != result) { LoggerE("Can't get record's mime_type: %d, %s", result, NFCUtil::getNFCErrorMessage(result).c_str()); - NFCMessageUtils::RemoveMessageHandle(message_handle); return NFCUtil::CodeToResult(result, "Can't get record's mime_type"); } @@ -845,11 +819,12 @@ PlatformResult NFCMessageUtils::ReportNdefRecordMediaFromMessage(nfc_ndef_messag picojson::object& out) { ScopeLogger(); nfc_ndef_record_h record_handle = NULL; - // This function just return the pointer of record. + // This function just return the pointer of record. do not remove + // record_handle int result = nfc_ndef_message_get_record(message_handle, index, &record_handle); if (NFC_ERROR_NONE != result) { - LoggerE("Can't get Ndef Record: %d, %s", result, NFCUtil::getNFCErrorMessage(result).c_str()); - RemoveMessageHandle(message_handle); + LoggerE("Can't get Ndef Record: %d, %s", result, + NFCUtil::getNFCErrorMessage(result).c_str()); return NFCUtil::CodeToResult(result, "Can't get Ndef Record"); } @@ -904,6 +879,7 @@ PlatformResult NFCMessageUtils::ReportNDEFRecordMedia(const picojson::value& arg } nfc_ndef_record_h handle = NULL; + SCOPE_EXIT { removeRecordHandle(handle); }; short _tnf = NFC_RECORD_TNF_UNKNOWN; UCharVector _type_name; @@ -942,7 +918,6 @@ PlatformResult NFCMessageUtils::ReportNDEFRecordMedia(const picojson::value& arg // constructing json ConstructRecordJson(_tnf, _type_name, _id, _payload, out); - removeRecordHandle(handle); return PlatformResult(ErrorCode::NO_ERROR); } |