diff --git a/CMakeLists.txt b/CMakeLists.txt index ab785f3..b882e49 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,7 +10,7 @@ set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY build) set(CMAKE_LIBRARY_OUTPUT_DIRECTORY build) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY build) -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unused-result -Wreturn-type") +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror") #-------Bootstrap contract------- diff --git a/bootstrap-contract/bootstrap_contract.cpp b/bootstrap-contract/bootstrap_contract.cpp index 1826759..b73363f 100644 --- a/bootstrap-contract/bootstrap_contract.cpp +++ b/bootstrap-contract/bootstrap_contract.cpp @@ -26,7 +26,7 @@ int main(int argc, char **argv) const void *input_mmap = hp_init_user_input_mmap(); // Iterate through all users. - for (int u = 0; u < ctx->users.count; u++) + for (size_t u = 0; u < ctx->users.count; u++) { const struct hp_user *user = &ctx->users.list[u]; @@ -35,7 +35,7 @@ int main(int argc, char **argv) continue; // Iterate through all inputs from this user. - for (int i = 0; i < user->inputs.count; i++) + for (size_t i = 0; i < user->inputs.count; i++) { const struct hp_user_input input = user->inputs.list[i]; diff --git a/bootstrap-contract/hotpocket_contract.h b/bootstrap-contract/hotpocket_contract.h index b3bfb6c..83b5f08 100644 --- a/bootstrap-contract/hotpocket_contract.h +++ b/bootstrap-contract/hotpocket_contract.h @@ -233,7 +233,7 @@ int hp_init_contract() } char buf[4096]; - const size_t len = read(STDIN_FILENO, buf, sizeof(buf)); + const ssize_t len = read(STDIN_FILENO, buf, sizeof(buf)); if (len == -1) { perror("Error when reading stdin."); @@ -271,14 +271,14 @@ int hp_deinit_contract() // Cleanup user and npl fd. close(cctx->users.in_fd); - for (int i = 0; i < cctx->users.count; i++) + for (size_t i = 0; i < cctx->users.count; i++) close(cctx->users.list[i].outfd); close(cctx->unl.npl_fd); // Cleanup user list allocation. if (cctx->users.list) { - for (int i = 0; i < cctx->users.count; i++) + for (size_t i = 0; i < cctx->users.count; i++) __HP_FREE(cctx->users.list[i].inputs.list); __HP_FREE(cctx->users.list); @@ -682,7 +682,7 @@ int __hp_encode_json_string_array(char *buf, const char *elems[], const size_t c struct hp_config *__hp_read_from_patch_file(const int fd) { char buf[4096]; - const size_t len = read(fd, buf, sizeof(buf)); + const ssize_t len = read(fd, buf, sizeof(buf)); if (len == -1) return NULL; @@ -722,22 +722,22 @@ int __hp_write_to_patch_file(const int fd, const struct hp_config *config) const size_t unl_buf_size = 20 + (69 * config->unl.count - (config->unl.count ? 1 : 0)) + (9 * config->unl.count); char unl_buf[unl_buf_size]; - strncpy(unl_buf, " \"unl\": [", 12); + memcpy(unl_buf, " \"unl\": [", 12); size_t pos = 12; - for (int i = 0; i < config->unl.count; i++) + for (size_t i = 0; i < config->unl.count; i++) { if (i > 0) unl_buf[pos++] = ','; - strncpy(unl_buf + pos, "\n ", 9); + memcpy(unl_buf + pos, "\n ", 9); pos += 9; unl_buf[pos++] = '"'; - strncpy(unl_buf + pos, config->unl.list[i].data, HP_KEY_SIZE); + memcpy(unl_buf + pos, config->unl.list[i].data, HP_KEY_SIZE); pos += HP_KEY_SIZE; unl_buf[pos++] = '"'; } - strncpy(unl_buf + pos, "\n ],\n", 8); + memcpy(unl_buf + pos, "\n ],\n", 8); iov_vec[1].iov_base = unl_buf; iov_vec[1].iov_len = unl_buf_size; @@ -825,7 +825,7 @@ void __hp_populate_patch_from_json_object(struct hp_config *config, const struct if (unl_count > 0) { struct json_array_element_s *unl_elem = unl_array->start; - for (int i = 0; i < unl_count; i++) + for (size_t i = 0; i < unl_count; i++) { __HP_ASSIGN_STRING(config->unl.list[i].data, unl_elem); unl_elem = unl_elem->next; @@ -956,7 +956,7 @@ void __hp_parse_args_json(const struct json_object_s *object) if (user_count > 0) { struct json_object_element_s *user_elem = user_object->start; - for (int i = 0; i < user_count; i++) + for (size_t i = 0; i < user_count; i++) { struct hp_user *user = &cctx->users.list[i]; memcpy(user->pubkey.data, user_elem->name->string, HP_KEY_SIZE); @@ -973,7 +973,7 @@ void __hp_parse_args_json(const struct json_object_s *object) // Subsequent elements are tupels of [offset, size] of input messages for this user. user->inputs.count = arr->length - 1; user->inputs.list = user->inputs.count ? (struct hp_user_input *)malloc(user->inputs.count * sizeof(struct hp_user_input)) : NULL; - for (int i = 0; i < user->inputs.count; i++) + for (size_t i = 0; i < user->inputs.count; i++) { if (arr_elem->value->type == json_type_array) { @@ -1010,7 +1010,7 @@ void __hp_parse_args_json(const struct json_object_s *object) if (unl_count > 0) { struct json_object_element_s *unl_elem = unl_obj->start; - for (int i = 0; i < unl_count; i++) + for (size_t i = 0; i < unl_count; i++) { // Each element(key) is named by the pubkey. strncpy(cctx->unl.list[i].pubkey.data, unl_elem->name->string, unl_elem->name->string_size); diff --git a/sashi-cli/main.cpp b/sashi-cli/main.cpp index e266901..5ff6149 100644 --- a/sashi-cli/main.cpp +++ b/sashi-cli/main.cpp @@ -103,10 +103,17 @@ int parse_cmd(int argc, char **argv) CLI11_PARSE(app, argc, argv); // Take the realpath of sashi cli exec path. - std::array buffer; - realpath(argv[0], buffer.data()); - buffer[PATH_MAX] = '\0'; - exec_dir = dirname(buffer.data()); + { + std::array buffer; + const char *res = realpath(argv[0], buffer.data()); + if (!res) + { + std::cerr << errno << ": Error in executable path." << std::endl; + return -1; + } + buffer[PATH_MAX] = '\0'; + exec_dir = dirname(buffer.data()); + } // Verifying subcommands. if (version->parsed()) diff --git a/src/comm/comm_handler.cpp b/src/comm/comm_handler.cpp index 5ae83b8..32c63ba 100644 --- a/src/comm/comm_handler.cpp +++ b/src/comm/comm_handler.cpp @@ -2,12 +2,12 @@ #include "../util/util.hpp" #include "../conf.hpp" -#define __HANDLE_RESPONSE(type, content, ret) \ - { \ - std::string res; \ - msg_parser.build_response(res, type, content, (type == msg::MSGTYPE_CREATE_RES || type == msg::MSGTYPE_LIST_RES || type == msg::MSGTYPE_INSPECT_RES) && ret == 0); \ - send(res); \ - return ret; \ +#define __HANDLE_RESPONSE(type, content, ret) \ + { \ + std::string res; \ + msg_parser.build_response(res, type, content, ((void *)type == (void *)msg::MSGTYPE_CREATE_RES || (void *)type == (void *)msg::MSGTYPE_LIST_RES || (void *)type == (void *)msg::MSGTYPE_INSPECT_RES) && ret == 0); \ + send(res); \ + return ret; \ } namespace comm @@ -186,7 +186,7 @@ namespace comm if (msg_parser.parse(msg) == -1 || msg_parser.extract_type(type) == -1) { read_buffer.clear(); - __HANDLE_RESPONSE("error", FORMAT_ERROR, -1); + __HANDLE_RESPONSE(msg::MSGTYPE_ERROR, FORMAT_ERROR, -1); } // Clear the buffer after the message is parsed. diff --git a/src/crypto.cpp b/src/crypto.cpp index dbc10f6..6a650ae 100644 --- a/src/crypto.cpp +++ b/src/crypto.cpp @@ -28,10 +28,10 @@ namespace crypto // Currently using ed25519. So append prefix byte to represent that. pubkey.resize(crypto_sign_ed25519_PUBLICKEYBYTES + 1); - pubkey[0] = KEYPFX_ed25519; + pubkey[0] = crypto::KEYPFX_ed25519; seckey.resize(crypto_sign_ed25519_SECRETKEYBYTES + 1); - seckey[0] = KEYPFX_ed25519; + seckey[0] = crypto::KEYPFX_ed25519; crypto_sign_ed25519_keypair( reinterpret_cast(pubkey.data() + 1), // +1 to skip the prefix byte. diff --git a/src/crypto.hpp b/src/crypto.hpp index 8f34b4d..f60b9c5 100644 --- a/src/crypto.hpp +++ b/src/crypto.hpp @@ -11,7 +11,7 @@ namespace crypto { // Prefix byte to append to ed25519 keys. - static unsigned char KEYPFX_ed25519 = 0xED; + constexpr unsigned char KEYPFX_ed25519 = 0xED; int init(); diff --git a/src/hp_manager.cpp b/src/hp_manager.cpp index ffb8828..b79f2c8 100644 --- a/src/hp_manager.cpp +++ b/src/hp_manager.cpp @@ -116,7 +116,7 @@ namespace hp LOG_ERROR << "Error getting allocated instance count from db."; return -1; } - else if (allocated_count >= conf::cfg.system.max_instance_count) + else if ((size_t)allocated_count >= conf::cfg.system.max_instance_count) { error_msg = MAX_ALLOCATION_REACHED; LOG_ERROR << "Max instance count is reached."; diff --git a/src/msg/json/msg_json.cpp b/src/msg/json/msg_json.cpp index 335b8d6..adfe250 100644 --- a/src/msg/json/msg_json.cpp +++ b/src/msg/json/msg_json.cpp @@ -645,7 +645,7 @@ namespace msg::json { msg.reserve(1024); msg += "["; - for (int i = 0; i < instances.size(); i++) + for (size_t i = 0; i < instances.size(); i++) { const hp::instance_info &instance = instances[i]; msg += "{\""; diff --git a/src/msg/msg_common.hpp b/src/msg/msg_common.hpp index 695fb65..7aa1083 100644 --- a/src/msg/msg_common.hpp +++ b/src/msg/msg_common.hpp @@ -190,6 +190,7 @@ namespace msg constexpr const char *MSGTYPE_INSPECT = "inspect"; // Message res types + constexpr const char *MSGTYPE_ERROR = "error"; constexpr const char *MSGTYPE_CREATE_RES = "create_res"; constexpr const char *MSGTYPE_CREATE_ERROR = "create_error"; constexpr const char *MSGTYPE_INITIATE_RES = "initiate_res"; diff --git a/src/sqlite.cpp b/src/sqlite.cpp index 5491ff1..6a906b9 100644 --- a/src/sqlite.cpp +++ b/src/sqlite.cpp @@ -145,7 +145,9 @@ namespace sqlite const int ret = exec_sql(db, sql); if (ret == -1) + { LOG_ERROR << "Error when creating sqlite table " << table_name; + } return ret; } @@ -166,7 +168,9 @@ namespace sqlite const int ret = exec_sql(db, sql); if (ret == -1) + { LOG_ERROR << "Error when creating sqlite index '" << index_name << "' in table " << table_name; + } return ret; } diff --git a/src/util/util.cpp b/src/util/util.cpp index 13b4491..2f0af01 100644 --- a/src/util/util.cpp +++ b/src/util/util.cpp @@ -127,7 +127,9 @@ namespace util const std::string realpath(std::string_view path) { std::array buffer; - ::realpath(path.data(), buffer.data()); + if (!::realpath(path.data(), buffer.data())) + return {}; + buffer[PATH_MAX] = '\0'; return buffer.data(); } @@ -469,13 +471,17 @@ namespace util { FILE *fpipe = popen(command, "r"); - if (fpipe == NULL) + if (!fpipe) { LOG_ERROR << "Error on popen for command " << std::string(command); return -1; } - fgets(output, output_len, fpipe); + if (!fgets(output, output_len, fpipe)) + { + LOG_ERROR << "Error on fgets for command " << std::string(command); + return -1; + } if (pclose(fpipe) < 0) {