From 3eda1a58a24336c4c1aabe953f84b32a03da62cb Mon Sep 17 00:00:00 2001 From: Eugene Siegel Date: Tue, 6 Aug 2024 11:49:23 -0400 Subject: [PATCH] blockchain: copy utxo status bytes to avoid UB It is undefined behavior if we directly use the value from a Get call after the transaction has completed. --- blockchain/chainio.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/blockchain/chainio.go b/blockchain/chainio.go index 3340dd14..27028eac 100644 --- a/blockchain/chainio.go +++ b/blockchain/chainio.go @@ -1058,7 +1058,14 @@ func dbPutUtxoStateConsistency(dbTx database.Tx, hash *chainhash.Hash) error { // nothing was found. func dbFetchUtxoStateConsistency(dbTx database.Tx) []byte { // Fetch the serialized data from the database. - return dbTx.Metadata().Get(utxoStateConsistencyKeyName) + statusBytes := dbTx.Metadata().Get(utxoStateConsistencyKeyName) + if statusBytes != nil { + result := make([]byte, len(statusBytes)) + copy(result, statusBytes) + return result + } + + return nil } // createChainState initializes both the database and the chain state to the