mirror of
https://github.com/Mbed-TLS/mbedtls.git
synced 2025-04-16 08:42:50 +00:00
Simplify zero-length buffers to always be NULL
Since it is implementation-dependent whether malloc(0) returns NULL or a pointer, explicitly represent zero-length buffers as NULL in the buffer-copy struct, so as to have a uniform behaviour. Signed-off-by: David Horstmann <david.horstmann@arm.com>
This commit is contained in:
parent
03b0472413
commit
0fee689e57
@ -8473,6 +8473,16 @@ psa_status_t psa_crypto_alloc_and_copy(const uint8_t *input, size_t input_len,
|
|||||||
* on any pointers safely. */
|
* on any pointers safely. */
|
||||||
memset(buffers, 0, sizeof(*buffers));
|
memset(buffers, 0, sizeof(*buffers));
|
||||||
|
|
||||||
|
/* Since calloc() may return NULL if we try to allocate zero-length
|
||||||
|
* buffers anyway, deal with this corner case explicitly to ensure
|
||||||
|
* predictable behaviour. Represent zero-length buffers as NULL. */
|
||||||
|
if (input_len == 0) {
|
||||||
|
input = NULL;
|
||||||
|
}
|
||||||
|
if (output_len == 0) {
|
||||||
|
output = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
if (output != NULL) {
|
if (output != NULL) {
|
||||||
buffers->output = mbedtls_calloc(output_len, 1);
|
buffers->output = mbedtls_calloc(output_len, 1);
|
||||||
if (buffers->output == NULL) {
|
if (buffers->output == NULL) {
|
||||||
|
@ -10430,8 +10430,16 @@ void psa_crypto_alloc_and_copy_zero_length(int input_zero_length,
|
|||||||
&buffer_copies);
|
&buffer_copies);
|
||||||
TEST_EQUAL(ret, PSA_SUCCESS);
|
TEST_EQUAL(ret, PSA_SUCCESS);
|
||||||
|
|
||||||
TEST_MEMORY_COMPARE(input_buffer, input_len, buffer_copies.input, buffer_copies.input_len);
|
if (input_zero_length) {
|
||||||
TEST_EQUAL(output_len, buffer_copies.output_len);
|
TEST_ASSERT(buffer_copies.input == NULL);
|
||||||
|
} else {
|
||||||
|
TEST_MEMORY_COMPARE(input_buffer, input_len, buffer_copies.input, buffer_copies.input_len);
|
||||||
|
}
|
||||||
|
if (output_zero_length) {
|
||||||
|
TEST_ASSERT(buffer_copies.output == NULL);
|
||||||
|
} else {
|
||||||
|
TEST_EQUAL(output_len, buffer_copies.output_len);
|
||||||
|
}
|
||||||
|
|
||||||
exit:
|
exit:
|
||||||
mbedtls_free(buffer_copies.input);
|
mbedtls_free(buffer_copies.input);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user