From 7205537b490f5c205772d3833d273c9b8a74179e Mon Sep 17 00:00:00 2001 From: Voldemarich Date: Mon, 10 Jul 2023 11:35:43 +0300 Subject: [PATCH] [BUG] Fix binding of NULL value parameters in prepared statements (#496) Fix binding of NULL value parameters in prepared statements Co-authored-by: anon --- src/messages.rs | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/messages.rs b/src/messages.rs index 196abe83..1f494658 100644 --- a/src/messages.rs +++ b/src/messages.rs @@ -832,10 +832,21 @@ impl TryFrom<&BytesMut> for Bind { for _ in 0..num_param_values { let param_len = cursor.get_i32(); - let mut param = BytesMut::with_capacity(param_len as usize); - param.resize(param_len as usize, b'0'); - cursor.copy_to_slice(&mut param); - param_values.push((param_len, param)); + // There is special occasion when the parameter is NULL + // In that case, param length is defined as -1 + // So if the passed parameter len is over 0 + if param_len > 0 { + let mut param = BytesMut::with_capacity(param_len as usize); + param.resize(param_len as usize, b'0'); + cursor.copy_to_slice(&mut param); + // we push and the length and the parameter into vector + param_values.push((param_len, param)); + } else { + // otherwise we push a tuple with -1 and 0-len BytesMut + // which means that after encountering -1 postgres proceeds + // to processing another parameter + param_values.push((param_len, BytesMut::new())); + } } let num_result_column_format_codes = cursor.get_i16();