Fix possible UB in mbedtls_asn1_write_raw_buffer()
This is mostly unrelated to other commits in this PR, except for the
fact that one of the added X.509 tests revealed that with UBSan.
Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
diff --git a/ChangeLog.d/fix-asn1write-raw-buffer.txt b/ChangeLog.d/fix-asn1write-raw-buffer.txt
new file mode 100644
index 0000000..292631a
--- /dev/null
+++ b/ChangeLog.d/fix-asn1write-raw-buffer.txt
@@ -0,0 +1,5 @@
+Bugfix
+ * When calling mbedtls_asn1_write_raw_buffer() with NULL, 0 as the last two
+ arguments, undefined behaviour would be triggered, in the form of a call to
+ memcpy(..., NULL, 0). This was harmless in practice, but could trigger
+ complains from sanitizers or static analyzers.
diff --git a/library/asn1write.c b/library/asn1write.c
index 415357b..97f9db0 100644
--- a/library/asn1write.c
+++ b/library/asn1write.c
@@ -90,7 +90,9 @@
len = size;
(*p) -= len;
- memcpy(*p, buf, len);
+ if (len != 0) {
+ memcpy(*p, buf, len);
+ }
return (int) len;
}