You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[BUG] SocketAddr performs misaligned sockaddr_in access through reinterpret_cast #4307
Status, re-checked 19 October 2026 against main at cdd17eb1. One of the two defects below was fixed by #4500, merged 31 August, and the original title named a signature that no longer exists. Retitled to the half that is still live. The text below is the report as filed.
state on cdd17eb1
misaligned sockaddr_in access
still there. socket_tools.h:180 is sockaddr_in &inet4 = reinterpret_cast<sockaddr_in &>(m_data);.
silent port truncation
fixed. The signature is SocketAddr(u_long addr, uint16_t port) and line 182 is htons(port) with no cast, so the three wrap values tabulated below cannot be passed any more.
Description
SocketTools::SocketAddr(u_long addr, int port) in ext/include/opentelemetry/ext/http/server/socket_tools.h has two defects:
1. Alignment / type-access UB. It binds a sockaddr_in & to the sockaddr m_data storage through a reinterpret_cast:
sockaddr and sockaddr_in do not share alignment on all platforms (on Linux alignof(sockaddr) == 2 but alignof(sockaddr_in) == 4), so accessing the storage through a sockaddr_in glvalue is an alignment / strict-aliasing issue that UBSan reports.
2. Silent port truncation.port is an int cast to uint16_t with no range check:
This constructor is on the live server path: it has an addListeningPort caller (unlike the SocketAddr(char const *) overload, which has no in-repo caller). The string-parsing overload was made safe in #4292 (it memcpys a local sockaddr_in in and out of the storage and parses a strict decimal port); this integer constructor still uses the old reinterpret_cast access and the unchecked port cast.
Suggested fix
Mirror #4292's approach: build a local sockaddr_in, memcpy it into m_data, and validate or clamp the port range (or document the caller's contract that the port is already in 0..65535).
Context
Split out of the ext/http correctness audit (#4287). #4292 deliberately scoped this out to keep that PR limited to the string parser.
The port truncation half of this overlaps #4305, which #4320 is fixing. Nothing links the two issues, so this one still reads as though both defects are open.
Once #4320 lands, what remains here is the first defect: the reinterpret_cast<sockaddr_in &> onto the sockaddr storage.
#4292 converted the string constructor to build a local sockaddr_in and memcpy it in and out, and that cast is now the only one of its kind left in the file, so the same shape fits here.
Leaving this open for anyone who wants to take it.
Hi @thc1006, is this still open for contribution or is anyone currently working on the remaining reinterpret_cast fix? I'd like to pick it up if available.
#4320 was closed on 31 August without being merged, so the route I described in August did not happen. #4305 itself is closed, but the first defect here is still on main: reinterpret_cast<sockaddr_in &>(m_data) at socket_tools.h:180. Nobody is assigned.
@noob339 said in August that they would look at this once #4320 landed, so please give them a little time to say whether they still want it. If not, it is yours.
The shape I would suggest is the one #4292 used for the string constructor: build a local sockaddr_in, memcpy it into the storage, and memcpy back out to read it, rather than casting the storage in place. That way the alignment question does not arise at all.
A few specifics that should make it shorter than it looks. The only site left is the SocketAddr(u_long, uint16_t) constructor. The read side was already converted: port() copies into a local sockaddr_in with memcpy and carries a comment pointing at this issue, and SocketAddr(char const *) does the same thing on the way in. Either one works as a template. The other reinterpret_casts in that header are char * casts for setsockopt, recv and send, and none of them are related to this.
The port truncation in the title no longer applies. The constructor takes a uint16_t now, so that half resolved itself and only the cast is left.
changed the title [-][BUG] SocketAddr(u_long, int) performs misaligned sockaddr_in access and truncates out-of-range ports[/-][+][BUG] SocketAddr performs misaligned sockaddr_in access through reinterpret_cast[/+]on Oct 19, 2026
Activity
Clarify the overlong-host test and point the alignment note at open-t…
thc1006 commentedon Aug 1, 2026
The port truncation half of this overlaps #4305, which #4320 is fixing. Nothing links the two issues, so this one still reads as though both defects are open.
Once #4320 lands, what remains here is the first defect: the
reinterpret_cast<sockaddr_in &>onto thesockaddrstorage.#4292 converted the string constructor to build a local
sockaddr_inandmemcpyit in and out, and that cast is now the only one of its kind left in the file, so the same shape fits here.Leaving this open for anyone who wants to take it.
noob339 commentedon Aug 1, 2026
I can look into it once #4320 lands
thc1006 commentedon Aug 1, 2026
Thanks for picking it up. Landing #4320 first makes sense, the two changes touch the same few lines.
Shubhammehta2008 commentedon Aug 31, 2026
Hi @thc1006, is this still open for contribution or is anyone currently working on the remaining reinterpret_cast fix? I'd like to pick it up if available.
thc1006 commentedon Sep 8, 2026
Yes, it is free, and sorry for the slow answer.
#4320 was closed on 31 August without being merged, so the route I described in August did not happen. #4305 itself is closed, but the first defect here is still on
main:reinterpret_cast<sockaddr_in &>(m_data)atsocket_tools.h:180. Nobody is assigned.@noob339 said in August that they would look at this once #4320 landed, so please give them a little time to say whether they still want it. If not, it is yours.
The shape I would suggest is the one #4292 used for the string constructor: build a local
sockaddr_in,memcpyit into the storage, andmemcpyback out to read it, rather than casting the storage in place. That way the alignment question does not arise at all.Shubhammehta2008 commentedon Sep 12, 2026
Thanks @thc1006. Since it's been a few days, I'll go ahead and pick this up using the memcpy approach as suggested. I'll open a draft PR shortly.
thc1006 commentedon Sep 19, 2026
Still yours, nobody else has picked it up.
A few specifics that should make it shorter than it looks. The only site left is the
SocketAddr(u_long, uint16_t)constructor. The read side was already converted:port()copies into a localsockaddr_inwithmemcpyand carries a comment pointing at this issue, andSocketAddr(char const *)does the same thing on the way in. Either one works as a template. The otherreinterpret_casts in that header arechar *casts forsetsockopt,recvandsend, and none of them are related to this.The port truncation in the title no longer applies. The constructor takes a
uint16_tnow, so that half resolved itself and only the cast is left.Tests live in
ext/test/http/socket_tools_test.cc.No rush from my side.
[-][BUG] SocketAddr(u_long, int) performs misaligned sockaddr_in access and truncates out-of-range ports[/-][+][BUG] SocketAddr performs misaligned sockaddr_in access through reinterpret_cast[/+][BUG] Fix misaligned sockaddr_in access in SocketAddr
[BUG] Fix misaligned sockaddr_in access in SocketAddr
[BUG] Fix misaligned sockaddr_in access in SocketAddr