[PATCH v2] cangen: flush the CAN frame at each iteration

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



The variable 'frame' is declared on the stack and is reused at each
iteration of the while loop.

If not flushed, garbage data from the previous iteration of the loop
could remain causing unexpected behavior. This is the case in DLC
random mode: the field frame.len8_dlc is not cleared when the dlc is
exactly 8 and the len8_dlc value of the previous iteration will be
sent again.

This can be observed by running 'cangen -8 can0'. Once a frame of DLC
in the range 9..15 is generated, no more frames of DLC 8 show out in
the log.

Signed-off-by: Vincent Mailhol <mailhol.vincent@xxxxxxxxxx>
--

Changes in v2: v1 was dumb and broke everything which was not in
random mode.  Just clear the len8_dlc.

---
 cangen.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/cangen.c b/cangen.c
index ab8122c..14887c8 100644
--- a/cangen.c
+++ b/cangen.c
@@ -171,7 +171,7 @@ int main(int argc, char **argv)
 	struct pollfd fds;
 
 	struct sockaddr_can addr;
-	static struct canfd_frame frame;
+	static struct canfd_frame frame = { 0 };
 	int nbytes;
 	int i;
 	struct ifreq ifr;
@@ -418,16 +418,16 @@ int main(int argc, char **argv)
 			if (canfd)
 				frame.len = can_dlc2len(random() & 0xF);
 			else {
+				struct can_frame *ccf = (struct can_frame *)&frame;
+
 				frame.len = random() & 0xF;
+				ccf->len8_dlc = 0;
 
 				if (frame.len > CAN_MAX_DLEN) {
-					if (len8_dlc) {
-						struct can_frame *ccf = (struct can_frame *)&frame;
-
+					if (len8_dlc)
 						/* generate Classic CAN len8 DLCs */
 						ccf->len8_dlc = frame.len;
-					}
-					frame.len = 8; /* for about 50% of the frames */
+					frame.len = CAN_MAX_DLEN; /* for about 50% of the frames */
 				}
 			}
 		}
-- 
2.26.2




[Index of Archives]     [Automotive Discussions]     [Linux ARM Kernel]     [Linux ARM]     [Linux Omap]     [Fedora ARM]     [IETF Annouce]     [Security]     [Bugtraq]     [Linux]     [Linux OMAP]     [Linux MIPS]     [eCos]     [Asterisk Internet PBX]     [Linux API]     [CAN Bus]

  Powered by Linux