Skip to content

Commit 65efc5b

Browse files
committed
Modernize deprecated APIs, replace VLAs, adopt uint256_to_str()
- signal() -> sigaction() (13 calls in main.c, ncui.c, fuzz-parse.c) - atoi()/atol() -> strtol() (4 sites) - usleep() -> nanosleep() (main.c) - localtime()/gmtime() -> _r variants (4 sites in util.c, ncui.c) - asctime() -> strftime() (util.c) - gettimeofday() -> clock_gettime() (2 sites in util.c) - VLAs -> safe_malloc (base58.c, script.c) - Adopt uint256_to_str() at ~30 call sites, removing char hashStr[80] boilerplate - Make uint256_to_str() thread-safe with _Thread_local - Batch getdata tx requests: 1 message instead of N (peer_handle_inv)
1 parent 800e1a3 commit 65efc5b

11 files changed

Lines changed: 158 additions & 165 deletions

File tree

apps/cli/main.c

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <fcntl.h>
77
#include <string.h>
88
#include <signal.h>
9+
#include <time.h>
910
#include <pthread.h>
1011
#include <curl/curl.h>
1112
#include <termios.h>
@@ -74,7 +75,9 @@ bool bitc_testing = 0;
7475
static void
7576
bitc_signal_int_default(void)
7677
{
77-
signal(SIGINT, SIG_DFL);
78+
struct sigaction sa = { .sa_handler = SIG_DFL };
79+
sigemptyset(&sa.sa_mask);
80+
sigaction(SIGINT, &sa, NULL);
7881
}
7982

8083

@@ -104,14 +107,22 @@ bitc_signal_handler(int sig)
104107
static void
105108
bitc_signal_install(void)
106109
{
107-
signal(SIGINT, bitc_sigint_handler);
108-
109-
signal(SIGSEGV, bitc_signal_handler);
110-
signal(SIGBUS, bitc_signal_handler);
111-
signal(SIGILL, bitc_signal_handler);
112-
113-
signal(SIGPIPE, SIG_IGN);
114-
signal(SIGHUP, SIG_IGN);
110+
struct sigaction sa;
111+
112+
sa = (struct sigaction){ .sa_handler = bitc_sigint_handler };
113+
sigemptyset(&sa.sa_mask);
114+
sigaction(SIGINT, &sa, NULL);
115+
116+
sa = (struct sigaction){ .sa_handler = bitc_signal_handler };
117+
sigemptyset(&sa.sa_mask);
118+
sigaction(SIGSEGV, &sa, NULL);
119+
sigaction(SIGBUS, &sa, NULL);
120+
sigaction(SIGILL, &sa, NULL);
121+
122+
sa = (struct sigaction){ .sa_handler = SIG_IGN };
123+
sigemptyset(&sa.sa_mask);
124+
sigaction(SIGPIPE, &sa, NULL);
125+
sigaction(SIGHUP, &sa, NULL);
115126
}
116127

117128

@@ -1143,10 +1154,10 @@ int main(int argc, char *argv[])
11431154
case 'c': configPath = optarg; break;
11441155
case 'C': btc->connectHost = optarg; break;
11451156
case 'S': btc->syncAndExit = 1; break;
1146-
case 'x': btc->stopAfterHeight = atoi(optarg); break;
1157+
case 'x': btc->stopAfterHeight = (int)strtol(optarg, NULL, 10); break;
11471158
case 'd': withui = 0; break;
11481159
case 'e': encrypt = 1; break;
1149-
case 'n': maxPeers = atoi(optarg); break;
1160+
case 'n': maxPeers = (int)strtol(optarg, NULL, 10); break;
11501161
case 'p': getpassword = 1; break;
11511162
case 't': testStr = optarg; break;
11521163
case 'T': btc->testnet = 1; break;
@@ -1162,7 +1173,10 @@ int main(int argc, char *argv[])
11621173

11631174
if (btc->testnet) {
11641175
printf("Using testnet.\n");
1165-
usleep(500 * 1000);
1176+
{
1177+
struct timespec ts = { .tv_sec = 0, .tv_nsec = 500 * 1000 * 1000 };
1178+
nanosleep(&ts, NULL);
1179+
}
11661180
}
11671181

11681182
log_set_level(1);

apps/cli/ncui.c

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -460,13 +460,18 @@ ncui_signal_cb(int sig)
460460
* possible (and in fact very common) to receive a signal while we're in the
461461
* middle of processing one.
462462
*/
463-
signal(SIGWINCH, SIG_IGN);
463+
struct sigaction sa = { .sa_handler = SIG_IGN };
464+
465+
sigemptyset(&sa.sa_mask);
466+
sigaction(SIGWINCH, &sa, NULL);
464467

465468
ncui_get_term_size(&y, &x);
466469
resizeterm(y, x);
467470
ncui_redraw();
468471

469-
signal(SIGWINCH, ncui_signal_cb);
472+
sa = (struct sigaction){ .sa_handler = ncui_signal_cb };
473+
sigemptyset(&sa.sa_mask);
474+
sigaction(SIGWINCH, &sa, NULL);
470475
}
471476

472477

@@ -582,12 +587,12 @@ ncui_status_update(bool update)
582587
static void
583588
ncui_update_time_str(void)
584589
{
585-
struct tm *tmp;
590+
struct tm tm_buf;
586591
time_t t;
587592

588593
t = time(NULL);
589-
tmp = localtime(&t);
590-
strftime(btc->ui->timeStr, sizeof btc->ui->timeStr, "%T", tmp);
594+
localtime_r(&t, &tm_buf);
595+
strftime(btc->ui->timeStr, sizeof btc->ui->timeStr, "%T", &tm_buf);
591596
}
592597

593598

@@ -1739,7 +1744,6 @@ ncui_blocklist_update(void)
17391744
ASSERT(mutex_islocked(btcui->lock));
17401745

17411746
while (btcui->blockConsIdx != btcui->blockProdIdx) {
1742-
char hashStr[80];
17431747
uint256 *hash;
17441748
uint32 timestamp;
17451749
int height;
@@ -1752,7 +1756,6 @@ ncui_blocklist_update(void)
17521756
height = btcui->blocks[btcui->blockConsIdx].height;
17531757
timestamp = btcui->blocks[btcui->blockConsIdx].timestamp;
17541758

1755-
uint256_snprintf_reverse(hashStr, sizeof hashStr, hash);
17561759
ts = print_time_local_short(timestamp);
17571760
orphan = last != -1 && height != last + 1;
17581761

@@ -1766,7 +1769,7 @@ ncui_blocklist_update(void)
17661769
mvwaddch(win, 0, 8, ACS_VLINE);
17671770
mvwprintw(win, 0, 10, "%s", ts);
17681771
mvwaddch(win, 0, 26, ACS_VLINE);
1769-
mvwprintw(win, 0, 28, "%s", hashStr);
1772+
mvwprintw(win, 0, 28, "%s", uint256_to_str(hash));
17701773
free(ts);
17711774
panel->num_lines = MIN(panel->max_lines, panel->num_lines + 1);
17721775
last = height;
@@ -2121,12 +2124,10 @@ ncui_dashboard_latest_blocks(WINDOW *win,
21212124
uint256 *hash = &btcui->blocks[idx].hash;
21222125
int height = btcui->blocks[idx].height;
21232126
time_t timestamp = btcui->blocks[idx].timestamp;
2124-
char hashStr[80];
21252127
char *ts;
21262128
bool orphan;
21272129

21282130
ts = print_time_local(timestamp, "%T");
2129-
uint256_snprintf_reverse(hashStr, sizeof hashStr, hash);
21302131

21312132
orphan = last != -1 && height != last - 1 && (last - height) < 100;
21322133
wattron(win, orphan ? PAIR_RED : PAIR_YELLOW);
@@ -2136,7 +2137,7 @@ ncui_dashboard_latest_blocks(WINDOW *win,
21362137
mvwprintw(win, y, 10, "%s", ts);
21372138
mvwaddch(win, y, 19, ACS_VLINE);
21382139
wattron(win, A_DIM);
2139-
mvwprintw(win, y, 21, "%s", hashStr);
2140+
mvwprintw(win, y, 21, "%s", uint256_to_str(hash));
21402141
wattroff(win, A_DIM);
21412142
y++;
21422143
free(ts);
@@ -2390,12 +2391,9 @@ ncui_dashboard_header(WINDOW *win,
23902391
mvwprintw(win, y, 1, "block: ");
23912392

23922393
if (btcui->numBlocks > 0) {
2393-
char hashStr[80];
23942394

2395-
uint256_snprintf_reverse(hashStr, sizeof hashStr,
2396-
&btcui->blocks[btcui->blockProdIdx].hash);
23972395
wattron(win,A_BOLD);
2398-
mvwprintw(win, y, 10, "%s", hashStr);
2396+
mvwprintw(win, y, 10, "%s", uint256_to_str(&btcui->blocks[btcui->blockProdIdx].hash));
23992397
wattroff(win,A_BOLD);
24002398
}
24012399
y++;
@@ -2601,7 +2599,11 @@ ncui_init(void)
26012599
ncui = safe_calloc(1, sizeof *ncui);
26022600
btc->ui = ncui;
26032601

2604-
signal(SIGWINCH, ncui_signal_cb);
2602+
{
2603+
struct sigaction sa = { .sa_handler = ncui_signal_cb };
2604+
sigemptyset(&sa.sa_mask);
2605+
sigaction(SIGWINCH, &sa, NULL);
2606+
}
26052607
ncui_ncurses_init();
26062608
panic_register_cb(ncui_on_panic_cb, NULL);
26072609
ncui_panel_init();

apps/test/fuzz-parse.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,15 @@ main(int argc, char **argv)
120120

121121
log_init("/tmp/fuzzharness.log");
122122
btc = calloc(1, sizeof *btc);
123-
signal(SIGSEGV, crash_handler);
124-
signal(SIGBUS, crash_handler);
125-
signal(SIGABRT, crash_handler);
123+
{
124+
struct sigaction sa;
125+
126+
sa = (struct sigaction){ .sa_handler = crash_handler };
127+
sigemptyset(&sa.sa_mask);
128+
sigaction(SIGSEGV, &sa, NULL);
129+
sigaction(SIGBUS, &sa, NULL);
130+
sigaction(SIGABRT, &sa, NULL);
131+
}
126132

127133
/* replay a single input: ./fuzz-parse --hex deadbeef... */
128134
if (argc > 2 && strcmp(argv[1], "--hex") == 0) {
@@ -141,7 +147,7 @@ main(int argc, char **argv)
141147
return 0;
142148
}
143149

144-
iters = argc > 1 ? atol(argv[1]) : 300000;
150+
iters = argc > 1 ? strtol(argv[1], NULL, 10) : 300000;
145151
for (i = 0; i < iters; i++) {
146152
size_t len = gen(base + (uint32)i * 2654435761u, g_bytes);
147153
g_iter = i;

core/base58.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,8 @@ base58_encode(const void *buf,
235235
size_t len)
236236
{
237237
const uint8 *buf0 = (uint8 *)buf;
238-
char str[len * 138 / 100 + 1];
239-
char rev[len + 1];
238+
char *str = safe_malloc(len * 138 / 100 + 1);
239+
char *rev = safe_malloc(len + 1);
240240
char *s = NULL;
241241
size_t strLen = 0;
242242
BN_CTX *ctx;
@@ -260,7 +260,7 @@ base58_encode(const void *buf,
260260

261261
str_copyreverse(rev, buf0, len);
262262
rev[len] = 0;
263-
base58_setvch(bn, rev, sizeof rev);
263+
base58_setvch(bn, rev, len + 1);
264264
s = NULL;
265265
ASSERT(!BN_is_zero(bn));
266266

@@ -288,6 +288,8 @@ base58_encode(const void *buf,
288288
str_copyreverse(s, str, strLen);
289289

290290
err:
291+
free(str);
292+
free(rev);
291293
BN_free(bn58);
292294
BN_free(bn0);
293295
BN_free(bn);

core/block-store.c

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -214,11 +214,9 @@ blockstore_get_block_height(struct blockstore *bs,
214214
mutex_lock(bs->lock);
215215

216216
s = hashtable_lookup(bs->hash_blk, hash, sizeof *hash, (void **)&be);
217-
if (s == 0) {
218-
char hashStr[80];
217+
if (s == 0) {
219218

220-
uint256_snprintf_reverse(hashStr, sizeof hashStr, hash);
221-
Panic(LGPFX" block %s not found.\n", hashStr);
219+
Panic(LGPFX" block %s not found.\n", uint256_to_str(hash));
222220
}
223221

224222
height = be->height;
@@ -321,15 +319,13 @@ blockstore_set_chain_links(struct blockstore *bs,
321319
struct blockentry *be)
322320
{
323321
struct blockentry *prev;
324-
char hashStr[80];
325322
uint256 hash;
326323
int height;
327324
bool s;
328325

329326
mutex_lock(bs->lock);
330327

331328
hash256_calc(&be->header, sizeof be->header, &hash);
332-
uint256_snprintf_reverse(hashStr, sizeof hashStr, &hash);
333329

334330
if (be->height >= 0) {
335331
struct blockentry *li;
@@ -338,13 +334,12 @@ blockstore_set_chain_links(struct blockstore *bs,
338334
* entries from now on need to be made orphans.
339335
*/
340336

341-
log_info(LGPFX" Reached block %s\n", hashStr);
337+
log_info(LGPFX" Reached block %s\n", uint256_to_str(&hash));
342338

343339
li = be->next;
344340
while (li) {
345341
hash256_calc(&li->header, sizeof li->header, &hash);
346-
uint256_snprintf_reverse(hashStr, sizeof hashStr, &hash);
347-
log_info(LGPFX" moving #%d %s from blk -> orphan\n", li->height, hashStr);
342+
log_info(LGPFX" moving #%d %s from blk -> orphan\n", li->height, uint256_to_str(&hash));
348343
s = hashtable_remove(bs->hash_blk, &hash, sizeof hash);
349344
ASSERT(s);
350345
li->height = -1;
@@ -363,7 +358,7 @@ blockstore_set_chain_links(struct blockstore *bs,
363358

364359
be->height = 1 + blockstore_set_chain_links(bs, prev);
365360

366-
log_info(LGPFX" moving #%d %s from orphan -> blk\n", be->height, hashStr);
361+
log_info(LGPFX" moving #%d %s from orphan -> blk\n", be->height, uint256_to_str(&hash));
367362

368363
prev->next = be;
369364
be->prev = prev;
@@ -507,15 +502,13 @@ blockstore_add_entry(struct blockstore *bs,
507502

508503
memcpy(&bs->best_hash, hash, sizeof *hash);
509504
} else {
510-
char hashStr[80];
511505
uint32 count;
512506

513507
be->height = -1;
514508
count = hashtable_getnumentries(bs->hash_orphans);
515509

516-
uint256_snprintf_reverse(hashStr, sizeof hashStr, hash);
517510
log_info(LGPFX" block %s orphaned. %u orphan%s total.\n",
518-
hashStr, count, count > 1 ? "s" : "");
511+
uint256_to_str(hash), count, count > 1 ? "s" : "");
519512

520513
s = hashtable_insert(bs->hash_orphans, hash, sizeof *hash, be);
521514
ASSERT(s);
@@ -823,11 +816,9 @@ blockset_open_file(struct blockstore *blockStore,
823816

824817
ts = time_get() - ts;
825818

826-
char hashStr[80];
827819
char *latStr;
828820

829-
uint256_snprintf_reverse(hashStr, sizeof hashStr, &blockStore->best_hash);
830-
log_info(LGPFX" loaded blocks up to %s\n", hashStr);
821+
log_info(LGPFX" loaded blocks up to %s\n", uint256_to_str(&blockStore->best_hash));
831822
latStr = print_latency(ts);
832823
log_info(LGPFX" this took %s\n", latStr);
833824
free(latStr);
@@ -1041,15 +1032,12 @@ blockstore_get_hash_from_birth(const struct blockstore *bs,
10411032

10421033
for (e = bs->best_chain; e != bs->genesis; e = e->prev) {
10431034
if (e->header.timestamp < birth) {
1044-
char hashStr[80];
10451035
char *s;
10461036
uint64 ts = birth;
10471037

10481038
hash256_calc(&e->header, sizeof e->header, hash);
1049-
1050-
uint256_snprintf_reverse(hashStr, sizeof hashStr, hash);
10511039
s = print_time_local(birth, "%c");
1052-
log_info(LGPFX" birth %llu (%s) --> block %s.\n", ts, s, hashStr);
1040+
log_info(LGPFX" birth %llu (%s) --> block %s.\n", ts, s, uint256_to_str(hash));
10531041
free(s);
10541042
return;
10551043
}
@@ -1253,10 +1241,8 @@ blockstore_get_block_timestamp(const struct blockstore *bs,
12531241

12541242
be = blockstore_lookup(bs, hash);
12551243
if (be == NULL) {
1256-
char hashStr[80];
12571244

1258-
uint256_snprintf_reverse(hashStr, sizeof hashStr, hash);
1259-
Panic(LGPFX" block %s not found.\n", hashStr);
1245+
Panic(LGPFX" block %s not found.\n", uint256_to_str(hash));
12601246
}
12611247

12621248
ts = be->header.timestamp;

core/hash.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@ bool uint256_from_str(const char *str, uint256 *hash);
131131
static inline const char *
132132
uint256_to_str(const uint256 *h)
133133
{
134-
static char buf[4][80];
135-
static int idx;
134+
static _Thread_local char buf[4][80];
135+
static _Thread_local int idx;
136136
char *b = buf[idx++ & 3];
137137
uint256_snprintf_reverse(b, 80, h);
138138
return b;

0 commit comments

Comments
 (0)