]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
isc_buffer_*(): if source can be NULL, only call memmove() when length is non-zero
authorMichał Kępień <michal@isc.org>
Tue, 15 May 2018 06:18:01 +0000 (08:18 +0200)
committerMichał Kępień <michal@isc.org>
Tue, 15 May 2018 07:32:19 +0000 (09:32 +0200)
Certain isc_buffer_*() functions might call memmove() with the second
argument (source) set to NULL and the third argument (length) set to 0.
While harmless, it triggers an ubsan warning:

    runtime error: null pointer passed as argument 2, which is declared to never be null

Modify all memmove() call sites in lib/isc/include/isc/buffer.h and
lib/isc/buffer.c which may potentially use NULL as the second argument
(source) so that memmove() is only called if the third argument (length)
is non-zero.

(cherry picked from commit 6ddbca6f2bc94b7e45958dab5576b3d9e48a6e5e)

lib/isc/buffer.c
lib/isc/include/isc/buffer.h
lib/isc/netaddr.c

index 05ae2b5443e211aa2d269cf9a9fda7decfc2185c..eec08443fb668d68177eb77b231dd4229eb11b37 100644 (file)
@@ -57,7 +57,10 @@ isc_buffer_reinit(isc_buffer_t *b, void *base, unsigned int length) {
        REQUIRE(base != NULL);
        REQUIRE(!b->autore);
 
-       (void)memmove(base, b->base, b->length);
+       if (b->length > 0U) {
+               (void)memmove(base, b->base, b->length);
+       }
+
        b->base = base;
        b->length = length;
 }
@@ -251,7 +254,9 @@ isc_buffer_compact(isc_buffer_t *b) {
 
        src = isc_buffer_current(b);
        length = isc_buffer_remaininglength(b);
-       (void)memmove(b->base, src, (size_t)length);
+       if (length > 0U) {
+               (void)memmove(b->base, src, (size_t)length);
+       }
 
        if (b->active > b->current)
                b->active -= b->current;
@@ -524,8 +529,10 @@ isc_buffer_copyregion(isc_buffer_t *b, const isc_region_t *r) {
        }
        if (r->length > available)
                return (ISC_R_NOSPACE);
-       memmove(base, r->base, r->length);
-       b->used += r->length;
+       if (r->length > 0U) {
+               memmove(base, r->base, r->length);
+               b->used += r->length;
+       }
 
        return (ISC_R_SUCCESS);
 }
index 8dcab73a5a8df9b637899c41b678ddb3e65c9a6b..0e0f7ff8dc8ddf53354182efc6716f82aa3d3db9 100644 (file)
@@ -889,8 +889,10 @@ ISC_LANG_ENDDECLS
                                == ISC_R_SUCCESS); \
                } \
                ISC_REQUIRE(isc_buffer_availablelength(_b) >= (unsigned int) _length); \
-               memmove(isc_buffer_used(_b), (_base), (_length)); \
-               (_b)->used += (_length); \
+               if (_length > 0U) { \
+                       memmove(isc_buffer_used(_b), (_base), (_length)); \
+                       (_b)->used += (_length); \
+               } \
        } while (0)
 
 #define ISC__BUFFER_PUTSTR(_b, _source) \
index 75775f8f571ef9418d9f96524f00d62a016786e8..5370993652b5e665381f46b86f493d674867d0a6 100644 (file)
@@ -166,7 +166,7 @@ isc_netaddr_totext(const isc_netaddr_t *netaddr, isc_buffer_t *target) {
                return (ISC_R_NOSPACE);
 
        isc_buffer_putmem(target, (unsigned char *)abuf, alen);
-       isc_buffer_putmem(target, (unsigned char *)zbuf, zlen);
+       isc_buffer_putmem(target, (unsigned char *)zbuf, (unsigned int)zlen);
 
        return (ISC_R_SUCCESS);
 }