Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 6 additions & 0 deletions include/picotls.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,17 @@ extern "C" {
#if __GNUC__ >= 3
#define PTLS_LIKELY(x) __builtin_expect(!!(x), 1)
#define PTLS_UNLIKELY(x) __builtin_expect(!!(x), 0)
#define PTLS_BUILD_ASSERT(cond) ((void)sizeof(char[2 * !!(!__builtin_constant_p(cond) || (cond)) - 1]))
#define PTLS_ASSERT_IS_ARRAY(a) PTLS_BUILD_ASSERT(__builtin_types_compatible_p(__typeof__(a[0])[], __typeof__(a)))
#else
#define PTLS_LIKELY(x) (x)
#define PTLS_UNLIKELY(x) (x)
#define PTLS_BUILD_ASSERT(cond) 1
#define PTLS_ASSERT_IS_ARRAY(a) 1
#endif

#define PTLS_ELEMENTSOF(x) (PTLS_ASSERT_IS_ARRAY(x), sizeof(x) / sizeof((x)[0]))

#ifdef _WINDOWS
#define PTLS_THREADLOCAL __declspec(thread)
#else
Expand Down
2 changes: 1 addition & 1 deletion lib/openssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -1038,7 +1038,7 @@ int ptls_openssl_init_sign_certificate(ptls_openssl_sign_certificate_t *self, EV
return PTLS_ERROR_INCOMPATIBLE_KEY;
}
PUSH_SCHEME(UINT16_MAX, NULL);
assert(scheme_index <= sizeof(self->schemes) / sizeof(self->schemes[0]));
assert(scheme_index <= PTLS_ELEMENTSOF(self->schemes));

#undef PUSH_SCHEME

Expand Down
19 changes: 9 additions & 10 deletions lib/picotls.c
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ static ptls_aead_context_t *new_aead(ptls_aead_algorithm_t *aead, ptls_hash_algo
static int is_supported_version(uint16_t v)
{
size_t i;
for (i = 0; i != sizeof(supported_versions) / sizeof(supported_versions[0]); ++i)
for (i = 0; i != PTLS_ELEMENTSOF(supported_versions); ++i)
if (supported_versions[i] == v)
return 1;
return 0;
Expand Down Expand Up @@ -1518,7 +1518,7 @@ static int decode_signature_algorithms(struct st_ptls_signature_algorithms_t *sa
uint16_t id;
if ((ret = ptls_decode16(&id, src, end)) != 0)
goto Exit;
if (sa->count < sizeof(sa->list) / sizeof(sa->list[0]))
if (sa->count < PTLS_ELEMENTSOF(sa->list))
sa->list[sa->count++] = id;
} while (*src != end);
});
Expand Down Expand Up @@ -2028,7 +2028,7 @@ static int send_client_hello(ptls_t *tls, ptls_message_emitter_t *emitter, ptls_
buffer_push_extension(sendbuf, PTLS_EXTENSION_TYPE_SUPPORTED_VERSIONS, {
ptls_buffer_push_block(sendbuf, 1, {
size_t i;
for (i = 0; i != sizeof(supported_versions) / sizeof(supported_versions[0]); ++i)
for (i = 0; i != PTLS_ELEMENTSOF(supported_versions); ++i)
ptls_buffer_push16(sendbuf, supported_versions[i]);
});
});
Expand Down Expand Up @@ -2676,7 +2676,7 @@ static int handle_certificate(ptls_t *tls, const uint8_t *src, const uint8_t *en
ptls_decode_block(src, end, 3, {
while (src != end) {
ptls_decode_open_block(src, end, 3, {
if (num_certs < sizeof(certs) / sizeof(certs[0]))
if (num_certs < PTLS_ELEMENTSOF(certs))
certs[num_certs++] = ptls_iovec_init(src, end - src);
src = end;
});
Expand Down Expand Up @@ -3249,7 +3249,7 @@ static int decode_client_hello(ptls_t *tls, struct st_ptls_client_hello_t *ch, c
ret = PTLS_ALERT_DECODE_ERROR;
goto Exit;
}
if (ch->alpn.count < sizeof(ch->alpn.list) / sizeof(ch->alpn.list[0]))
if (ch->alpn.count < PTLS_ELEMENTSOF(ch->alpn.list))
ch->alpn.list[ch->alpn.count++] = ptls_iovec_init(src, end - src);
src = end;
});
Expand All @@ -3262,8 +3262,7 @@ static int decode_client_hello(ptls_t *tls, struct st_ptls_client_hello_t *ch, c
uint16_t id;
if ((ret = ptls_decode16(&id, &src, end)) != 0)
goto Exit;
if (ch->cert_compression_algos.count <
sizeof(ch->cert_compression_algos.list) / sizeof(ch->cert_compression_algos.list[0]))
if (ch->cert_compression_algos.count < PTLS_ELEMENTSOF(ch->cert_compression_algos.list))
ch->cert_compression_algos.list[ch->cert_compression_algos.count++] = id;
} while (src != end);
});
Expand All @@ -3280,7 +3279,7 @@ static int decode_client_hello(ptls_t *tls, struct st_ptls_client_hello_t *ch, c
break;
case PTLS_EXTENSION_TYPE_SUPPORTED_VERSIONS:
ptls_decode_block(src, end, 1, {
size_t selected_index = sizeof(supported_versions) / sizeof(supported_versions[0]);
size_t selected_index = PTLS_ELEMENTSOF(supported_versions);
do {
size_t i;
uint16_t v;
Expand All @@ -3293,7 +3292,7 @@ static int decode_client_hello(ptls_t *tls, struct st_ptls_client_hello_t *ch, c
}
}
} while (src != end);
if (selected_index != sizeof(supported_versions) / sizeof(supported_versions[0]))
if (selected_index != PTLS_ELEMENTSOF(supported_versions))
ch->selected_version = supported_versions[selected_index];
});
break;
Expand Down Expand Up @@ -3344,7 +3343,7 @@ static int decode_client_hello(ptls_t *tls, struct st_ptls_client_hello_t *ch, c
});
if ((ret = ptls_decode32(&psk.obfuscated_ticket_age, &src, end)) != 0)
goto Exit;
if (ch->psk.identities.count < sizeof(ch->psk.identities.list) / sizeof(ch->psk.identities.list[0]))
if (ch->psk.identities.count < PTLS_ELEMENTSOF(ch->psk.identities.list))
ch->psk.identities.list[ch->psk.identities.count++] = psk;
++num_identities;
} while (src != end);
Expand Down
2 changes: 1 addition & 1 deletion t/picotls.c
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@ static void test_handshake(ptls_iovec_t ticket, int mode, int expect_ticket, int
ctx_peer->on_client_hello = &cb;
static const ptls_iovec_t protocols[] = {{(uint8_t *)"h2", 2}, {(uint8_t *)"http/1.1", 8}};
client_hs_prop.client.negotiated_protocols.list = protocols;
client_hs_prop.client.negotiated_protocols.count = sizeof(protocols) / sizeof(protocols[0]);
client_hs_prop.client.negotiated_protocols.count = PTLS_ELEMENTSOF(protocols);
ptls_set_server_name(client, "test.example.com", 0);
}

Expand Down