Skip to content

stun: drop the redundant address copy after stun_atoaddr()#336

Open
garacil wants to merge 1 commit into
freeswitch:masterfrom
garacil:fix-335-stun-redundant-copy
Open

stun: drop the redundant address copy after stun_atoaddr()#336
garacil wants to merge 1 commit into
freeswitch:masterfrom
garacil:fix-335-stun-redundant-copy

Conversation

@garacil

@garacil garacil commented Jul 6, 2026

Copy link
Copy Markdown

Fixes #335.

Both stun_test_lifetime() and stun_get_nattype(), on the branch where a server address is supplied, copied from &sd->sd_pri_info.ai_addr right after stun_atoaddr(). That is the address of the ai_addr pointer field inside the su_addrinfo_t, not the sockaddr it points to, so the copy overwrote the just-resolved server address in sd_pri_addr with addrinfo tail bytes (and overlapped, since sd_pri_info precedes sd_pri_addr in the struct).

The copy is also unnecessary: stun_discovery_create() sets sd->sd_pri_info.ai_addr = &sd->sd_pri_addr->su_sa (stun.c:1148) and stun_atoaddr() writes the resolved address through that pointer (stun.c:2623), so sd_pri_addr already holds the address when stun_atoaddr() returns.

This removes the redundant copy in both functions. The no-server branch's copy from sh_pri_addr (a different object) is a genuine copy and is left unchanged. GCC 14 at -O2 also stops warning at the memcpy site (-Wrestrict, overlap).

No ABI or behavior change other than preserving the resolved address instead of clobbering it.

stun_discovery_create() points sd_pri_info.ai_addr at sd_pri_addr, and
stun_atoaddr() writes the resolved server address through info->ai_addr, so
sd_pri_addr already holds the address when stun_atoaddr() returns. The
follow-up copy read from &sd_pri_info.ai_addr - the address of the pointer
field inside the su_addrinfo_t, not the pointed-to sockaddr - which overlaps
sd_pri_addr and copied addrinfo tail bytes over the just-resolved address.
Remove that copy in both stun_test_lifetime() and stun_get_nattype(); the
no-server copy from sh_pri_addr is a separate valid copy and is unchanged.
This also removes the -Wrestrict warning the overlapping copy produced.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

STUN: address-of-pointer copy after stun_atoaddr() overwrites the resolved server address

1 participant