Skip to content

Commit 97ac07c

Browse files
committed
avoid using null pointer everywhere possible
1 parent 0da33ff commit 97ac07c

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

bindings/src/adaptors/mod.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -256,21 +256,21 @@ impl ChainWatchInterface for FFIChainWatchInterface {
256256
(self.watch_all_txn_ptr)()
257257
}
258258
fn get_chain_utxo(&self, genesis_hash: BlockHash, unspent_tx_output_identifier: u64) -> Result<(Script, u64), ChainError> {
259-
let err = std::ptr::null_mut();
259+
println!("Querying chain utxo by shortChannelId {}. ", unspent_tx_output_identifier);
260+
let mut err = &mut FFIChainError::UnInitialized;
260261
// the length can be anything as long as it is enough to put the scriptPubKey.
261262
// probably this is a bit overkill but who cares.
262263
let mut script = [0u8; 128];
263-
let script_len = std::ptr::null_mut();
264-
let amount_satoshis = std::ptr::null_mut();
265-
(self.get_chain_utxo_ptr)(&genesis_hash.into(), unspent_tx_output_identifier, err, script.as_mut_ptr(), script_len, amount_satoshis);
266-
if err.is_null() {
264+
let mut script_len = &mut usize::MAX;
265+
let mut amount_satoshis = &mut u64::MAX;
266+
(self.get_chain_utxo_ptr)(&genesis_hash.into(), unspent_tx_output_identifier, err as *mut FFIChainError, script.as_mut_ptr(), script_len as *mut _, amount_satoshis as *mut _);
267+
if *err == FFIChainError::UnInitialized {
267268
let script_bytes: &[u8] = unsafe_block!("We know the caller has set the value into the script_ptr, script_len" => &script[..(*script_len)]);
268269
let amount: u64 = unsafe_block!("We know the caller has set the value into the amount_satoshis" => *amount_satoshis);
269270
let s = bitcoin::consensus::deserialize(script_bytes).expect("Failed to parse scriptpubkey");
270271
Ok((s, amount))
271272
} else {
272-
let e = unsafe_block!("we know the error is not a null pointer" => (*err).clone());
273-
Err(e.into())
273+
Err(err.clone().into())
274274
}
275275
}
276276

bindings/src/adaptors/primitives.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,15 +228,16 @@ pub struct FFITxOut {
228228
pub script_pubkey: FFIScript,
229229
}
230230

231-
#[derive(Clone, Copy, Debug)]
232-
#[repr(C)]
231+
#[derive(Clone, Copy, Debug, PartialEq)]
232+
#[repr(u32)]
233233
pub enum FFIChainError {
234234
/// Client doesn't support UTXO lookup (but the chain hash matches our genesis block hash)
235235
NotSupported,
236236
/// Chain isn't the one watched
237237
NotWatched,
238238
/// Tx doesn't exist or is unconfirmed
239239
UnknownTx,
240+
UnInitialized,
240241
}
241242

242243
impl From<FFIChainError> for ChainError {
@@ -245,6 +246,7 @@ impl From<FFIChainError> for ChainError {
245246
FFIChainError::NotSupported => ChainError::NotSupported,
246247
FFIChainError::NotWatched => ChainError::NotWatched,
247248
FFIChainError::UnknownTx => ChainError::UnknownTx,
249+
FFIChainError::UnInitialized => panic!("We should never try to convert uninitialized FFIChainError into ChainError")
248250
}
249251
}
250252
}

0 commit comments

Comments
 (0)