Remove tal_len, use tal_count() or tal_bytelen().

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 <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell
2018-07-28 15:30:16 +09:30
committed by Christian Decker
parent eae9b81099
commit 5cf34d6618
57 changed files with 200 additions and 195 deletions

View File

@@ -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;
}
@@ -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;
}

View File

@@ -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);

View File

@@ -351,7 +351,7 @@ 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);
@@ -359,7 +359,7 @@ static char *decode_f(struct bolt11 *b11,
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',
@@ -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);
}
}
@@ -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);
}
@@ -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));
}
@@ -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 */

View File

@@ -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
@@ -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);
}

View File

@@ -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:
@@ -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;

View File

@@ -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);
@@ -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. */
@@ -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);

View File

@@ -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) {

View File

@@ -423,7 +423,7 @@ void json_add_hex_talarr(struct json_result *result,
const char *fieldname,
const tal_t *data)
{
json_add_hex(result, fieldname, data, tal_len(data));
json_add_hex(result, fieldname, data, tal_bytelen(data));
}
void json_add_object(struct json_result *result, ...)

View File

@@ -128,7 +128,7 @@ static void scan_for_pointers(struct htable *memtable, const tal_t *p)
size_t i, n;
/* Search for (aligned) pointers. */
n = tal_len(p) / sizeof(void *);
n = tal_bytelen(p) / sizeof(void *);
for (i = 0; i < n; i++) {
void *ptr;

View File

@@ -13,7 +13,7 @@ static void do_enqueue(struct msg_queue *q, const u8 *add)
{
size_t n = tal_count(q->q);
tal_resize(&q->q, n+1);
q->q[n] = tal_dup_arr(q->ctx, u8, add, tal_len(add), 0);
q->q[n] = tal_dup_arr(q->ctx, u8, add, tal_count(add), 0);
/* In case someone is waiting */
io_wake(q);

View File

@@ -14,9 +14,9 @@ static bool input_better(const struct bitcoin_tx_input *a,
return a->index < b->index;
/* These shouldn't happen, but let's get a canonical order anyway. */
if (tal_len(a->script) != tal_len(b->script))
return tal_len(a->script) < tal_len(b->script);
cmp = memcmp(a->script, b->script, tal_len(a->script));
if (tal_count(a->script) != tal_count(b->script))
return tal_count(a->script) < tal_count(b->script);
cmp = memcmp(a->script, b->script, tal_count(a->script));
if (cmp != 0)
return cmp < 0;
return a->sequence_number < b->sequence_number;
@@ -102,16 +102,16 @@ static bool output_better(const struct bitcoin_tx_output *a,
return a->amount < b->amount;
/* Lexicographical sort. */
if (tal_len(a->script) < tal_len(b->script))
len = tal_len(a->script);
if (tal_count(a->script) < tal_count(b->script))
len = tal_count(a->script);
else
len = tal_len(b->script);
len = tal_count(b->script);
ret = memcmp(a->script, b->script, len);
if (ret != 0)
return ret < 0;
return tal_len(a->script) < tal_len(b->script);
return tal_count(a->script) < tal_count(b->script);
}
static size_t find_best_out(struct bitcoin_tx_output *outputs, size_t num)

View File

@@ -78,12 +78,12 @@ const char *got_pong(const u8 *pong, size_t *num_pings_outstanding)
if (*num_pings_outstanding == 0)
return "Unexpected pong";
for (i = 0; i < tal_len(ignored); i++) {
for (i = 0; i < tal_count(ignored); i++) {
if (ignored[i] < ' ' || ignored[i] == 127)
break;
}
status_trace("Got pong %zu bytes (%.*s...)",
tal_len(ignored), i, (char *)ignored);
tal_count(ignored), i, (char *)ignored);
(*num_pings_outstanding)--;
return NULL;

View File

@@ -319,7 +319,7 @@ static void serialize_hop_data(tal_t *ctx, u8 *dst, const struct hop_data *data)
towire_u32(&buf, data->outgoing_cltv);
towire_pad(&buf, 12);
towire(&buf, data->hmac, SECURITY_PARAMETER);
memcpy(dst, buf, tal_len(buf));
memcpy(dst, buf, tal_count(buf));
tal_free(buf);
}
@@ -456,7 +456,7 @@ struct route_step *process_onionpacket(
u8 *create_onionreply(const tal_t *ctx, const struct secret *shared_secret,
const u8 *failure_msg)
{
size_t msglen = tal_len(failure_msg);
size_t msglen = tal_count(failure_msg);
size_t padlen = ONION_REPLY_SIZE - msglen;
u8 *reply = tal_arr(ctx, u8, 0), *payload = tal_arr(ctx, u8, 0);
u8 key[KEY_LEN];
@@ -488,7 +488,7 @@ u8 *create_onionreply(const tal_t *ctx, const struct secret *shared_secret,
* - Note: this value is 118 bytes longer than the longest
* currently-defined message.
*/
assert(tal_len(payload) == ONION_REPLY_SIZE + 4);
assert(tal_count(payload) == ONION_REPLY_SIZE + 4);
/* BOLT #4:
*
@@ -497,10 +497,10 @@ u8 *create_onionreply(const tal_t *ctx, const struct secret *shared_secret,
*/
generate_key(key, "um", 2, shared_secret->data);
compute_hmac(hmac, payload, tal_len(payload), key, KEY_LEN);
compute_hmac(hmac, payload, tal_count(payload), key, KEY_LEN);
towire(&reply, hmac, sizeof(hmac));
towire(&reply, payload, tal_len(payload));
towire(&reply, payload, tal_count(payload));
tal_free(payload);
return reply;
@@ -510,7 +510,7 @@ u8 *wrap_onionreply(const tal_t *ctx,
const struct secret *shared_secret, const u8 *reply)
{
u8 key[KEY_LEN];
size_t streamlen = tal_len(reply);
size_t streamlen = tal_count(reply);
u8 stream[streamlen];
u8 *result = tal_arr(ctx, u8, streamlen);
@@ -533,17 +533,17 @@ struct onionreply *unwrap_onionreply(const tal_t *ctx,
const int numhops, const u8 *reply)
{
struct onionreply *oreply = tal(tmpctx, struct onionreply);
u8 *msg = tal_arr(oreply, u8, tal_len(reply));
u8 *msg = tal_arr(oreply, u8, tal_count(reply));
u8 key[KEY_LEN], hmac[HMAC_SIZE];
const u8 *cursor;
size_t max;
u16 msglen;
if (tal_len(reply) != ONION_REPLY_SIZE + sizeof(hmac) + 4) {
if (tal_count(reply) != ONION_REPLY_SIZE + sizeof(hmac) + 4) {
return NULL;
}
memcpy(msg, reply, tal_len(reply));
memcpy(msg, reply, tal_count(reply));
oreply->origin_index = -1;
for (int i = 0; i < numhops; i++) {
@@ -555,7 +555,7 @@ struct onionreply *unwrap_onionreply(const tal_t *ctx,
* the origin */
generate_key(key, "um", 2, shared_secrets[i].data);
compute_hmac(hmac, msg + sizeof(hmac),
tal_len(msg) - sizeof(hmac), key, KEY_LEN);
tal_count(msg) - sizeof(hmac), key, KEY_LEN);
if (memcmp(hmac, msg, sizeof(hmac)) == 0) {
oreply->origin_index = i;
break;
@@ -566,7 +566,7 @@ struct onionreply *unwrap_onionreply(const tal_t *ctx,
}
cursor = msg + sizeof(hmac);
max = tal_len(msg) - sizeof(hmac);
max = tal_count(msg) - sizeof(hmac);
msglen = fromwire_u16(&cursor, &max);
if (msglen > ONION_REPLY_SIZE) {

View File

@@ -48,11 +48,11 @@ static bool test_sign(const u5 *u5bytes,
char *hrp;
struct sha256 sha;
hrp = tal_dup_arr(NULL, char, (char *)hrpu8, tal_len(hrpu8), 1);
hrp[tal_len(hrpu8)] = '\0';
hrp = tal_dup_arr(NULL, char, (char *)hrpu8, tal_count(hrpu8), 1);
hrp[tal_count(hrpu8)] = '\0';
hash_u5_init(&hu5, hrp);
hash_u5(&hu5, u5bytes, tal_len(u5bytes));
hash_u5(&hu5, u5bytes, tal_count(u5bytes));
hash_u5_done(&hu5, &sha);
tal_free(hrp);
@@ -94,9 +94,9 @@ static void test_b11(const char *b11str,
assert(tal_count(b11->fallbacks) == tal_count(expect_b11->fallbacks));
for (size_t i = 0; i < tal_count(b11->fallbacks); i++)
assert(memeq(b11->fallbacks[i], tal_len(b11->fallbacks[i]),
assert(memeq(b11->fallbacks[i], tal_count(b11->fallbacks[i]),
expect_b11->fallbacks[i],
tal_len(expect_b11->fallbacks[i])));
tal_count(expect_b11->fallbacks[i])));
/* FIXME: compare routes. */
assert(tal_count(b11->routes) == tal_count(expect_b11->routes));

View File

@@ -60,7 +60,7 @@ int main(void)
assert(!feature_offered(bits, 16));
/* We always support no features. */
memset(bits, 0, tal_len(bits));
memset(bits, 0, tal_count(bits));
assert(features_supported(bits, bits));
/* We must support our own features. */
@@ -70,18 +70,18 @@ int main(void)
/* We can add random odd features, no problem. */
for (size_t i = 1; i < 16; i += 2) {
bits = tal_dup_arr(tmpctx, u8, lf, tal_len(lf), 0);
bits = tal_dup_arr(tmpctx, u8, lf, tal_count(lf), 0);
set_bit(&bits, i);
assert(features_supported(gf, bits));
bits = tal_dup_arr(tmpctx, u8, gf, tal_len(gf), 0);
bits = tal_dup_arr(tmpctx, u8, gf, tal_count(gf), 0);
set_bit(&bits, i);
assert(features_supported(bits, lf));
}
/* We can't add random even features. */
for (size_t i = 0; i < 16; i += 2) {
bits = tal_dup_arr(tmpctx, u8, lf, tal_len(lf), 0);
bits = tal_dup_arr(tmpctx, u8, lf, tal_count(lf), 0);
set_bit(&bits, i);
/* Special case for missing compulsory feature */
@@ -93,7 +93,7 @@ int main(void)
ARRAY_SIZE(local_features)));
}
bits = tal_dup_arr(tmpctx, u8, gf, tal_len(gf), 0);
bits = tal_dup_arr(tmpctx, u8, gf, tal_count(gf), 0);
set_bit(&bits, i);
assert(features_supported(bits, lf)
== feature_supported(i, global_features,

View File

@@ -148,12 +148,12 @@ static void run_unit_tests(void)
printf("input_packet %s\n", tal_hex(tmpctx, reply));
reply = wrap_onionreply(tmpctx, &ss[i], reply);
printf("obfuscated_packet %s\n", tal_hex(tmpctx, reply));
assert(memcmp(reply, intermediates[i], tal_len(reply)) == 0);
assert(memcmp(reply, intermediates[i], tal_count(reply)) == 0);
}
oreply = unwrap_onionreply(tmpctx, ss, 5, reply);
printf("unwrapped %s\n", tal_hex(tmpctx, oreply->msg));
assert(memcmp(raw, oreply->msg, tal_len(raw)) == 0);
assert(memcmp(raw, oreply->msg, tal_bytelen(raw)) == 0);
}
int main(int argc, char **argv)

View File

@@ -16,7 +16,7 @@ char *tal_hexstr(const tal_t *ctx, const void *data, size_t len)
char *tal_hex(const tal_t *ctx, const tal_t *data)
{
return tal_hexstr(ctx, data, tal_len(data));
return tal_hexstr(ctx, data, tal_bytelen(data));
}
u8 *tal_hexdata(const tal_t *ctx, const void *str, size_t len)

View File

@@ -70,11 +70,11 @@ char *sanitize_error(const tal_t *ctx, const u8 *errmsg,
* through 126, inclusive):
* - SHOULD NOT print out `data` verbatim.
*/
for (i = 0; i < tal_len(data); i++) {
for (i = 0; i < tal_count(data); i++) {
if (data[i] < 32 || data[i] > 127) {
/* Convert to hex, minus NUL term */
data = (u8 *)tal_hex(ctx, data);
tal_resize(&data, tal_len(data)-1);
tal_resize(&data, strlen((const char *)data));
break;
}
}
@@ -83,5 +83,5 @@ char *sanitize_error(const tal_t *ctx, const u8 *errmsg,
channel_id_is_all(channel_id)
? "ALL"
: type_to_string(ctx, struct channel_id, channel_id),
(int)tal_len(data), (char *)data);
(int)tal_count(data), (char *)data);
}

View File

@@ -307,9 +307,9 @@ bool wireaddr_from_hostname(struct wireaddr *addr, const char *hostname,
if (strends(hostname, ".onion")) {
u8 *dec = b32_decode(tmpctx, hostname,
strlen(hostname) - strlen(".onion"));
if (tal_len(dec) == TOR_V2_ADDRLEN)
if (tal_count(dec) == TOR_V2_ADDRLEN)
addr->type = ADDR_TYPE_TOR_V2;
else if (tal_len(dec) == TOR_V3_ADDRLEN)
else if (tal_count(dec) == TOR_V3_ADDRLEN)
addr->type = ADDR_TYPE_TOR_V3;
else {
if (err_msg)
@@ -317,9 +317,9 @@ bool wireaddr_from_hostname(struct wireaddr *addr, const char *hostname,
return false;
}
addr->addrlen = tal_len(dec);
addr->addrlen = tal_count(dec);
addr->port = port;
memcpy(&addr->addr, dec, tal_len(dec));
memcpy(&addr->addr, dec, tal_count(dec));
return true;
}
@@ -344,7 +344,7 @@ bool wireaddr_from_hostname(struct wireaddr *addr, const char *hostname,
return false;
}
if (broken_reply != NULL && memeq(addrinfo->ai_addr, addrinfo->ai_addrlen, broken_reply, tal_len(broken_reply))) {
if (broken_reply != NULL && memeq(addrinfo->ai_addr, addrinfo->ai_addrlen, broken_reply, tal_count(broken_reply))) {
res = false;
goto cleanup;
}