From 3c776172b830f3f68509175139a63dd2c9b1cbce Mon Sep 17 00:00:00 2001 From: Changgyu Choi Date: Mon, 17 Oct 2022 13:25:14 +0900 Subject: [PATCH] Fix static analysis issues Change-Id: Ica815a67211bee0d76bb35f64e15b1cb84124d2c Signed-off-by: Changgyu Choi --- parcel/parcel.cc | 2 +- parcel/stub.cc | 2 +- src/bundle-internal.cc | 21 +++++++++++++-------- src/bundle-internal.h | 4 ++-- 4 files changed, 17 insertions(+), 12 deletions(-) diff --git a/parcel/parcel.cc b/parcel/parcel.cc index 439e212..9322d49 100644 --- a/parcel/parcel.cc +++ b/parcel/parcel.cc @@ -130,7 +130,7 @@ int Parcel::Impl::ReadSize(uint32_t* size) { template int Parcel::Impl::Read(T* d) { - uint32_t size = sizeof(T); + uint32_t size = static_cast(sizeof(T)); if (reader_ + size > data_size_) return TIZEN_ERROR_ILLEGAL_BYTE_SEQ; diff --git a/parcel/stub.cc b/parcel/stub.cc index f6b6299..245e3ac 100644 --- a/parcel/stub.cc +++ b/parcel/stub.cc @@ -390,7 +390,7 @@ extern "C" EXPORT int parcel_get_raw(parcel_h parcel, void** raw, auto* h = static_cast(parcel); *raw = reinterpret_cast(h->GetData()); - *size = h->GetDataSize(); + *size = static_cast(h->GetDataSize() & UINT32_MAX); return PARCEL_ERROR_NONE; } diff --git a/src/bundle-internal.cc b/src/bundle-internal.cc index 8088c60..44942ac 100644 --- a/src/bundle-internal.cc +++ b/src/bundle-internal.cc @@ -19,7 +19,9 @@ #include #include +#include #include +#include #include "include/bundle.h" @@ -31,11 +33,14 @@ namespace internal { static const int CHECKSUM_LENGTH = 32; Bundle::Bundle(unsigned char* raw, int size, bool base64) { + if (size < 0) + THROW(BUNDLE_ERROR_INVALID_PARAMETER); + int ret; if (base64) - ret = Decode(raw, size); + ret = Decode(raw, static_cast(size)); else - ret = DecodeRaw(raw, size); + ret = DecodeRaw(raw, static_cast(size)); if (ret != BUNDLE_ERROR_NONE) THROW(ret); } @@ -161,7 +166,7 @@ unsigned char* Bundle::Encode() { return reinterpret_cast(encoded_data); } -int Bundle::Decode(unsigned char* raw, int size) { +int Bundle::Decode(unsigned char* raw, size_t size) { unsigned char* d_str = new (std::nothrow) unsigned char[(size / 4) * 3 + 3]; if (d_str == nullptr) return BUNDLE_ERROR_OUT_OF_MEMORY; @@ -169,8 +174,8 @@ int Bundle::Decode(unsigned char* raw, int size) { std::unique_ptr d_ptr(d_str); gint state = 0; guint save = 0; - unsigned int d_len_raw = g_base64_decode_step(reinterpret_cast(raw), - size, d_str, &state, &save); + size_t d_len_raw = static_cast(g_base64_decode_step( + reinterpret_cast(raw), size, d_str, &state, &save)); if (d_len_raw < CHECKSUM_LENGTH) return BUNDLE_ERROR_OUT_OF_MEMORY; @@ -206,14 +211,14 @@ unsigned char* Bundle::EncodeRaw(int* size) { return raw; } -int Bundle::DecodeRaw(unsigned char* raw, int size) { +int Bundle::DecodeRaw(unsigned char* raw, size_t size) { char* extract_checksum = new (std::nothrow) char[CHECKSUM_LENGTH + 1]; if (extract_checksum == nullptr) return BUNDLE_ERROR_OUT_OF_MEMORY; std::unique_ptr extract_ptr(extract_checksum); unsigned char* d_str = raw; - unsigned int d_len_raw = size; + size_t d_len_raw = size; strncpy(extract_checksum, reinterpret_cast(d_str), CHECKSUM_LENGTH); extract_checksum[CHECKSUM_LENGTH] = '\0'; @@ -229,7 +234,7 @@ int Bundle::DecodeRaw(unsigned char* raw, int size) { return BUNDLE_ERROR_INVALID_PARAMETER; unsigned char* d_r = d_str + CHECKSUM_LENGTH; - unsigned int d_len = d_len_raw - CHECKSUM_LENGTH; + size_t d_len = d_len_raw - CHECKSUM_LENGTH; unsigned int reader = 0; std::vector bytes(d_r, d_r + d_len); diff --git a/src/bundle-internal.h b/src/bundle-internal.h index 19813dd..f8d0fb1 100644 --- a/src/bundle-internal.h +++ b/src/bundle-internal.h @@ -71,12 +71,12 @@ class Bundle { unsigned char* Encode(); unsigned char* EncodeRaw(int* size); - int DecodeRaw(unsigned char* raw, int size); + int DecodeRaw(unsigned char* raw, size_t size); const std::unordered_map>& GetMap() const; std::vector Export(); private: - int Decode(unsigned char* raw, int size); + int Decode(unsigned char* raw, size_t size); int Import(int argc, char** argv); private: -- 2.7.4