fix use after free (#22854)

1. `freeAddrInfo` is called prematurely, the variable `myAddr` is still
in use
2. Use defer syntax to ensure that `freeAddrInfo` is also called on
exceptions
This commit is contained in:
握猫猫
2023-10-23 15:11:13 +08:00
committed by GitHub
parent ca577dbab1
commit 562a5fb8f9

View File

@@ -392,6 +392,8 @@ when not useNimNetLite:
myAddr: pointer
addrLen = 0
family = 0
defer: freeAddrInfo(addrInfo)
if addrInfo.ai_addr.sa_family.cint == nativeAfInet:
family = nativeAfInet
@@ -404,8 +406,6 @@ when not useNimNetLite:
else:
raise newException(IOError, "Unknown socket family in `getHostByAddr()`")
freeAddrInfo(addrInfo)
when useWinVersion:
var s = winlean.gethostbyaddr(cast[ptr InAddr](myAddr), addrLen.cuint,
cint(family))