util/SecureUtil.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #pragma once | ||
| 2 | |||
| 3 | #include <QByteArray> | ||
| 4 | |||
| 5 | #include <cstddef> | ||
| 6 | |||
| 7 | // SEC-22: Secure memory handling utilities. | ||
| 8 | // Defense-in-depth: zeroing is best-effort because Qt may have internal copies. | ||
| 9 | namespace SecureUtil { | ||
| 10 | |||
| 11 | // Overwrite `size` bytes at `data` with zeros in a way the optimizer cannot | ||
| 12 | // remove (explicit_bzero, or a volatile write loop). | ||
| 13 | 1893 | inline void wipeBytes(void *data, std::size_t size) { | |
| 14 |
3/4✓ Branch 2 → 3 taken 1892 times.
✓ Branch 2 → 4 taken 1 time.
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 1892 times.
|
1893 | if (!data || size == 0) |
| 15 | 1 | return; | |
| 16 | #if defined(Q_OS_LINUX) || defined(Q_OS_MACOS) || defined(Q_OS_UNIX) | ||
| 17 | 1892 | explicit_bzero(data, size); | |
| 18 | #else | ||
| 19 | volatile char *p = static_cast<char *>(data); | ||
| 20 | for (std::size_t i = 0; i < size; ++i) | ||
| 21 | p[i] = 0; | ||
| 22 | #endif | ||
| 23 | } | ||
| 24 | |||
| 25 | // Overwrite QByteArray buffer with zeros, then clear it. | ||
| 26 | 8079 | inline void zeroMemory(QByteArray &data) { | |
| 27 |
2/2✓ Branch 3 → 4 taken 1891 times.
✓ Branch 3 → 9 taken 6188 times.
|
8079 | if (!data.isEmpty()) { |
| 28 | // Force detach so we own the buffer before zeroing | ||
| 29 | 1891 | data.detach(); | |
| 30 | 1891 | wipeBytes(data.data(), static_cast<std::size_t>(data.size())); | |
| 31 | 1891 | data.clear(); | |
| 32 | } | ||
| 33 | 8079 | } | |
| 34 | |||
| 35 | } // namespace SecureUtil | ||
| 36 |