void ovs_strlcpy(char *dst, const char *src, size_t size);
void ovs_strzcpy(char *dst, const char *src, size_t size);
+/* The C standards say that neither the 'dst' nor 'src' argument to
+ * memcpy() may be null, even if 'n' is zero. This wrapper tolerates
+ * the null case. */
+static inline void
+nullable_memcpy(void *dst, const void *src, size_t n)
+{
+ if (n) {
+ memcpy(dst, src, n);
+ }
+}
+
+/* The C standards say that the 'dst' argument to memset may not be
+ * null, even if 'n' is zero. This wrapper tolerates the null case. */
+static inline void
+nullable_memset(void *dst, int c, size_t n)
+{
+ if (n) {
+ memset(dst, c, n);
+ }
+}
+
/* Copy string SRC to DST, but no more bytes than the shorter of DST or SRC.
* DST and SRC must both be char arrays, not pointers, and with GNU C, this
* raises a compiler error if either DST or SRC is a pointer instead of an
nbrec_logical_switch_verify_ports(ls);
struct nbrec_logical_switch_port **new_ports = xmalloc(sizeof *new_ports *
(ls->n_ports + 1));
- memcpy(new_ports, ls->ports, sizeof *new_ports * ls->n_ports);
+ nullable_memcpy(new_ports, ls->ports, sizeof *new_ports * ls->n_ports);
new_ports[ls->n_ports] = CONST_CAST(struct nbrec_logical_switch_port *,
lsp);
nbrec_logical_switch_set_ports(ls, new_ports, ls->n_ports + 1);
/* Insert the acl into the logical switch. */
nbrec_logical_switch_verify_acls(ls);
struct nbrec_acl **new_acls = xmalloc(sizeof *new_acls * (ls->n_acls + 1));
- memcpy(new_acls, ls->acls, sizeof *new_acls * ls->n_acls);
+ nullable_memcpy(new_acls, ls->acls, sizeof *new_acls * ls->n_acls);
new_acls[ls->n_acls] = acl;
nbrec_logical_switch_set_acls(ls, new_acls, ls->n_acls + 1);
free(new_acls);
struct nbrec_load_balancer **new_lbs
= xmalloc(sizeof *new_lbs * (lr->n_load_balancer + 1));
- memcpy(new_lbs, lr->load_balancer, sizeof *new_lbs * lr->n_load_balancer);
+ nullable_memcpy(new_lbs, lr->load_balancer,
+ sizeof *new_lbs * lr->n_load_balancer);
new_lbs[lr->n_load_balancer] = CONST_CAST(struct nbrec_load_balancer *,
new_lb);
nbrec_logical_router_set_load_balancer(lr, new_lbs,
struct nbrec_load_balancer **new_lbs
= xmalloc(sizeof *new_lbs * (ls->n_load_balancer + 1));
- memcpy(new_lbs, ls->load_balancer, sizeof *new_lbs * ls->n_load_balancer);
+ nullable_memcpy(new_lbs, ls->load_balancer,
+ sizeof *new_lbs * ls->n_load_balancer);
new_lbs[ls->n_load_balancer] = CONST_CAST(struct nbrec_load_balancer *,
new_lb);
nbrec_logical_switch_set_load_balancer(ls, new_lbs,
nbrec_logical_router_verify_static_routes(lr);
struct nbrec_logical_router_static_route **new_routes
= xmalloc(sizeof *new_routes * (lr->n_static_routes + 1));
- memcpy(new_routes, lr->static_routes,
- sizeof *new_routes * lr->n_static_routes);
+ nullable_memcpy(new_routes, lr->static_routes,
+ sizeof *new_routes * lr->n_static_routes);
new_routes[lr->n_static_routes] = route;
nbrec_logical_router_set_static_routes(lr, new_routes,
lr->n_static_routes + 1);
/* Insert the NAT into the logical router. */
nbrec_logical_router_verify_nat(lr);
struct nbrec_nat **new_nats = xmalloc(sizeof *new_nats * (lr->n_nat + 1));
- memcpy(new_nats, lr->nat, sizeof *new_nats * lr->n_nat);
+ nullable_memcpy(new_nats, lr->nat, sizeof *new_nats * lr->n_nat);
new_nats[lr->n_nat] = nat;
nbrec_logical_router_set_nat(lr, new_nats, lr->n_nat + 1);
free(new_nats);
nbrec_logical_router_verify_ports(lr);
struct nbrec_logical_router_port **new_ports = xmalloc(sizeof *new_ports *
(lr->n_ports + 1));
- memcpy(new_ports, lr->ports, sizeof *new_ports * lr->n_ports);
+ nullable_memcpy(new_ports, lr->ports, sizeof *new_ports * lr->n_ports);
new_ports[lr->n_ports] = CONST_CAST(struct nbrec_logical_router_port *,
lrp);
nbrec_logical_router_set_ports(lr, new_ports, lr->n_ports + 1);