Skip to content

Commit 7205537

Browse files
voldemarichanon
andauthored
[BUG] Fix binding of NULL value parameters in prepared statements (#496)
Fix binding of NULL value parameters in prepared statements Co-authored-by: anon <anon@non.existent>
1 parent 1ed6e92 commit 7205537

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

src/messages.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -832,10 +832,21 @@ impl TryFrom<&BytesMut> for Bind {
832832

833833
for _ in 0..num_param_values {
834834
let param_len = cursor.get_i32();
835-
let mut param = BytesMut::with_capacity(param_len as usize);
836-
param.resize(param_len as usize, b'0');
837-
cursor.copy_to_slice(&mut param);
838-
param_values.push((param_len, param));
835+
// There is special occasion when the parameter is NULL
836+
// In that case, param length is defined as -1
837+
// So if the passed parameter len is over 0
838+
if param_len > 0 {
839+
let mut param = BytesMut::with_capacity(param_len as usize);
840+
param.resize(param_len as usize, b'0');
841+
cursor.copy_to_slice(&mut param);
842+
// we push and the length and the parameter into vector
843+
param_values.push((param_len, param));
844+
} else {
845+
// otherwise we push a tuple with -1 and 0-len BytesMut
846+
// which means that after encountering -1 postgres proceeds
847+
// to processing another parameter
848+
param_values.push((param_len, BytesMut::new()));
849+
}
839850
}
840851

841852
let num_result_column_format_codes = cursor.get_i16();

0 commit comments

Comments
 (0)