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>
diff --git a/library/psa_crypto.c b/library/psa_crypto.c
index fc5e241..7b4fc6c 100644
--- a/library/psa_crypto.c
+++ b/library/psa_crypto.c
@@ -8473,6 +8473,16 @@
* on any pointers safely. */
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) {
buffers->output = mbedtls_calloc(output_len, 1);
if (buffers->output == NULL) {