Skip to content

Fix native handle leaks when KEM and signature constructors fail - #165

Merged
dstebila merged 1 commit into
open-quantum-safe:mainfrom
rdupre1168:fix-constructor-cleanup
Dec 5, 2026
Merged

dstebila merged 1 commit into
open-quantum-safe:mainfrom
rdupre1168:fix-constructor-cleanup

Conversation

@rdupre1168

Copy link
Copy Markdown
Contributor

KeyEncapsulation and Signature allocate native liboqs structures before
validating supplied secret keys. If initialization raises, those structures
are not freed.

This change releases native resources when initialization fails and
re-raises the original exception, following the existing StatefulSignature
cleanup pattern.

Regression tests verify that an empty secret key raises ValueError and
calls the native free function exactly once.

Validation:

  • Both new regression tests fail against the original constructors.
  • Both pass with the fix.
  • All six focused constructor and cleanup tests pass.
  • git diff --check passes.

Test environment: macOS arm64, Python 3.13, liboqs 0.16.0.

Related: #160.

AI assistance: Used Codex for explanation and review, regression tests,
and local validation. I wrote the constructor changes.

Signed-off-by: rdupre1168 <60334854+rdupre1168@users.noreply.github.com>
@rdupre1168
rdupre1168 force-pushed the fix-constructor-cleanup branch from 59abf5b to 24e68b9 Compare December 5, 2026 06:40

@dstebila dstebila left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks very much for the contribution!

dstebila added a commit that referenced this pull request Dec 5, 2026
- Signature.__init__ never copied suf_cma from the native struct, so
  Signature.suf_cma and details["is_suf_cma"] were always False.
- KeyEncapsulation and Signature now raise RuntimeError naming the
  algorithm when OQS_KEM_new/OQS_SIG_new returns NULL, matching
  StatefulSignature, instead of a ValueError from the NULL dereference
  that callers could mistake for an invalid key length.
- Narrow the secret_key annotation on both constructors to
  Optional[bytes]; an int was advertised but always raised TypeError.

Follow-up to #165.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Douglas Stebila <dstebila@uwaterloo.ca>
dstebila added a commit that referenced this pull request Dec 5, 2026
- Signature.__init__ never copied suf_cma from the native struct, so
  Signature.suf_cma and details["is_suf_cma"] were always False.
- KeyEncapsulation and Signature now raise RuntimeError naming the
  algorithm when OQS_KEM_new/OQS_SIG_new returns NULL, matching
  StatefulSignature, instead of a ValueError from the NULL dereference
  that callers could mistake for an invalid key length.
- Narrow the secret_key annotation on both constructors to
  Optional[bytes]; an int was advertised but always raised TypeError.

Follow-up to #165.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Douglas Stebila <dstebila@uwaterloo.ca>
@dstebila
dstebila merged commit 1d168b5 into open-quantum-safe:main Dec 5, 2026
10 checks passed
dstebila added a commit that referenced this pull request Dec 5, 2026
- Signature.__init__ never copied suf_cma from the native struct, so
  Signature.suf_cma and details["is_suf_cma"] were always False.
- KeyEncapsulation and Signature now raise RuntimeError naming the
  algorithm when OQS_KEM_new/OQS_SIG_new returns NULL, matching
  StatefulSignature, instead of a ValueError from the NULL dereference
  that callers could mistake for an invalid key length.
- Narrow the secret_key annotation on both constructors to
  Optional[bytes]; an int was advertised but always raised TypeError.

Follow-up to #165.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Douglas Stebila <dstebila@uwaterloo.ca>
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.

2 participants