Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issues reported by SonarCloud (11) #490

Merged
merged 3 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 19 additions & 17 deletions api/oc_base64.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,13 @@ oc_base64_encode(const uint8_t *input, size_t input_len, uint8_t *output_buffer,
/* The Base64 alphabet. This table provides a mapping from 6-bit binary
* values to Base64 characters.
*/
uint8_t alphabet[65] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', '+', '/', '=' };
uint8_t val = 0;
size_t i;
int j = 0;
const uint8_t alphabet[65] = {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/', '=',
};

/* Calculate the length of the Base64 encoded output.
* Every sequence of 3 bytes (with padding, if necessary)
Expand All @@ -50,13 +47,17 @@ oc_base64_encode(const uint8_t *input, size_t input_len, uint8_t *output_buffer,
}

/* If the output buffer provided was not large enough, return an error. */
if (output_buffer_len < output_len)
if (output_buffer_len < output_len) {
return -1;
}

/* handle the case that an empty input is provided */
if (input_len == 0) {
output_buffer[0] = '\0';
}
size_t i;
int j = 0;
uint8_t val = 0;
/* Process every byte of input by keeping state across 3 byte blocks
* to capture 4 6-bit binary blocks that each map to a Base64 character.
*/
Expand All @@ -70,7 +71,7 @@ oc_base64_encode(const uint8_t *input, size_t input_len, uint8_t *output_buffer,
if (i % 3 == 0) {
val = (input[i] >> 2);
output_buffer[j++] = alphabet[val];
val = input[i] << 4;
val = (uint8_t)(input[i] << 4);
val &= 0x30;
}
/* This is the second byte of a 3 byte block of input. Combine
Expand All @@ -83,7 +84,7 @@ oc_base64_encode(const uint8_t *input, size_t input_len, uint8_t *output_buffer,
else if (i % 3 == 1) {
val |= (input[i] >> 4);
output_buffer[j++] = alphabet[val];
val = input[i] << 2;
val = (uint8_t)(input[i] << 2);
val &= 0x3D;
}
/* This is the last byte of a 3 byte block of input. Combine
Expand Down Expand Up @@ -164,29 +165,30 @@ oc_base64_decode(uint8_t *str, size_t len)
/* Return an error if we encounter a character that is outside
* of the Base64 alphabet.
*/
else
else {
return -1;
}

/* Decode all 4 byte blocks to 3 bytes of binary output by
* laying out their 6-bit blocks into a sequence of 3 bytes.
*/
if (i % 4 == 0) {
/* 1st 6 bits of output byte 1 */
val_c = val_s << 2;
val_c = (uint8_t)(val_s << 2);
val_c &= 0xFD;
} else if (i % 4 == 1) {
/* Last 2 bits of output byte 1 */
val_c |= (val_s >> 4);
str[j++] = val_c;
/* 1st 4 bits of output byte 2 */
val_c = val_s << 4;
val_c = (uint8_t)(val_s << 4);
val_c &= 0xF0;
} else if (i % 4 == 2) {
/* Last 4 bits of output byte 2 */
val_c |= (val_s >> 2);
str[j++] = val_c;
/* 1st 2 bits of output byte 3 */
val_c = val_s << 6;
val_c = (uint8_t)(val_s << 6);
val_c &= 0xD0;
} else {
/* Last 6 bits of output byte 3 */
Expand Down
2 changes: 1 addition & 1 deletion api/oc_helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ _oc_alloc_string_array(

for (size_t i = 0; i < size; ++i) {
size_t pos = i * STRING_ARRAY_ITEM_MAX_LEN;
memcpy((char *)oc_string(*ocstringarray) + pos, (const char *)"", 1);
memcpy(oc_string(*ocstringarray) + pos, (const char *)"", 1);
}
}

Expand Down
4 changes: 3 additions & 1 deletion api/oc_server_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,9 @@ oc_add_collection_v1(oc_resource_t *collection)
void
oc_add_collection(oc_resource_t *collection)
{
oc_add_collection_v1(collection);
if (!oc_add_collection_v1(collection)) {
OC_ERR("failed to add collection");
}
}

oc_resource_t *
Expand Down
14 changes: 14 additions & 0 deletions api/unittest/base64test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,20 @@ toString(From *arr, size_t arrSize)
return str;
}

TEST(B64Test, RFC4648_EncodeFail)
{
std::vector<std::string> inputs = {
"foo",
"foobar",
};

std::for_each(inputs.begin(), inputs.end(), [](const std::string &str) {
auto toEncode = fromString<uint8_t>(str);
EXPECT_EQ(-1,
oc_base64_encode(toEncode.data(), toEncode.size(), nullptr, 0));
});
}

/*
* Expected input and output comes from section 10 of RFC4648
*/
Expand Down
7 changes: 7 additions & 0 deletions api/unittest/logtest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "oc_log.h"
#include "port/oc_log_internal.h"
#include "util/oc_compiler.h"

#include <cstdarg>
#include <cstdio>
Expand Down Expand Up @@ -48,6 +49,12 @@ TEST_F(TestLog, LogToStdout)
OC_DBG("debug");
}

static void expectWarningOrError(oc_log_level_t log_level,
oc_log_component_t component, const char *file,
int line, const char *func_name,
const char *format, ...)
OC_PRINTF_FORMAT(6, 7);

static void
expectWarningOrError(oc_log_level_t log_level, oc_log_component_t component,
const char *file, int line, const char *func_name,
Expand Down
14 changes: 7 additions & 7 deletions api/unittest/ocapitest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -389,8 +389,7 @@ class ApiHelper {

oc_set_con_res_announced(false);

int initResult = oc_main_init(&s_handler);
if (initResult < 0) {
if (oc_main_init(&s_handler) < 0) {
errorMessage += "Initialization of main server failed";
s_isServerStarted = false;
return false;
Expand Down Expand Up @@ -517,8 +516,8 @@ ApiResource ApiHelper::s_TestResource{};

class ResourceDiscovered {
private:
std::set<std::string> requiredURI_;
std::set<std::string> deviceURI_;
std::set<std::string, std::less<>> requiredURI_;
std::set<std::string, std::less<>> deviceURI_;

void addRequired(const std::string &uri) { requiredURI_.insert(uri); }

Expand Down Expand Up @@ -558,8 +557,8 @@ class ResourceDiscovered {

class DevicesDiscovered {
private:
std::set<std::string> requiredDevices_;
std::set<std::string> devices_;
std::set<std::string, std::less<>> requiredDevices_;
std::set<std::string, std::less<>> devices_;

public:
bool isDone() const
Expand Down Expand Up @@ -925,7 +924,8 @@ TEST_F(TestObt, DiscoverUnownedResources)
for (const auto &device : devices) {
OC_PRINTF("Discovered unowned device: %s\n", device.c_str());
}
std::set<std::string> deviceUUIDs(devices.begin(), devices.end());
std::set<std::string, std::less<>> deviceUUIDs(devices.begin(),
devices.end());
if (ApiHelper::s_LightResource.enabled) {
EXPECT_EQ(1, deviceUUIDs.count(ApiHelper::s_LightResource.uuid));
deviceUUIDs.erase(ApiHelper::s_LightResource.uuid);
Expand Down
2 changes: 1 addition & 1 deletion api/unittest/plgdtimetest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -487,13 +487,13 @@ TEST_F(TestPlgdTimeWithServer, PostRequestFail)
ASSERT_TRUE(epOpt.has_value());
auto ep = std::move(*epOpt);

bool invoked = false;
auto post_handler = [](oc_client_response_t *data) {
oc::TestDevice::Terminate();
ASSERT_LT(OC_STATUS_NOT_MODIFIED, data->code);
*static_cast<bool *>(data->user_data) = true;
};

bool invoked = false;
ASSERT_TRUE(oc_init_post(PLGD_TIME_URI, &ep, nullptr, post_handler, HIGH_QOS,
&invoked));
oc_rep_start_root_object();
Expand Down
6 changes: 2 additions & 4 deletions api/unittest/swupdatetest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -778,8 +778,7 @@ postRequest(const std::function<void()> &payloadFn)
EXPECT_EQ(OC_STATUS_CHANGED, data->code);
oc::TestDevice::Terminate();
OC_DBG("POST payload: %s", oc::RepPool::GetJson(data->payload).data());
auto *invoked = static_cast<bool *>(data->user_data);
*invoked = true;
*static_cast<bool *>(data->user_data) = true;
};

bool invoked = false;
Expand Down Expand Up @@ -918,8 +917,7 @@ postRequestWithFailure(const std::function<void()> &payloadFn)
EXPECT_EQ(ErrorCode, data->code);
oc::TestDevice::Terminate();
OC_DBG("POST payload: %s", oc::RepPool::GetJson(data->payload).data());
auto *invoked = static_cast<bool *>(data->user_data);
*invoked = true;
*static_cast<bool *>(data->user_data) = true;
};

bool invoked = false;
Expand Down
3 changes: 2 additions & 1 deletion apps/client_certification_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,8 @@ read_pem(const char *file_path, char *buffer, size_t *buffer_len)
fclose(fp);
return -1;
}
if (fread(buffer, 1, pem_len, fp) < (size_t)pem_len) {
size_t to_read = (size_t)pem_len;
if (fread(buffer, 1, to_read, fp) < (size_t)pem_len) {
OC_PRINTF("ERROR: unable to read PEM\n");
fclose(fp);
return -1;
Expand Down
5 changes: 3 additions & 2 deletions apps/cloud_certification_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ read_pem(const char *file_path, char *buffer, size_t *buffer_len)
fclose(fp);
return -1;
}
if (pem_len > (long)*buffer_len) {
if (pem_len >= (long)*buffer_len) {
OC_PRINTF("ERROR: buffer provided too small\n");
fclose(fp);
return -1;
Expand All @@ -554,7 +554,8 @@ read_pem(const char *file_path, char *buffer, size_t *buffer_len)
fclose(fp);
return -1;
}
if (fread(buffer, 1, pem_len, fp) < (size_t)pem_len) {
size_t to_read = (size_t)pem_len;
if (fread(buffer, 1, to_read, fp) < (size_t)pem_len) {
OC_PRINTF("ERROR: unable to read PEM\n");
fclose(fp);
return -1;
Expand Down
5 changes: 3 additions & 2 deletions apps/cloud_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ read_pem(const char *file_path, char *buffer, size_t *buffer_len)
fclose(fp);
return -1;
}
if (pem_len > (long)*buffer_len) {
if (pem_len >= (long)*buffer_len) {
OC_PRINTF("ERROR: buffer provided too small\n");
fclose(fp);
return -1;
Expand All @@ -325,7 +325,8 @@ read_pem(const char *file_path, char *buffer, size_t *buffer_len)
fclose(fp);
return -1;
}
if (fread(buffer, 1, pem_len, fp) < (size_t)pem_len) {
size_t to_read = (size_t)pem_len;
if (fread(buffer, 1, to_read, fp) < (size_t)pem_len) {
OC_PRINTF("ERROR: unable to read PEM\n");
fclose(fp);
return -1;
Expand Down
6 changes: 4 additions & 2 deletions apps/cloud_proxy.c
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@
#endif /* OC_INTROSPECTION && OC_IDD_API */

#include <signal.h>
#include <stdlib.h>

#ifndef DOXYGEN
// Force doxygen to document static inline
Expand Down Expand Up @@ -1634,7 +1635,7 @@ read_pem(const char *file_path, char *buffer, size_t *buffer_len)
fclose(fp);
return -1;
}
if (pem_len > (long)*buffer_len) {
if (pem_len >= (long)*buffer_len) {
OC_PRINTF("ERROR: buffer provided too small\n");
fclose(fp);
return -1;
Expand All @@ -1644,7 +1645,8 @@ read_pem(const char *file_path, char *buffer, size_t *buffer_len)
fclose(fp);
return -1;
}
if (fread(buffer, 1, pem_len, fp) < (size_t)pem_len) {
size_t to_read = (size_t)pem_len;
if (fread(buffer, 1, to_read, fp) < (size_t)pem_len) {
OC_PRINTF("ERROR: unable to read PEM\n");
fclose(fp);
return -1;
Expand Down
5 changes: 3 additions & 2 deletions apps/cloud_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -828,7 +828,7 @@ read_pem(const char *file_path, char *buffer, size_t *buffer_len)
fclose(fp);
return -1;
}
if (pem_len > (long)*buffer_len) {
if (pem_len >= (long)*buffer_len) {
OC_PRINTF("ERROR: buffer provided too small\n");
fclose(fp);
return -1;
Expand All @@ -838,7 +838,8 @@ read_pem(const char *file_path, char *buffer, size_t *buffer_len)
fclose(fp);
return -1;
}
if (fread(buffer, 1, pem_len, fp) < (size_t)pem_len) {
size_t to_read = (size_t)pem_len;
if (fread(buffer, 1, to_read, fp) < (size_t)pem_len) {
OC_PRINTF("ERROR: unable to read PEM\n");
fclose(fp);
return -1;
Expand Down
3 changes: 2 additions & 1 deletion apps/server_certification_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -2023,7 +2023,8 @@ read_pem(const char *file_path, char *buffer, size_t *buffer_len)
fclose(fp);
return -1;
}
if (fread(buffer, 1, pem_len, fp) < (size_t)pem_len) {
size_t to_read = (size_t)pem_len;
if (fread(buffer, 1, to_read, fp) < (size_t)pem_len) {
OC_PRINTF("ERROR: unable to read PEM\n");
fclose(fp);
return -1;
Expand Down
3 changes: 2 additions & 1 deletion apps/smart_home_server_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -737,7 +737,8 @@ read_pem(const char *file_path, char *buffer, size_t *buffer_len)
fclose(fp);
return -1;
}
if (fread(buffer, 1, pem_len, fp) < (size_t)pem_len) {
size_t to_read = (size_t)pem_len;
if (fread(buffer, 1, to_read, fp) < (size_t)pem_len) {
printf("ERROR: unable to read PEM\n");
fclose(fp);
return -1;
Expand Down
3 changes: 2 additions & 1 deletion apps/smart_home_server_with_mock_swupdate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,8 @@ read_pem(const char *file_path, char *buffer, size_t *buffer_len)
fclose(fp);
return -1;
}
if (fread(buffer, 1, pem_len, fp) < (size_t)pem_len) {
size_t to_read = (size_t)pem_len;
if (fread(buffer, 1, to_read, fp) < (size_t)pem_len) {
OC_PRINTF("ERROR: unable to read PEM\n");
fclose(fp);
return -1;
Expand Down
1 change: 1 addition & 0 deletions include/oc_base64.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#ifndef OC_BASE64_H
#define OC_BASE64_H

#include "util/oc_compiler.h"
#include <stddef.h>
#include <stdint.h>

Expand Down
3 changes: 2 additions & 1 deletion include/oc_log.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ extern "C" {
#endif /* !OC_SNPRINTF */

#define OC_IPADDR_BUFF_SIZE \
64 // max size : scheme://[ipv6%scope]:port = 63 bytes
(64) // max size : scheme://[ipv6%scope]:port = 63 bytes

#define OC_SNPRINT_ENDPOINT_ADDR(str, size, endpoint, addr_memb) \
do { \
const char *scheme = "coap"; \
Expand Down
4 changes: 0 additions & 4 deletions messaging/coap/conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,6 @@ extern "C" {
(OC_MAX_APP_RESOURCES + OC_MAX_NUM_CONCURRENT_REQUESTS)
#endif /* COAP_MAX_OBSERVERS */

/* Interval in notifies in which NON notifies are changed to CON notifies to
* check client. */
#define COAP_OBSERVE_REFRESH_INTERVAL 5

#ifdef __cplusplus
}
#endif
Expand Down
Loading
Loading