Skip to content

Bugfix: utils: Avoid potential modulo by 0 if secret is zero length #126

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

Merged
merged 4 commits into from
Jul 14, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ add_executable(datum_gateway
src/datum_stratum_dupes.c
src/datum_submitblock.c
src/datum_utils.c
src/datum_utils_tests.c
src/thirdparty_base58.c
src/thirdparty_segwit_addr.c
${CMAKE_CURRENT_BINARY_DIR}/web_resources.h
Expand Down
6 changes: 6 additions & 0 deletions src/datum_gateway.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ static char doc[] = "Decentralized Alternative Templates for Universal Mining -
static char args_doc[] = "";
static struct argp_option options[] = {
{"help", '?', 0, 0, "Show custom help", 0},
{"test", 0x101, NULL, 0, "Run tests only", 0},
{"usage", '?', 0, 0, "Show custom help", 0},
{"config", 'c', "FILE", 0, "Configuration JSON file"},
{0}
Expand All @@ -77,6 +78,8 @@ struct arguments {
char *config_file;
};

void datum_utils_tests(void);

static error_t parse_opt(int key, char *arg, struct argp_state *state) {
struct arguments *arguments = state->input;
switch (key) {
Expand All @@ -89,6 +92,9 @@ static error_t parse_opt(int key, char *arg, struct argp_state *state) {
arguments->config_file = arg;
break;
}
case 0x101: // test
datum_utils_tests();
exit(datum_test_failed);
default:
return ARGP_ERR_UNKNOWN;
}
Expand Down
8 changes: 8 additions & 0 deletions src/datum_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,16 @@
#include "thirdparty_segwit_addr.h"
#include "datum_logger.h"

unsigned int datum_test_failed = 0;
volatile int panic_mode = 0;

bool datum_test_fail_(const char *expr, const char *file, unsigned int line, const char *func) {
fprintf(stderr, "ERROR: TEST FAILED at %s:%u (%s): %s\n", file, line, func, expr);
fflush(stderr);
++datum_test_failed;
return false;
}

void get_target_from_diff(unsigned char *result, uint64_t diff) {
uint64_t dividend_parts[4] = {0, 0, 0, 0x00000000FFFF0000};
uint64_t remainder = 0;
Expand Down
8 changes: 8 additions & 0 deletions src/datum_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@
#include "datum_logger.h"

void datum_utils_init(void);

extern unsigned int datum_test_failed;
bool datum_test_fail_(const char *expr, const char *file, unsigned int line, const char *func);
#define datum_test_(expr, fake_expr, line, fake_func) \
((expr) ? true : datum_test_fail_(fake_expr, __FILE__, line, fake_func))
#define datum_test(expr) \
datum_test_(expr, #expr, __LINE__, __func__)

uint64_t current_time_millis(void);
uint64_t current_time_micros(void);
unsigned char hex2bin_uchar(const char *in);
Expand Down
39 changes: 39 additions & 0 deletions src/datum_utils_tests.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
*
* DATUM Gateway
* Decentralized Alternative Templates for Universal Mining
*
* This file is part of OCEAN's Bitcoin mining decentralization
* project, DATUM.
*
* https://ocean.xyz
*
* ---
*
* Copyright (c) 2024 Bitcoin Ocean, LLC & Luke Dashjr
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/

#include "datum_utils.h"

void datum_utils_tests(void) {
}