Skip to content

fix: null-deref when dbgKey unset under HELIB_DEBUG (#501)#523

Open
BAder82t wants to merge 1 commit intohomenc:masterfrom
BAder82t:fix/debug-null-sk-501
Open

fix: null-deref when dbgKey unset under HELIB_DEBUG (#501)#523
BAder82t wants to merge 1 commit intohomenc:masterfrom
BAder82t:fix/debug-null-sk-501

Conversation

@BAder82t
Copy link
Copy Markdown

Summary

Fixes segfault reported in #501.

When HElib is compiled with `HELIB_DEBUG` and the user program does not call `setupDebugGlobals(&sk, ...)`, the global `dbgKey` / `dbgEa` remain `nullptr`. Several debug-only call sites dereference `*dbgKey` / `*dbgEa` unconditionally, causing a segfault. The reporter hit this running the bundled `BGV_binary_arithmetic` example:

```
SEGV at keys.cpp:273 PubKey::getContext(this=0x0)
<- SecKey::Decrypt(this=0x0, ...) keys.cpp:1338
<- embeddingLargestCoeff debugging.cpp:60
<- realToEstimatedNoise debugging.cpp:30
<- checkNoise debugging.cpp:50
<- Ctxt::multiplyBy Ctxt.cpp:1772
```

Fix

Guard every `*dbgKey` / `*dbgEa` dereference with an explicit null check, matching the existing pattern already used in `extractDigits.cpp` and `debugging.cpp`. Release builds (`HELIB_DEBUG` off) are unchanged.

Call sites touched:

  • `src/Ctxt.cpp` — `Ctxt::multiplyBy` (the reported site)
  • `src/binaryCompare.cpp` — both debug blocks in `compProducts` / `compEqGt`
  • `src/binaryArith.cpp` — `multByNegative`, `multTwoNumbers`, both blocks in `AddDAG::printAddDAG`, and `decryptAndSum`

Verification

Minimal BGV repro (single `Ctxt::multiplyBy`, no `setupDebugGlobals` call, built with `-DHELIB_DEBUG -fsanitize=address`):

Build Result
unpatched master `SEGV keys.cpp:273` (exit 134) — exact stack from #501
this PR `multiplyBy completed` — exit 0

Closes

#501

Several debug-only paths dereference *dbgKey / *dbgEa unconditionally, segfaulting when setupDebugGlobals() was never called (e.g. the bundled BGV_binary_arithmetic example under Ctxt::multiplyBy - > checkNoise)

Guard each debug block with a null check, matching the existing pattern in extractDigits.cpp and debugging.cpp

Closes homenc#501
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.

1 participant