[DPDK/core Bug 1506] Fix undefined behavior in RTE_IPV4 macro

bugzilla at dpdk.org bugzilla at dpdk.org
Tue Jul 30 10:11:09 CEST 2024


https://bugs.dpdk.org/show_bug.cgi?id=1506

            Bug ID: 1506
           Summary: Fix undefined behavior in RTE_IPV4 macro
           Product: DPDK
           Version: 24.07
          Hardware: All
                OS: All
            Status: UNCONFIRMED
          Severity: normal
          Priority: Normal
         Component: core
          Assignee: dev at dpdk.org
          Reporter: user202729 at protonmail.com
  Target Milestone: ---

Currently we have the following definition

```
/** Create IPv4 address */
#define RTE_IPV4(a, b, c, d) ((uint32_t)(((a) & 0xff) << 24) | \
                                           (((b) & 0xff) << 16) | \
                                           (((c) & 0xff) << 8)  | \
                                           ((d) & 0xff))
```

The problem is that the cast to `uint32_t` is done after the shift, and
overflowing left shift is undefined behavior in C and C++:
https://stackoverflow.com/questions/3784996/why-does-left-shift-operation-invoke-undefined-behaviour-when-the-left-side-oper
.

The fix is easy, just cast the `(a) & 0xff` to `uint32_t` before shifting it by
24.

-- 
You are receiving this mail because:
You are the assignee for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mails.dpdk.org/archives/dev/attachments/20240730/f019e14f/attachment-0001.htm>


More information about the dev mailing list