Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitbdc1043

Browse files
committed
#78 Fixed memory overrun reading FD frames
1 parent308d9d6 commitbdc1043

File tree

4 files changed

+75
-10
lines changed

4 files changed

+75
-10
lines changed

‎CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
The change log for the Rust[socketcan](https://crates.io/crates/socketcan) library.
44

5+
##[Version 3.3.1](https://github.com/socketcan-rs/socketcan-rs/compare/v3.3.0..v3.3.1) (2023-10-27)
6+
7+
-[#78](https://github.com/socketcan-rs/socketcan-rs/issues/78) Memory error receiving CAN FD frames.
8+
9+
510
##[Version 3.3.0](https://github.com/socketcan-rs/socketcan-rs/compare/v3.2.0..v3.3.0) (2023-10-27)
611

712
-[#53](https://github.com/socketcan-rs/socketcan-rs/pull/53) Added CanFD support for tokio

‎Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name ="socketcan"
3-
version ="3.3.0"
3+
version ="3.3.1"
44
edition ="2021"
55
rust-version ="1.65"
66
authors = [

‎examples/echo_fd.rs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// socketcan/examples/echo_fd.rs
2+
//
3+
// This file is part of the Rust 'socketcan-rs' library.
4+
//
5+
// Licensed under the MIT license:
6+
// <LICENSE or http://opensource.org/licenses/MIT>
7+
// This file may not be copied, modified, or distributed except according
8+
// to those terms.
9+
//
10+
// @author Frank Pagliughi <fpagliughi@mindspring.com>
11+
// @author Natesh Narain <nnaraindev@gmail.com>
12+
// @date Jul 05 2022
13+
//
14+
15+
use anyhow::Context;
16+
use embedded_can::{FrameasEmbeddedFrame,StandardId};
17+
use socketcan::{CanAnyFrame,CanFdFrame,CanFdSocket,Frame,Socket};
18+
use std::{
19+
env,
20+
sync::atomic::{AtomicBool,Ordering},
21+
};
22+
23+
fnmain() -> anyhow::Result<()>{
24+
let iface = env::args().nth(1).unwrap_or_else(||"vcan0".into());
25+
26+
let sock =CanFdSocket::open(&iface)
27+
.with_context(||format!("Failed to open socket on interface {}", iface))?;
28+
29+
sock.set_nonblocking(true)
30+
.with_context(||"Failed to make socket non-blocking")?;
31+
32+
staticQUIT:AtomicBool =AtomicBool::new(false);
33+
34+
ctrlc::set_handler(||{
35+
QUIT.store(true,Ordering::Relaxed);
36+
})
37+
.expect("Failed to set signal handler");
38+
39+
while !QUIT.load(Ordering::Relaxed){
40+
ifletOk(CanAnyFrame::Fd(frame)) = sock.read_frame(){
41+
println!("{}", frame_to_string(&frame));
42+
43+
let new_id = frame.raw_id() +0x01;
44+
let new_id =StandardId::new(new_idasu16).expect("Failed to create ID");
45+
46+
ifletSome(echo_frame) =CanFdFrame::new(new_id, frame.data()){
47+
sock.write_frame(&echo_frame)
48+
.expect("Failed to echo recieved frame");
49+
}
50+
}
51+
}
52+
53+
Ok(())
54+
}
55+
56+
fnframe_to_string<F:Frame>(frame:&F) ->String{
57+
let id = frame.raw_id();
58+
59+
let data_string = frame
60+
.data()
61+
.iter()
62+
.fold(String::from(""), |a, b|format!("{} {:02x}", a, b));
63+
64+
format!("{:08X} [{}] {}", id, frame.dlc(), data_string)
65+
}

‎src/socket.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -122,20 +122,15 @@ impl CanAddr {
122122
mem::size_of::<sockaddr_can>()
123123
}
124124

125-
/// Converts the address into a `sockaddr_storage` type.
125+
/// Converts theCANaddress into a `sockaddr_storage` type.
126126
/// This is a generic socket address container with enough space to hold
127127
/// any address type in the system.
128128
pubfninto_storage(self) ->(sockaddr_storage,socklen_t){
129-
let len =Self::len();
130129
letmut storage:sockaddr_storage =unsafe{ mem::zeroed()};
131130
unsafe{
132-
ptr::copy_nonoverlapping(
133-
&self.0as*const_as*constsockaddr_storage,
134-
&mut storage,
135-
len,
136-
);
131+
ptr::copy_nonoverlapping(&self.0,&mut storageas*mut_as*mutsockaddr_can,1);
137132
}
138-
(storage, lenassocklen_t)
133+
(storage,Self::len()assocklen_t)
139134
}
140135
}
141136

@@ -700,7 +695,7 @@ impl Socket for CanFdSocket {
700695
ptr::copy_nonoverlapping(
701696
&fdframeas*const_as*constcan_frame,
702697
&mut frame,
703-
CAN_MTU,
698+
1,
704699
);
705700
}
706701
Ok(CanFrame::from(frame).into())

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp