microsoft / go-crypto-winnative

Go crypto backend for Windows using CNG
MIT License
28 stars 3 forks source link

Free cloned SHA context when it is garbage collected #30

Closed qmuntal closed 2 years ago

qmuntal commented 2 years ago

This PR fixes a memory leak on shaXHash.Clone(). The returned instance owns a bcrypt.HASH_HANDLE allocated using bcrypt.DuplicateHash. The logic to free the context memory resides in shaXHash.finalize, which should be called automatically when the sha instance is garbage collected.

The problem is that Go doesn't know it has to call shaXHash.finalize, we have to instruct it by calling runtime.SetFinalizer(h, (*shaXHash).finalize) just after instantiating the sha. And we are not doing that.