Skip to content

Commit

Permalink
fix use after free (#22854)
Browse files Browse the repository at this point in the history
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

(cherry picked from commit 562a5fb)
  • Loading branch information
haoyu234 authored and narimiran committed Oct 24, 2023
1 parent e8e9948 commit 805b4e2
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/pure/nativesockets.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))
Expand Down

0 comments on commit 805b4e2

Please sign in to comment.