From f8dd17f3de39b61d3296b585cf49c07348f60369 Mon Sep 17 00:00:00 2001 From: anon Date: Thu, 6 Jul 2023 00:20:57 +0300 Subject: [PATCH] Fix binding of NULL value parameters in prepared statements --- 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();