CKey::Reset() memory leak #1474

issue xanatos opened this issue on June 16, 2012
  1. xanatos commented at 3:42 PM on June 16, 2012: none

    (from key.cpp)

    void CKey::Reset()
    {
        fCompressedPubKey = false;
        pkey = EC_KEY_new_by_curve_name(NID_secp256k1);
        if (pkey == NULL)
            throw key_error("CKey::CKey() : EC_KEY_new_by_curve_name failed");
        fSet = false;
    }
    

    Clearly if before calling Reset() with pkey != null then you have a memory leak (the pkey value is overwritten). The problem is that, for example, CBasicKeyStore::GetKey() calls Reset() on an already instantiated CKey &keyOut.

    There should be a EC_KEY_free(pkey); like in CKey::SetSecret(), and in CKey::CKey() before calling Reset() pkey shoud be initialized to NULL.

  2. sipa commented at 8:11 PM on June 16, 2012: member

    Nice catch.

  3. Diapolo commented at 8:41 PM on June 20, 2012: none

    Seems no one created a patch yet ...

  4. sipa commented at 8:47 PM on June 20, 2012: member

    Knock yourself out.

  5. Diapolo commented at 10:11 AM on June 21, 2012: none

    I'm going to do this :). Currently preparing the pull.

  6. gavinandresen closed this on Jul 5, 2012

  7. suprnurd referenced this in commit 28a1d0ecc1 on Dec 5, 2017
  8. lateminer referenced this in commit da4e5003e6 on Jan 22, 2019
  9. lateminer referenced this in commit 840821321c on May 6, 2020
  10. DrahtBot locked this on Sep 8, 2021

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-04-21 21:16 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me