Skip to content

Commit 2483dc3

Browse files
committed
Modern C clean-up: stdbool/stdint/stdatomic, fix casts, add uint256_to_str()
- Replace custom type definitions with <stdbool.h>, <stdint.h>, <stddef.h> (bool now _Bool, uint8/uint32 rebased on uint8_t/uint32_t) - Fix broken uintptr_t on 64-bit Linux - Replace custom GCC __sync_* atomics with <stdatomic.h> (C11) - Replace non-standard __FUNCTION__ with __func__ (41 occurrences) - Anonymous structs/unions in poll.c and config.c (C11) - Fix 19 hashtable_lookup casts from (void*) to (void**) - Remove ~20 unnecessary void* -> typed pointer casts in callbacks - Fix (char*)"secp256k1" const-discard, (void)0 case-label workaround - Fix free((void*)buf_orig) and (uint8*) const-discards in netasync.c - Add uint256_to_str() rotating-buffer convenience function - Guard TRUE/FALSE macros against ncurses redefinition
1 parent 2d6c303 commit 2483dc3

23 files changed

Lines changed: 221 additions & 371 deletions

File tree

apps/cli/bitc_ui.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,7 @@ bitcui_poll_shutdown(void)
585585
static void
586586
bitcui_exit(void)
587587
{
588-
log_info(LGPFX" %s\n", __FUNCTION__);
588+
log_info(LGPFX" %s\n", __func__);
589589

590590
fx_exit();
591591
poolworker_wait(btc->pw);

apps/cli/main.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -837,7 +837,7 @@ bitc_process_events(void)
837837
* BITC_STATE_EXITING.
838838
*/
839839
if (btc->stop == 2) {
840-
log_info(LGPFX" %s -- BITC_STATE_EXITING (CTRL-C)\n", __FUNCTION__);
840+
log_info(LGPFX" %s -- BITC_STATE_EXITING (CTRL-C)\n", __func__);
841841
btc->state = BITC_STATE_EXITING;
842842
}
843843

@@ -851,12 +851,12 @@ bitc_process_events(void)
851851

852852
switch (req->type) {
853853
case BTC_REQ_STOP:
854-
log_info(LGPFX" %s -- BITC_STATE_EXITING.\n", __FUNCTION__);
854+
log_info(LGPFX" %s -- BITC_STATE_EXITING.\n", __func__);
855855
btc->stop = 1;
856856
btc->state = BITC_STATE_EXITING;
857857
break;
858858
case BTC_REQ_TX:
859-
log_info(LGPFX" %s -- initiating tx.\n", __FUNCTION__);
859+
log_info(LGPFX" %s -- initiating tx.\n", __func__);
860860
struct btc_tx_desc *tx_desc = req->clientData;
861861
bitc_transmit_tx(tx_desc);
862862
free(tx_desc);
@@ -986,7 +986,7 @@ bitc_init(struct secure_area *passphrase,
986986
{
987987
int res;
988988

989-
log_info(LGPFX" %s -- BITC_STATE_STARTING.\n", __FUNCTION__);
989+
log_info(LGPFX" %s -- BITC_STATE_STARTING.\n", __func__);
990990
btc->state = BITC_STATE_STARTING;
991991
btc->wallet_state = WALLET_UNKNOWN;
992992
btc->updateAndExit = updateAndExit;
@@ -1046,7 +1046,7 @@ bitc_init(struct secure_area *passphrase,
10461046
static void
10471047
bitc_exit(void)
10481048
{
1049-
log_info(LGPFX" %s\n", __FUNCTION__);
1049+
log_info(LGPFX" %s\n", __func__);
10501050
rpc_exit();
10511051
peergroup_exit(btc->peerGroup);
10521052
btc->peerGroup = NULL;

core/addrbook.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ addrbook_replace_entry(struct addrbook *book,
7676
bool s;
7777

7878
s = hashtable_lookup(book->hash_addr, paddr->addr.ip,
79-
sizeof paddr->addr.ip, (void*)&paddr0);
79+
sizeof paddr->addr.ip, (void **)&paddr0);
8080
ASSERT(s);
8181
addrbook_remove_entry(book, paddr0);
8282
free(paddr0);
@@ -132,7 +132,7 @@ addrbook_get_rand_addr(const struct addrbook *book)
132132
key = NULL;
133133
keyLen = 0;
134134

135-
hashtable_get_entry_idx(book->hash_addr, idx, (void *)&key,
135+
hashtable_get_entry_idx(book->hash_addr, idx, (const void **)&key,
136136
&keyLen, (void**)&addr);
137137

138138
ASSERT(addr);
@@ -319,7 +319,7 @@ addrbook_save(struct addrbook *book)
319319
count = addrbook_get_count(book);
320320
ASSERT(count > 0);
321321

322-
hashtable_linearize(book->hash_addr, sizeof(btc_msg_address), (void *)&addrs);
322+
hashtable_linearize(book->hash_addr, sizeof(btc_msg_address), (void **)&addrs);
323323
ASSERT(addrs);
324324
len = count * sizeof *addrs;
325325

core/block-store.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,12 @@ blockstore_lookup(const struct blockstore *bs,
100100
be = NULL;
101101
mutex_lock(bs->lock);
102102

103-
s = hashtable_lookup(bs->hash_orphans, hash, sizeof *hash, (void *)&be);
103+
s = hashtable_lookup(bs->hash_orphans, hash, sizeof *hash, (void **)&be);
104104
if (s) {
105105
goto done;
106106
}
107-
s = hashtable_lookup(bs->hash_blk, hash, sizeof *hash, (void *)&be);
108-
if (s) {
107+
s = hashtable_lookup(bs->hash_blk, hash, sizeof *hash, (void **)&be);
108+
if (s) {
109109
goto done;
110110
}
111111
done:
@@ -213,9 +213,9 @@ blockstore_get_block_height(struct blockstore *bs,
213213

214214
mutex_lock(bs->lock);
215215

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

220220
uint256_snprintf_reverse(hashStr, sizeof hashStr, hash);
221221
Panic(LGPFX" block %s not found.\n", hashStr);
@@ -1085,7 +1085,7 @@ blockstore_is_next(struct blockstore *bs,
10851085

10861086
mutex_lock(bs->lock);
10871087

1088-
s = hashtable_lookup(bs->hash_blk, prev, sizeof *prev, (void*)&be);
1088+
s = hashtable_lookup(bs->hash_blk, prev, sizeof *prev, (void **)&be);
10891089
if (s == 0 || be->next == NULL) {
10901090
mutex_unlock(bs->lock);
10911091
return 0;
@@ -1123,7 +1123,7 @@ blockstore_get_next_hashes(struct blockstore *bs,
11231123

11241124
mutex_lock(bs->lock);
11251125

1126-
s = hashtable_lookup(bs->hash_blk, start, sizeof *start, (void*)&be);
1126+
s = hashtable_lookup(bs->hash_blk, start, sizeof *start, (void **)&be);
11271127
if (s == 0 || be->next == NULL) {
11281128
goto exit;
11291129
}

core/crypt.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ crypt_determine_count(const struct secure_area *pass,
107107
loop--;
108108
}
109109

110-
log_info(LGPFX" %s: result= %llu\n", __FUNCTION__, count);
110+
log_info(LGPFX" %s: result= %llu\n", __func__, count);
111111

112112
return MAX(CRYPT_NUM_ITERATIONS_MIN, count);
113113
}
@@ -176,7 +176,7 @@ crypt_encrypt(struct crypt_key *ckey,
176176
uint8 *c;
177177
int res;
178178

179-
log_info(LGPFX" %s:%u\n", __FUNCTION__, __LINE__);
179+
log_info(LGPFX" %s:%u\n", __func__, __LINE__);
180180

181181
*cipher = NULL;
182182
*cipher_len = 0;
@@ -196,7 +196,7 @@ crypt_encrypt(struct crypt_key *ckey,
196196

197197
if (res == 0) {
198198
log_info(LGPFX" %s: failed to encrypt %zu bytes\n",
199-
__FUNCTION__, plaintext->len);
199+
__func__, plaintext->len);
200200
OPENSSL_cleanse(c, clen);
201201
free(c);
202202
return 0;
@@ -244,7 +244,7 @@ crypt_decrypt(struct crypt_key *ckey,
244244
EVP_CIPHER_CTX_free(ctx);
245245

246246
if (res == 0) {
247-
log_info(LGPFX" %s: failed to decrypt %zu bytes\n", __FUNCTION__, cipher_len);
247+
log_info(LGPFX" %s: failed to decrypt %zu bytes\n", __func__, cipher_len);
248248
secure_free(sec);
249249
return 0;
250250
}

core/hash.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,16 @@ void uint256_snprintf_reverse(char *s, size_t len, const uint256 *h);
128128
void uint160_snprintf_reverse(char *s, size_t len, const uint160 *h);
129129
bool uint256_from_str(const char *str, uint256 *hash);
130130

131+
static inline const char *
132+
uint256_to_str(const uint256 *h)
133+
{
134+
static char buf[4][80];
135+
static int idx;
136+
char *b = buf[idx++ & 3];
137+
uint256_snprintf_reverse(b, 80, h);
138+
return b;
139+
}
140+
131141
void hash256_calc(const void *buf, size_t len, uint256 *hash);
132142
void hash160_calc(const void *buf, size_t bufLen, uint160 *digest);
133143
void hash4_calc(const void *buf, size_t len, uint8 hash[4]);

core/key.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ key_generate_new(void)
242242
OSSL_PARAM params[2];
243243

244244
params[0] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME,
245-
(char *)"secp256k1", 0);
245+
"secp256k1", 0);
246246
params[1] = OSSL_PARAM_construct_end();
247247

248248
gctx = EVP_PKEY_CTX_new_from_name(NULL, "EC", NULL);

core/peergroup.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ peergroup_free_tx_broadcast_cb(const void *key,
137137
size_t keylen,
138138
void *clientData)
139139
{
140-
struct tx_broadcast *txb = (struct tx_broadcast *)clientData;
140+
struct tx_broadcast *txb = clientData;
141141

142142
peergroup_free_tx_broadcast_entry(txb);
143143
}
@@ -159,9 +159,9 @@ peergroup_stop_broadcast_tx(struct peergroup *pg,
159159
char hashStr[80];
160160
bool s;
161161

162-
s = hashtable_lookup(pg->hash_broadcast, hash, sizeof *hash, (void*)&txb);
163-
if (s == 0) {
164-
return;
162+
s = hashtable_lookup(pg->hash_broadcast, hash, sizeof *hash, (void **)&txb);
163+
if (s == 0) {
164+
return;
165165
}
166166

167167
uint256_snprintf_reverse(hashStr, sizeof hashStr, hash);
@@ -193,9 +193,9 @@ peergroup_lookup_broadcast_tx(struct peergroup *pg,
193193

194194
*bufOut = NULL;
195195

196-
s = hashtable_lookup(pg->hash_broadcast, hash, sizeof *hash, (void*)&txb);
197-
if (s == 0) {
198-
return 0;
196+
s = hashtable_lookup(pg->hash_broadcast, hash, sizeof *hash, (void **)&txb);
197+
if (s == 0) {
198+
return 0;
199199
}
200200

201201
*bufOut = buff_dup(txb->buf);
@@ -444,7 +444,7 @@ peergroup_download_complete(void)
444444
if (btc->updateAndExit) {
445445
bitc_req_stop();
446446
} else {
447-
log_info(LGPFX" %s -- BITC_STATE_READY.\n", __FUNCTION__);
447+
log_info(LGPFX" %s -- BITC_STATE_READY.\n", __func__);
448448
btc->state = BITC_STATE_READY;
449449
peergroup_on_ready();
450450
}
@@ -1162,7 +1162,7 @@ peergroup_download_headers(struct peer *peer,
11621162
peergroup_download_progress();
11631163

11641164
if (btc->state == BITC_STATE_STARTING) {
1165-
log_info(LGPFX" %s -- BITC_STATE_UPDATE_HEADERS.\n", __FUNCTION__);
1165+
log_info(LGPFX" %s -- BITC_STATE_UPDATE_HEADERS.\n", __func__);
11661166
btc->state = BITC_STATE_UPDATE_HEADERS;
11671167
bitcui_set_status("online, fetching headers..");
11681168
if (btc->peerGroup->numHdrToFetch > 0) {
@@ -1261,7 +1261,7 @@ peergroup_download_filtered_blocks(struct peer *peer)
12611261
return 0;
12621262
}
12631263

1264-
log_info(LGPFX" %s -- BITC_STATE_UPDATE_TXDB.\n", __FUNCTION__);
1264+
log_info(LGPFX" %s -- BITC_STATE_UPDATE_TXDB.\n", __func__);
12651265
btc->state = BITC_STATE_UPDATE_TXDB;
12661266
bitcui_set_status("online, fetching tx..");
12671267

core/rpc.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ rpc_accept_cb(struct netasync_socket *socket,
4646
{
4747
ASSERT(err == 0);
4848

49-
log_info(LGPFX" %s:%u -- got a connection.\n", __FUNCTION__, __LINE__);
49+
log_info(LGPFX" %s:%u -- got a connection.\n", __func__, __LINE__);
5050

5151
netasync_close(socket); // for now.
5252
}
@@ -67,11 +67,11 @@ rpc_init(void)
6767
int err;
6868

6969
if (config_getbool(btc->config, 0, "rpc.enable") == 0) {
70-
log_info(LGPFX" %s: rpc disabled\n", __FUNCTION__);
70+
log_info(LGPFX" %s: rpc disabled\n", __func__);
7171
return 0;
7272
}
7373

74-
log_info(LGPFX" %s:%u\n", __FUNCTION__, __LINE__);
74+
log_info(LGPFX" %s:%u\n", __func__, __LINE__);
7575

7676
sock = netasync_create();
7777

@@ -85,7 +85,7 @@ rpc_init(void)
8585
log_info(LGPFX" failed to bind: %s (%d)\n", strerror(err), err);
8686
return err;
8787
}
88-
log_info(LGPFX" %s: listening on localhost:999.\n", __FUNCTION__);
88+
log_info(LGPFX" %s: listening on localhost:999.\n", __func__);
8989

9090
return 0;
9191
}

core/script.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ script_push_data(struct buff *buf,
5858
{
5959
ASSERT(buf->grow);
6060

61-
log_info("%s: len=%zu / %#zx\n", __FUNCTION__, len, len);
61+
log_info("%s: len=%zu / %#zx\n", __func__, len, len);
6262

6363
if (len < OP_PUSHDATA1) {
6464
serialize_uint8(buf, len);
@@ -621,8 +621,7 @@ script_sign(struct wallet *wallet,
621621
log_warn(LGPFX" script TX_PUBKEY\n");
622622
NOT_IMPLEMENTED();
623623
break;
624-
case TX_PUBKEYHASH:
625-
(void)0; // XXX: clang bug?
624+
case TX_PUBKEYHASH: {
626625
uint160 *keyHash = (uint160*)data_addr;
627626

628627
ASSERT(data_len == sizeof(uint160));
@@ -639,6 +638,7 @@ script_sign(struct wallet *wallet,
639638
goto exit;
640639
}
641640
break;
641+
}
642642
default:
643643
NOT_IMPLEMENTED();
644644
log_warn(LGPFX" script TX_NONSTANDARD\n");

0 commit comments

Comments
 (0)