summary history branches tags files
commit:cb57fdbe3e402a475fe0db5daf581bffafd80bdd
author:Trevor Bentley
committer:Trevor Bentley
date:Sat May 25 14:56:03 2019 +0200
parents:13811932d14173636a4e63dfefa7ae118ea7fdb9
FFI: don't overwrite input length on ossuary_recv_data
diff --git a/examples/ffi.c b/examples/ffi.c
line changes: +1/-1
index 782ba01..64d7eef
--- a/examples/ffi.c
+++ b/examples/ffi.c
@@ -90,7 +90,7 @@ int main(int argc, char **argv) {
 
   // Client receives decrypted data
   out_len = sizeof(client_buf);
-  bytes = ossuary_recv_data(client_conn, client_buf, &bytes, client_buf, &out_len);
+  bytes = ossuary_recv_data(client_conn, client_buf, bytes, client_buf, &out_len);
   printf("client recv data bytes: %d\n", bytes);
   printf("decrypted: %s\n", client_buf);
 

diff --git a/ffi/ossuary.h b/ffi/ossuary.h
line changes: +1/-1
index 04d3b13..5c91156
--- a/ffi/ossuary.h
+++ b/ffi/ossuary.h
@@ -34,7 +34,7 @@ int32_t ossuary_send_data(OssuaryConnection *conn,
                           uint8_t *in_buf, uint16_t in_buf_len,
                           uint8_t *out_buf, uint16_t *out_buf_len);
 int32_t ossuary_recv_data(OssuaryConnection *conn,
-                          uint8_t *in_buf, uint16_t *in_buf_len,
+                          uint8_t *in_buf, uint16_t in_buf_len,
                           uint8_t *out_buf, uint16_t *out_buf_len);
 
 #define _OSSUARY_H

diff --git a/src/clib.rs b/src/clib.rs
line changes: +3/-5
index 7418285..14ff831
--- a/src/clib.rs
+++ b/src/clib.rs
@@ -190,23 +190,21 @@ pub extern "C" fn ossuary_send_data(conn: *mut OssuaryConnection,
 
 #[no_mangle]
 pub extern "C" fn ossuary_recv_data(conn: *mut OssuaryConnection,
-                                    in_buf: *mut u8, in_buf_len: *mut u16,
+                                    in_buf: *mut u8, in_buf_len: u16,
                                     out_buf: *mut u8, out_buf_len: *mut u16) -> i32 {
-    if conn.is_null() || in_buf.is_null() || out_buf.is_null() ||
-        in_buf_len.is_null() || out_buf_len.is_null() {
+    if conn.is_null() || in_buf.is_null() || out_buf.is_null() ||  out_buf_len.is_null() {
         return -1i32;
     }
     let conn = unsafe { &mut *conn };
     let r_out_buf: &mut [u8] = unsafe {
         std::slice::from_raw_parts_mut(out_buf, *out_buf_len as usize)
     };
-    let r_in_buf: &[u8] = unsafe { std::slice::from_raw_parts(in_buf, *in_buf_len as usize) };
+    let r_in_buf: &[u8] = unsafe { std::slice::from_raw_parts(in_buf, in_buf_len as usize) };
     let mut out_slice = r_out_buf;
     let mut in_slice = r_in_buf;
     let bytes_read = match conn.recv_data(&mut in_slice, &mut out_slice) {
         Ok((read,written)) => {
             unsafe {
-                *in_buf_len = read as u16;
                 *out_buf_len = written as u16;
             };
             read as i32

diff --git a/tests/clib_ffi.rs b/tests/clib_ffi.rs
line changes: +4/-4
index 87f5349..ec07907
--- a/tests/clib_ffi.rs
+++ b/tests/clib_ffi.rs
@@ -78,10 +78,10 @@ fn server() -> Result<(), std::io::Error> {
         let in_buf = reader.fill_buf().unwrap();
         if in_buf.len() > 0 {
             let mut out_len = out_buf.len() as u16;
-            let mut in_len = in_buf.len() as u16;
+            let in_len = in_buf.len() as u16;
             let len = ossuary_recv_data(
                 conn,
-                (in_buf) as *const [u8] as *mut u8, &mut in_len,
+                (in_buf) as *const [u8] as *mut u8, in_len,
                 (&out_buf) as *const u8 as *mut u8, &mut out_len);
             if len != -1 {
                 println!("CLIB READ: {:?}",
@@ -157,10 +157,10 @@ fn client() -> Result<(), std::io::Error> {
             break;
         }
         let mut out_len = out_buf.len() as u16;
-        let mut in_len = in_buf.len() as u16;
+        let in_len = in_buf.len() as u16;
         let len = ossuary_recv_data(
             conn,
-            in_buf as *const [u8] as *mut u8, &mut in_len,
+            in_buf as *const [u8] as *mut u8, in_len,
             (&out_buf) as *const u8 as *mut u8, &mut out_len);
         if len == -1 {
             break;