]> git.ipfire.org Git - thirdparty/krb5.git/commitdiff
Avoid undefined memcpy in asn1_encode.c
authorGreg Hudson <ghudson@mit.edu>
Wed, 11 Jun 2025 18:08:31 +0000 (14:08 -0400)
committerGreg Hudson <ghudson@mit.edu>
Wed, 16 Jul 2025 16:08:44 +0000 (12:08 -0400)
The C standard specifies that passing null pointers to most standard
library functions results in undefined behavior (C99 7.1.4).  This
applies to memcpy() even when the length is 0.  insert_bytes() in
asn1_encode.c may be called with a null pointer from an empty
krb5_data or other counted value in a structure to be encoded.  Do not
call memcpy() in this case.

Reported by Kirill Furman.

ticket: 9175

src/lib/krb5/asn.1/asn1_encode.c

index c4140021e5e041d01d9328fc2960f30fffb457da..651d213c49cc119b1d10cdc9c5ac7efb0733daa8 100644 (file)
@@ -49,7 +49,7 @@ insert_byte(asn1buf *buf, uint8_t o)
 static inline void
 insert_bytes(asn1buf *buf, const void *bytes, size_t len)
 {
-    if (buf->ptr != NULL) {
+    if (buf->ptr != NULL && len > 0) {
         memcpy(buf->ptr - len, bytes, len);
         buf->ptr -= len;
     }