diff --git a/CMakeLists.txt b/CMakeLists.txt index 8c3500f..1de9f92 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -193,7 +193,6 @@ else() skalacoin_append_supported_c_flags(SKALACOIN_C_WARNINGS -Wall -Wextra - -Wpedantic -Wshadow -Wconversion -Wsign-conversion diff --git a/include/balance_sheet.h b/include/balance_sheet.h index d459c0d..6324308 100644 --- a/include/balance_sheet.h +++ b/include/balance_sheet.h @@ -47,7 +47,7 @@ extern khash_t(balance_sheet_map_m)* g_sheetMap; void BalanceSheet_Init(); int BalanceSheet_Insert(balance_sheet_entry_t entry); -bool BalanceSheet_Lookup(uint8_t* address, balance_sheet_entry_t* out); +bool BalanceSheet_Lookup(const uint8_t* address, balance_sheet_entry_t* out); bool BalanceSheet_SaveToFile(const char* outPath); bool BalanceSheet_LoadFromFile(const char* inPath); void BalanceSheet_Print(); diff --git a/include/constants.h b/include/constants.h index 5a3e5d6..3791783 100644 --- a/include/constants.h +++ b/include/constants.h @@ -133,7 +133,7 @@ static const size_t MAX_ORPHAN_BLOCKS = 512U; static const uint64_t IBD_TIP_AGE_BLOCKS = 20ULL; // Number of trailing blocks whose median timestamp is used for the age test above. Using a median // rather than the tip alone means a single miner cannot backdate one block to fake being in IBD. -static const size_t MEDIAN_TIME_SPAN = 11U; +#define MEDIAN_TIME_SPAN 11U // Reward schedule acceleration: 1 means normal-speed progression. #define EMISSION_ACCELERATION_FACTOR 1ULL diff --git a/include/utils.h b/include/utils.h index 3e7f0c7..37d65e0 100644 --- a/include/utils.h +++ b/include/utils.h @@ -38,7 +38,7 @@ static inline uint64_t get_current_time_ms(void) { return 0; // Handle error } // Convert seconds to milliseconds and add nanoseconds converted to milliseconds - return (spec.tv_sec * 1000) + (spec.tv_nsec / 1000000); + return ((uint64_t)spec.tv_sec * 1000ULL) + ((uint64_t)spec.tv_nsec / 1000000ULL); } static inline void sleep_for_microseconds(uint64_t microseconds) { diff --git a/src/autolykos2/autolykos2.c b/src/autolykos2/autolykos2.c index 35ed59b..6bc134e 100644 --- a/src/autolykos2/autolykos2.c +++ b/src/autolykos2/autolykos2.c @@ -459,8 +459,8 @@ bool Autolykos2_CheckTarget( } #ifdef SKALACOIN_AUTOLYKOS2_REF_AVAILABLE - if (ctx->backend) { - const bool ok = skalacoin_autolykos2_ref_check_target(ctx->backend, message32, nonce, height, target32); + if (ctx->backend && height <= UINT32_MAX) { + const bool ok = skalacoin_autolykos2_ref_check_target(ctx->backend, message32, nonce, (uint32_t)height, target32); if (Autolykos2_Hash(ctx, message32, 32, nonce, height, outHash)) { return ok; } diff --git a/src/balance_sheet.c b/src/balance_sheet.c index 4f7012a..3f86771 100644 --- a/src/balance_sheet.c +++ b/src/balance_sheet.c @@ -22,7 +22,7 @@ static bool BalanceSheet_GetSimEntry( return true; } - if (BalanceSheet_Lookup((uint8_t*)address, out)) { + if (BalanceSheet_Lookup(address, out)) { int ret = 0; k = kh_put(balance_sheet_map_m, simMap, key, &ret); if (k == kh_end(simMap)) { @@ -170,7 +170,7 @@ int BalanceSheet_Insert(balance_sheet_entry_t entry) { return ret; } -bool BalanceSheet_Lookup(uint8_t* address, balance_sheet_entry_t* out) { +bool BalanceSheet_Lookup(const uint8_t* address, balance_sheet_entry_t* out) { if (!address || !out) { return false; } pthread_mutex_lock(&g_sheetLock); diff --git a/src/block/chain.c b/src/block/chain.c index fbefd60..68d8141 100644 --- a/src/block/chain.c +++ b/src/block/chain.c @@ -41,8 +41,16 @@ static bool BuildPath(char* out, size_t outSize, const char* dirpath, const char return false; } - const int written = snprintf(out, outSize, "%s/%s", dirpath, filename); - return written > 0 && (size_t)written < outSize; + const size_t dirLen = strlen(dirpath); + const size_t fileLen = strlen(filename); + if (dirLen + 1 + fileLen >= outSize) { + return false; + } + + memcpy(out, dirpath, dirLen); + out[dirLen] = '/'; + memcpy(out + dirLen + 1, filename, fileLen + 1); + return true; } static bool BuildSpendAmount(const signed_transaction_t* tx, uint256_t* outSpend) { @@ -70,7 +78,7 @@ static bool CreditAddress(const uint8_t address[32], uint64_t amount) { } balance_sheet_entry_t entry; - if (BalanceSheet_Lookup((uint8_t*)address, &entry)) { + if (BalanceSheet_Lookup(address, &entry)) { if (uint256_add_u64(&entry.balance, amount)) { return false; } @@ -97,7 +105,7 @@ static bool DebitAddress(const uint8_t address[32], const uint256_t* amount, uin } balance_sheet_entry_t entry; - if (!BalanceSheet_Lookup((uint8_t*)address, &entry)) { + if (!BalanceSheet_Lookup(address, &entry)) { return false; } @@ -985,7 +993,7 @@ bool Chain_BlockRespectsSenderOrdering(const block_t* block) { // Baseline is THIS sender's own last included transaction. uint64_t lastSeen = 0; balance_sheet_entry_t senderEntry; - if (BalanceSheet_Lookup((uint8_t*)tx->transaction.senderAddress, &senderEntry)) { + if (BalanceSheet_Lookup(tx->transaction.senderAddress, &senderEntry)) { lastSeen = senderEntry.lastTxTimestamp; } @@ -1537,7 +1545,7 @@ bool Chain_LoadFromFile(blockchain_t* chain, const char* dirpath, uint256_t* out } // Seek to that position - if (fseek(chainFile, loc.byteNumber, SEEK_SET) != 0) { + if (loc.byteNumber > (uint64_t)LONG_MAX || fseek(chainFile, (long)loc.byteNumber, SEEK_SET) != 0) { fclose(chainFile); fclose(tableFile); return false; diff --git a/src/main.c b/src/main.c index 5c5afef..5939d06 100644 --- a/src/main.c +++ b/src/main.c @@ -42,7 +42,7 @@ uint64_t g_localNodeId = 0; // Randomised in main() before the node comes up pthread_rwlock_t g_chainLock; pthread_mutex_t g_balanceSheetLock; -void handle_sigint(int sig) { +static void handle_sigint(int sig) { printf("Caught signal %d, exiting...\n", sig); Block_ShutdownPowContext(); BalanceSheet_Destroy(); @@ -335,7 +335,7 @@ static void PrintBlockDetail(const block_t* block, size_t txCount, const uint8_t printf("\n"); } -static bool ComputeHistoricalAutolykosHashFromChain(const blockchain_t* chain, const block_t* block, uint64_t blockHeight, uint8_t outHash[32]) { +static bool ComputeHistoricalAutolykosHashFromChain(blockchain_t* chain, const block_t* block, uint64_t blockHeight, uint8_t outHash[32]) { if (!chain || !block || !outHash) { return false; } @@ -345,7 +345,7 @@ static bool ComputeHistoricalAutolykosHashFromChain(const blockchain_t* chain, c // that disagreed with the one in constants.h -- notably at exactly height EPOCH_LENGTH. uint8_t seed[32]; size_t dagBytes = 0; - if (!Chain_DagParamsForHeight((blockchain_t*)chain, blockHeight, &dagBytes, seed)) { + if (!Chain_DagParamsForHeight(chain, blockHeight, &dagBytes, seed)) { return false; } @@ -770,7 +770,7 @@ static bool VerifyChainFully(blockchain_t* chain) { } // Use when error -[[noreturn]] void KillEverythingAndExit(net_node_t* node, blockchain_t* chain) { +[[noreturn]] static void KillEverythingAndExit(net_node_t* node, blockchain_t* chain) { Node_Destroy(node); g_currentChain = NULL; Chain_Destroy(chain); @@ -914,7 +914,7 @@ int main(int argc, char* argv[]) { bool loadedWallet = false; // Attempt load - char* path = "chain_data/wallet.data"; // TODO: Don't hardcode path + const char* path = "chain_data/wallet.data"; // TODO: Don't hardcode path FILE* walletFile = fopen(path, "rb"); if (walletFile) { size_t read = fread(minerPrivateKey, 1, 32, walletFile);