Skip to content

Commit c0ab585

Browse files
authored
Merge pull request #270 from mitsuhiko/feature/do-not-crash-in-buffer
Do not crash with a segfault if empty buffer is passed
2 parents 8b7db11 + ebb6d8e commit c0ab585

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/easy/form.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,16 @@ impl<'form, 'data> Part<'form, 'data> {
250250
self._buffer(name.as_ref(), data)
251251
}
252252

253-
fn _buffer(&mut self, name: &'data Path, data: Vec<u8>) -> &mut Self {
253+
fn _buffer(&mut self, name: &'data Path, mut data: Vec<u8>) -> &mut Self {
254254
if let Some(bytes) = self.path2cstr(name) {
255+
// If `CURLFORM_BUFFERLENGTH` is set to `0`, libcurl will instead do a strlen() on the
256+
// contents to figure out the size so we need to make sure the buffer is actually
257+
// zero terminated.
258+
let length = data.len();
259+
if length == 0 {
260+
data.push(0);
261+
}
262+
255263
let pos = self.array.len() - 1;
256264
self.array.insert(
257265
pos,
@@ -272,7 +280,7 @@ impl<'form, 'data> Part<'form, 'data> {
272280
pos + 2,
273281
curl_sys::curl_forms {
274282
option: curl_sys::CURLFORM_BUFFERLENGTH,
275-
value: data.len() as *mut _,
283+
value: length as *mut _,
276284
},
277285
);
278286
self.form.buffers.push(data);

0 commit comments

Comments
 (0)