verityutils: fix a memory leak
`d` is never freed here. use a unique_ptr to ensure that happens in the
future.
caught by the static analyzer:
> system/security/ondevice-signing/VerityUtils.cpp:88:26: warning:
Potential leak of memory pointed to by 'd' [clang-analyzer-unix.Malloc]
Bug: None
Test: TreeHugger
Change-Id: I0158eae424a024046089d86fe3c1f38efbfa9a2b
diff --git a/ondevice-signing/VerityUtils.cpp b/ondevice-signing/VerityUtils.cpp
index 579d3d8..b4a6a54 100644
--- a/ondevice-signing/VerityUtils.cpp
+++ b/ondevice-signing/VerityUtils.cpp
@@ -76,9 +76,10 @@
static Result<std::vector<uint8_t>> signDigest(const KeymasterSigningKey& key,
const std::vector<uint8_t>& digest) {
- struct fsverity_signed_digest* d = NULL;
+ fsverity_signed_digest* d;
size_t signed_digest_size = sizeof(*d) + digest.size();
- d = (struct fsverity_signed_digest*)malloc(signed_digest_size);
+ std::unique_ptr<uint8_t[]> digest_buffer{new uint8_t[signed_digest_size]};
+ d = (fsverity_signed_digest*)digest_buffer.get();
memcpy(d->magic, "FSVerity", 8);
d->digest_algorithm = cpu_to_le16(FS_VERITY_HASH_ALG_SHA256);