Skip to content

Commit

Permalink
Remove tal_len, use tal_count() or tal_bytelen().
Browse files Browse the repository at this point in the history
tal_count() is used where there's a type, even if it's char or u8, and
tal_bytelen() is going to replace tal_len() for clarity: it's only needed
where a pointer is void.

We shim tal_bytelen() for now.

Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
rustyrussell authored and cdecker committed Jul 30, 2018
1 parent eae9b81 commit 5cf34d6
Show file tree
Hide file tree
Showing 57 changed files with 200 additions and 195 deletions.
4 changes: 2 additions & 2 deletions bitcoin/pullpush.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ void push_varint_blob(const tal_t *blob,
void (*push)(const void *, size_t, void *),
void *pushp)
{
push_varint(tal_len(blob), push, pushp);
push(blob, tal_len(blob), pushp);
push_varint(tal_bytelen(blob), push, pushp);
push(blob, tal_bytelen(blob), pushp);
}

void push(const void *data, size_t len, void *pptr_)
Expand Down
22 changes: 11 additions & 11 deletions bitcoin/script.c
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ u8 *p2wpkh_scriptcode(const tal_t *ctx, const struct pubkey *key)

bool is_p2pkh(const u8 *script, struct bitcoin_address *addr)
{
size_t script_len = tal_len(script);
size_t script_len = tal_count(script);

if (script_len != BITCOIN_SCRIPTPUBKEY_P2PKH_LEN)
return false;
Expand All @@ -390,7 +390,7 @@ bool is_p2pkh(const u8 *script, struct bitcoin_address *addr)

bool is_p2sh(const u8 *script, struct ripemd160 *addr)
{
size_t script_len = tal_len(script);
size_t script_len = tal_count(script);

if (script_len != BITCOIN_SCRIPTPUBKEY_P2SH_LEN)
return false;
Expand All @@ -407,7 +407,7 @@ bool is_p2sh(const u8 *script, struct ripemd160 *addr)

bool is_p2wsh(const u8 *script, struct sha256 *addr)
{
size_t script_len = tal_len(script);
size_t script_len = tal_count(script);

if (script_len != BITCOIN_SCRIPTPUBKEY_P2WSH_LEN)
return false;
Expand All @@ -422,7 +422,7 @@ bool is_p2wsh(const u8 *script, struct sha256 *addr)

bool is_p2wpkh(const u8 *script, struct bitcoin_address *addr)
{
size_t script_len = tal_len(script);
size_t script_len = tal_count(script);

if (script_len != BITCOIN_SCRIPTPUBKEY_P2WPKH_LEN)
return false;
Expand Down Expand Up @@ -668,7 +668,7 @@ u8 **bitcoin_witness_htlc_timeout_tx(const tal_t *ctx,
witness[1] = stack_sig(witness, remotehtlcsig);
witness[2] = stack_sig(witness, localhtlcsig);
witness[3] = stack_number(witness, 0);
witness[4] = tal_dup_arr(witness, u8, wscript, tal_len(wscript), 0);
witness[4] = tal_dup_arr(witness, u8, wscript, tal_count(wscript), 0);

return witness;
}
Expand All @@ -685,7 +685,7 @@ u8 **bitcoin_witness_htlc_success_tx(const tal_t *ctx,
witness[1] = stack_sig(witness, remotesig);
witness[2] = stack_sig(witness, localhtlcsig);
witness[3] = stack_preimage(witness, preimage);
witness[4] = tal_dup_arr(witness, u8, wscript, tal_len(wscript), 0);
witness[4] = tal_dup_arr(witness, u8, wscript, tal_count(wscript), 0);

return witness;
}
Expand Down Expand Up @@ -724,12 +724,12 @@ u8 *bitcoin_wscript_htlc_tx(const tal_t *ctx,
return script;
}

bool scripteq(const tal_t *s1, const tal_t *s2)
bool scripteq(const u8 *s1, const u8 *s2)
{
memcheck(s1, tal_len(s1));
memcheck(s2, tal_len(s2));
memcheck(s1, tal_count(s1));
memcheck(s2, tal_count(s2));

if (tal_len(s1) != tal_len(s2))
if (tal_count(s1) != tal_count(s2))
return false;
return memcmp(s1, s2, tal_len(s1)) == 0;
return memcmp(s1, s2, tal_count(s1)) == 0;
}
2 changes: 1 addition & 1 deletion bitcoin/script.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ bool is_p2wsh(const u8 *script, struct sha256 *addr);
bool is_p2wpkh(const u8 *script, struct bitcoin_address *addr);

/* Are these two scripts equal? */
bool scripteq(const tal_t *s1, const tal_t *s2);
bool scripteq(const u8 *s1, const u8 *s2);

/* OP_DUP + OP_HASH160 + PUSH(20-byte-hash) + OP_EQUALVERIFY + OP_CHECKSIG */
#define BITCOIN_SCRIPTPUBKEY_P2PKH_LEN (1 + 1 + 1 + 20 + 1 + 1)
Expand Down
8 changes: 4 additions & 4 deletions bitcoin/signature.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ static void dump_tx(const char *msg,
warnx("output[%zu].amount = %llu",
i, (long long)tx->output[i].amount);
warnx("output[%zu].script = %zu",
i, tal_len(tx->output[i].script));
for (j = 0; j < tal_len(tx->output[i].script); j++)
i, tal_count(tx->output[i].script));
for (j = 0; j < tal_count(tx->output[i].script); j++)
fprintf(stderr, "%02x", tx->output[i].script[j]);
fprintf(stderr, "\n");
}
warnx("input[%zu].script = %zu", inputnum, tal_len(script));
for (i = 0; i < tal_len(script); i++)
warnx("input[%zu].script = %zu", inputnum, tal_count(script));
for (i = 0; i < tal_count(script); i++)
fprintf(stderr, "%02x", script[i]);
if (key) {
fprintf(stderr, "\nPubkey: ");
Expand Down
2 changes: 1 addition & 1 deletion bitcoin/test/run-tx-encode.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ int main(void)

/* This is a p2sh-p2wpkh: */
/* ScriptSig is push of "version 0 + hash of pubkey" */
hexeq(tx->input[0].script, tal_len(tx->input[0].script),
hexeq(tx->input[0].script, tal_count(tx->input[0].script),
"16" "00" "144aa38e396e1394fb45cbf83f48d1464fbc9f498f");

/* Witness with 2 items */
Expand Down
4 changes: 2 additions & 2 deletions bitcoin/tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ static void pull_input(const tal_t *ctx, const u8 **cursor, size_t *max,
input->script = tal_arr(ctx, u8, script_len);
else
input->script = NULL;
pull(cursor, max, input->script, tal_len(input->script));
pull(cursor, max, input->script, tal_count(input->script));
input->sequence_number = pull_le32(cursor, max);
}

Expand All @@ -350,7 +350,7 @@ static void pull_output(const tal_t *ctx, const u8 **cursor, size_t *max,
{
output->amount = pull_value(cursor, max);
output->script = tal_arr(ctx, u8, pull_length(cursor, max, 1));
pull(cursor, max, output->script, tal_len(output->script));
pull(cursor, max, output->script, tal_count(output->script));
}

static u8 *pull_witness_item(const tal_t *ctx, const u8 **cursor, size_t *max)
Expand Down
4 changes: 4 additions & 0 deletions ccan_compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@
#define sha256_be16(ctx, v) ccan_sha256_be16(ctx, v)
#define sha256_be32(ctx, v) ccan_sha256_be32(ctx, v)
#define sha256_be64(ctx, v) ccan_sha256_be64(ctx, v)

/* Transition for ccan update. */
#define tal_bytelen(x) tal_len(x)

#endif /* LIGHTNING_CCAN_COMPAT_H */
10 changes: 5 additions & 5 deletions channeld/channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ static void billboard_update(const struct peer *peer)
/* Returns a pointer to the new end */
static void *tal_arr_append_(void **p, size_t size)
{
size_t n = tal_len(*p) / size;
size_t n = tal_bytelen(*p) / size;
tal_resize_(p, size, n+1, false);
return (char *)(*p) + n * size;
}
Expand All @@ -223,7 +223,7 @@ static void *tal_arr_append_(void **p, size_t size)
static void do_peer_write(struct peer *peer)
{
int r;
size_t len = tal_len(peer->peer_outmsg);
size_t len = tal_count(peer->peer_outmsg);

r = write(PEER_FD, peer->peer_outmsg + peer->peer_outoff,
len - peer->peer_outoff);
Expand Down Expand Up @@ -388,7 +388,7 @@ static void send_announcement_signatures(struct peer *peer)
strerror(errno));

/* Double-check that HSM gave valid signatures. */
sha256_double(&hash, ca + offset, tal_len(ca) - offset);
sha256_double(&hash, ca + offset, tal_count(ca) - offset);
if (!check_signed_hash(&hash, &peer->announcement_node_sigs[LOCAL],
&peer->node_ids[LOCAL])) {
/* It's ok to fail here, the channel announcement is
Expand Down Expand Up @@ -1735,7 +1735,7 @@ static void send_fail_or_fulfill(struct peer *peer, const struct htlc *h)
if (h->failcode & BADONION) {
/* Malformed: use special reply since we can't onion. */
struct sha256 sha256_of_onion;
sha256(&sha256_of_onion, h->routing, tal_len(h->routing));
sha256(&sha256_of_onion, h->routing, tal_count(h->routing));

msg = towire_update_fail_malformed_htlc(NULL, &peer->channel_id,
h->id, &sha256_of_onion,
Expand Down Expand Up @@ -2090,7 +2090,7 @@ static void handle_offer_htlc(struct peer *peer, const u8 *inmsg)
u8 onion_routing_packet[TOTAL_PACKET_SIZE];
enum channel_add_err e;
enum onion_type failcode;
/* Subtle: must be tal_arr since we marshal using tal_len() */
/* Subtle: must be tal object since we marshal using tal_bytelen() */
const char *failmsg;

if (!peer->funding_locked[LOCAL] || !peer->funding_locked[REMOTE])
Expand Down
2 changes: 1 addition & 1 deletion channeld/full_channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -1049,7 +1049,7 @@ bool channel_force_htlcs(struct channel *channel,
if (failed[i]->failreason)
htlc->fail = tal_dup_arr(htlc, u8,
failed[i]->failreason,
tal_len(failed[i]->failreason),
tal_count(failed[i]->failreason),
0);
else
htlc->fail = NULL;
Expand Down
6 changes: 3 additions & 3 deletions channeld/test/run-full_channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,8 @@ static void tx_must_be_eq(const struct bitcoin_tx *a,
lina = linearize_tx(tmpctx, a);
linb = linearize_tx(tmpctx, b);

for (i = 0; i < tal_len(lina); i++) {
if (i >= tal_len(linb))
for (i = 0; i < tal_count(lina); i++) {
if (i >= tal_count(linb))
errx(1, "Second tx is truncated:\n"
"%s\n"
"%s",
Expand All @@ -202,7 +202,7 @@ static void tx_must_be_eq(const struct bitcoin_tx *a,
tal_hex(tmpctx, lina),
tal_hex(tmpctx, linb));
}
if (i != tal_len(linb))
if (i != tal_count(linb))
errx(1, "First tx is truncated:\n"
"%s\n"
"%s",
Expand Down
2 changes: 1 addition & 1 deletion cli/test/run-large-input.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ int main(int argc UNUSED, char *argv[])
response_off += sizeof(TAILER)-1;
response[response_off++] = '\0';
assert(strlen(response) == response_off - 1);
assert(response_off < tal_len(response));
assert(response_off < tal_count(response));

response_off = 0;
max_read_return = -1;
Expand Down
4 changes: 2 additions & 2 deletions common/base32.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ char *b32_encode(const tal_t *ctx, const void *data, size_t len)
char *str = tal_arr(ctx, char, base32_str_size(len));

base32_chars = base32_lower;
base32_encode(data, len, str, tal_len(str));
base32_encode(data, len, str, tal_count(str));
return str;
}

Expand All @@ -18,7 +18,7 @@ u8 *b32_decode(const tal_t *ctx, const char *str, size_t len)
u8 *ret = tal_arr(ctx, u8, base32_data_size(str, len));

base32_chars = base32_lower;
if (!base32_decode(str, len, ret, tal_len(ret)))
if (!base32_decode(str, len, ret, tal_count(ret)))
return tal_free(ret);
return ret;
}
2 changes: 1 addition & 1 deletion common/bech32_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ static u8 get_bit(const u8 *src, size_t bitoff)
void bech32_push_bits(u5 **data, const void *src, size_t nbits)
{
size_t i, b;
size_t data_len = tal_len(*data);
size_t data_len = tal_count(*data);

for (i = 0; i < nbits; i += b) {
tal_resize(data, data_len+1);
Expand Down
16 changes: 8 additions & 8 deletions common/bolt11.c
Original file line number Diff line number Diff line change
Expand Up @@ -351,15 +351,15 @@ static char *decode_f(struct bolt11 *b11,
} else if (version < 17) {
u8 *f = tal_arr(b11, u8, data_length * 5 / 8);
if (version == 0) {
if (tal_len(f) != 20 && tal_len(f) != 32)
if (tal_count(f) != 20 && tal_count(f) != 32)
return tal_fmt(b11,
"f: witness v0 bad length %zu",
data_length);
}
pull_bits_certain(hu5, data, data_len, f, data_length * 5,
false);
fallback = scriptpubkey_witness_raw(b11, version,
f, tal_len(f));
f, tal_count(f));
tal_free(f);
} else
return unknown_field(b11, hu5, data, data_len, 'f',
Expand Down Expand Up @@ -816,16 +816,16 @@ static void encode_f(u5 **data, const u8 *fallback)
push_fallback_addr(data, 0, &pkh, sizeof(pkh));
} else if (is_p2wsh(fallback, &wsh)) {
push_fallback_addr(data, 0, &wsh, sizeof(wsh));
} else if (tal_len(fallback)
} else if (tal_count(fallback)
&& fallback[0] >= 0x50
&& fallback[0] < (0x50+16)) {
/* Other (future) witness versions: turn OP_N into N */
push_fallback_addr(data, fallback[0] - 0x50, fallback + 1,
tal_len(fallback) - 1);
tal_count(fallback) - 1);
} else {
/* Copy raw. */
push_field(data, 'f',
fallback, tal_len(fallback) * CHAR_BIT);
fallback, tal_count(fallback) * CHAR_BIT);
}
}

Expand All @@ -836,7 +836,7 @@ static void encode_r(u5 **data, const struct route_info *r)
for (size_t i = 0; i < tal_count(r); i++)
towire_route_info(&rinfo, r);

push_field(data, 'r', rinfo, tal_len(rinfo) * CHAR_BIT);
push_field(data, 'r', rinfo, tal_count(rinfo) * CHAR_BIT);
tal_free(rinfo);
}

Expand All @@ -848,7 +848,7 @@ static void encode_extra(u5 **data, const struct bolt11_field *extra)
push_varlen_uint(data, tal_count(extra->data), 10);

/* extra->data is already u5s, so do this raw. */
len = tal_len(*data);
len = tal_count(*data);
tal_resize(data, len + tal_count(extra->data));
memcpy(*data + len, extra->data, tal_count(extra->data));
}
Expand Down Expand Up @@ -938,7 +938,7 @@ char *bolt11_encode_(const tal_t *ctx,
encode_extra(&data, extra);

/* FIXME: towire_ should check this? */
if (tal_len(data) > 65535)
if (tal_count(data) > 65535)
return NULL;

/* Need exact length here */
Expand Down
4 changes: 2 additions & 2 deletions common/crypto_sync.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ bool sync_crypto_write(struct crypto_state *cs, int fd, const void *msg TAKES)
break;
}
#endif
ret = write_all(fd, enc, tal_len(enc));
ret = write_all(fd, enc, tal_count(enc));
tal_free(enc);

#if DEVELOPER
Expand All @@ -64,7 +64,7 @@ u8 *sync_crypto_read(const tal_t *ctx, struct crypto_state *cs, int fd)
}

enc = tal_arr(ctx, u8, len + 16);
if (!read_all(fd, enc, tal_len(enc))) {
if (!read_all(fd, enc, tal_count(enc))) {
status_trace("Failed reading body: %s", strerror(errno));
return tal_free(enc);
}
Expand Down
4 changes: 2 additions & 2 deletions common/decode_short_channel_ids.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ static u8 *unzlib(const tal_t *ctx, const u8 *encoded, size_t len)
struct short_channel_id *decode_short_ids(const tal_t *ctx, const u8 *encoded)
{
struct short_channel_id *scids;
size_t max = tal_len(encoded), n;
size_t max = tal_count(encoded), n;
enum scid_encode_types type;

/* BOLT #7:
Expand All @@ -43,7 +43,7 @@ struct short_channel_id *decode_short_ids(const tal_t *ctx, const u8 *encoded)
encoded = unzlib(tmpctx, encoded, max);
if (!encoded)
return NULL;
max = tal_len(encoded);
max = tal_count(encoded);
/* fall thru */
case SHORTIDS_UNCOMPRESSED:
n = 0;
Expand Down
8 changes: 4 additions & 4 deletions common/features.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ static const u32 global_features[] = {
*/
static void set_bit(u8 **ptr, u32 bit)
{
size_t len = tal_len(*ptr);
size_t len = tal_count(*ptr);
if (bit / 8 >= len) {
size_t newlen = (bit / 8) + 1;
u8 *newarr = tal_arrz(tal_parent(*ptr), u8, newlen);
Expand All @@ -31,8 +31,8 @@ static void set_bit(u8 **ptr, u32 bit)

static bool test_bit(const u8 *features, size_t byte, unsigned int bit)
{
assert(byte < tal_len(features));
return features[tal_len(features) - 1 - byte] & (1 << (bit % 8));
assert(byte < tal_count(features));
return features[tal_count(features) - 1 - byte] & (1 << (bit % 8));
}

/* We don't insist on anything, it's all optional. */
Expand All @@ -59,7 +59,7 @@ static bool feature_set(const u8 *features, size_t bit)
{
size_t bytenum = bit / 8;

if (bytenum >= tal_len(features))
if (bytenum >= tal_count(features))
return false;

return test_bit(features, bytenum, bit % 8);
Expand Down
4 changes: 2 additions & 2 deletions common/htlc_wire.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ void towire_fulfilled_htlc(u8 **pptr, const struct fulfilled_htlc *fulfilled)
void towire_failed_htlc(u8 **pptr, const struct failed_htlc *failed)
{
/* Only one can be set. */
assert(failed->failcode || tal_len(failed->failreason));
assert(!failed->failcode || !tal_len(failed->failreason));
assert(failed->failcode || tal_count(failed->failreason));
assert(!failed->failcode || !tal_count(failed->failreason));
towire_u64(pptr, failed->id);
towire_u16(pptr, failed->failcode);
if (failed->failcode & UPDATE) {
Expand Down
Loading

0 comments on commit 5cf34d6

Please sign in to comment.