--- a/sys/netgraph/iface/ng_iface.c +++ b/sys/netgraph/iface/ng_iface.c @@ -287,11 +287,21 @@ M_NETGRAPH, M_NOWAIT); if (newarray == NULL) return (ENOMEM); - bcopy(ng_iface_units, newarray, - ng_iface_units_len * sizeof(*ng_iface_units)); + + /* + * Never pass a NULL source to bcopy(): sys/sys/systm.h + * maps bcopy() onto __builtin_memmove(), and GCC then + * assumes the source pointer cannot be NULL (even for + * length 0) and optimizes away later NULL tests on it, + * which turned the kfree() NULL guard below into dead + * code (first call has ng_iface_units == NULL). + */ + if (ng_iface_units != NULL) { + bcopy(ng_iface_units, newarray, + ng_iface_units_len * sizeof(*ng_iface_units)); + kfree(ng_iface_units, M_NETGRAPH); + } for (i = ng_iface_units_len; i < newlen; i++) newarray[i] = ~0; - if (ng_iface_units != NULL) - kfree(ng_iface_units, M_NETGRAPH); ng_iface_units = newarray; ng_iface_units_len = newlen;