Alejandro Pacheco via dev <[email protected]> writes:

> From: Alejandro Pacheco <[email protected]>
>
> The set-field and NAT setters in packets.c and odp-execute.c cast
> dp_packet_l3() or dp_packet_l4() to a header struct and rely on their
> callers to check that the packet holds the header.  Some callers do,
> some only assert that the offset is set, as set_arp() did.
>
> Add dp_packet_l3_hdr() and dp_packet_l4_hdr(), which return the header
> only when the offset is set and the packet holds the given number of
> bytes from it, and use them in the IPv4, IPv6, ARP, TCP, UDP, SCTP,
> ICMP and IGMP setters and in packet_get_ip_proto().  A packet that is
> too short is left unchanged.  packet_rh_present() is bounded by the L3
> size, and by the L4 offset when it is set.
>
> Assisted-by: claude-opus-5-5, Claude Code
> Signed-off-by: Alejandro Pacheco <[email protected]>
> ---
> v4: Initialize the IPv6 address in the new tests with braces, as
>     test_ipv6_static_masks() does, for the sparse error the robot
>     reported on v3: include/sparse has no IN6ADDR_LOOPBACK_INIT.
>
>  lib/dp-packet.h      |  31 ++++++++
>  lib/odp-execute.c    |  42 +++++++----
>  lib/packets.c        | 140 +++++++++++++++++++++++-----------
>  tests/test-packets.c | 175 +++++++++++++++++++++++++++++++++++++++++++
>  4 files changed, 332 insertions(+), 56 deletions(-)

The following is an AI LLM review of the patch.  A human has reviewed it
and found the report to be plausible.

> diff --git a/lib/dp-packet.h b/lib/dp-packet.h
> index e4dff1436..78f90d380 100644
> --- a/lib/dp-packet.h
> +++ b/lib/dp-packet.h
> @@ -507,6 +507,37 @@ dp_packet_l4_size(const struct dp_packet *b)
>          : 0;
>  }
>  
> +/* Returns the data at offset 'ofs' of 'b' if at least 'size' bytes follow
> + * it before the L2 padding, otherwise NULL.  'ofs' may be unset. */
> +static inline void *
> +dp_packet_hdr_at__(const struct dp_packet *b, uint16_t ofs, size_t size)
> +{
> +    size_t len = dp_packet_size(b);
> +    size_t pad = dp_packet_l2_pad_size(b);
> +    size_t end = pad <= len ? len - pad : 0;
> +
> +    return ofs != UINT16_MAX && ofs <= end && size <= end - ofs
> +           ? (char *) dp_packet_data(b) + ofs
> +           : NULL;

The bounds arithmetic is overflow-safe: `ofs <= end` guards the `end -
ofs` subtraction, and there is no unsigned wrap when pad >
len. Excluding L2 padding matches how dp_packet_l3_size()/l4_size()
compute their sizes (tail - ofs - pad), so `dp_packet_l4_hdr(b, N)` is
exactly equivalent to the old
`dp_packet_l4(b) && dp_packet_l4_size(b) >= N` idiom. The `pad <= len`
guard is redundant with dp_packet_set_l2_pad_size()'s assert but
harmless.

One nit, not a defect: with size == 0 and ofs == end the helper returns
a one-past-the-payload pointer rather than NULL. No caller passes size 0
(the new tests exercise it explicitly), so nothing depends on it; if a
future caller treats non-NULL as "header present" this is subtly wrong
for zero-size requests.
_______________________________________________
dev mailing list
[email protected]
https://mail.openvswitch.org/mailman/listinfo/ovs-dev

Reply via email to