lib:talloc: Fix undefined behavior in talloc_memdup
authorAndreas Schneider <asn@samba.org>
Thu, 22 Nov 2018 15:10:39 +0000 (16:10 +0100)
committerAndreas Schneider <asn@cryptomilk.org>
Wed, 12 Dec 2018 17:34:10 +0000 (18:34 +0100)
lib/talloc/talloc.c:2419: runtime error: null pointer passed as argument
2, which is declared to never be null

Signed-off-by: Andreas Schneider <asn@samba.org>
Reviewed-by: Volker Lendecke <vl@samba.org>
lib/talloc/talloc.c

index 54be63495aec35d9c05cdb05663bd84c41836099..073a3e50d4b327d0c444e248e8ed7f325badaf7c 100644 (file)
@@ -2413,9 +2413,14 @@ _PUBLIC_ void *_talloc_zero(const void *ctx, size_t size, const char *name)
 */
 _PUBLIC_ void *_talloc_memdup(const void *t, const void *p, size_t size, const char *name)
 {
-       void *newp = _talloc_named_const(t, size, name);
+       void *newp = NULL;
 
-       if (likely(newp)) {
+       if (likely(size > 0) && unlikely(p == NULL)) {
+               return NULL;
+       }
+
+       newp = _talloc_named_const(t, size, name);
+       if (likely(newp != NULL) && likely(size > 0)) {
                memcpy(newp, p, size);
        }