Skip to content

Commit 27aef3f

Browse files
committed
fix neo4j-labs#150. read_chunk had a buffer read bug before
1 parent 5119ba7 commit 27aef3f

File tree

1 file changed

+11
-30
lines changed

1 file changed

+11
-30
lines changed

lib/src/connection.rs

+11-30
Original file line numberDiff line numberDiff line change
@@ -154,36 +154,17 @@ impl Connection {
154154
// let pos = bytes.len();
155155
// bytes.resize(pos + chunk_size, 0);
156156
// self.stream.read_exact(&mut bytes[pos..]).await?;
157-
//
158-
// Alternatively, this an unsafe variant of the following except
159-
// it does read extactly `chunk_size` bytes and not maybe more
160-
//
161-
// bytes.reserve(chunk_size);
162-
// self.stream.read_buf(&mut bytes).await?;
163-
164-
buf.reserve(chunk_size);
165-
let read = {
166-
// shadowing `buf` and the extra block help with
167-
// maintaining the safety invariants
168-
let buf = buf.chunk_mut();
169-
let available = buf.len();
170-
assert!(available >= chunk_size);
171-
172-
// SAFETY:
173-
// We are only passing the buffer to `read_buf` which will
174-
// never read and never write uninitialized data.
175-
let buf = unsafe { buf.as_uninit_slice_mut() };
176-
let mut buf = &mut buf[..chunk_size];
177-
178-
let read = self.stream.read_buf(&mut buf).await?;
179-
assert!(read <= chunk_size);
180-
181-
read
182-
};
183-
184-
// SAFETY: We have asserted to have read `read` bytes into the buffer.
185-
unsafe { buf.advance_mut(read) };
186-
157+
let pos = buf.len();
158+
let new_len = pos + chunk_size;
159+
// Ensure the buffer has enough capacity
160+
if buf.capacity() < new_len {
161+
buf.reserve(new_len - buf.capacity());
162+
}
163+
// Unsafe to set the length of the buffer, but we will fill it with read_exact
164+
unsafe {
165+
buf.set_len(new_len);
166+
}
167+
self.stream.read_exact(&mut buf[pos..]).await?;
187168
Ok(())
188169
}
189170
}

0 commit comments

Comments
 (0)