From 95a7ca513fc50e17d40d5ac6bac7551bce08bb05 Mon Sep 17 00:00:00 2001 From: linD026 Date: Thu, 8 Sep 2022 05:37:48 +0800 Subject: [PATCH] Fix the buffer length may cause a read error Since The length of the message buffer is BUF_LEN. When writing the BUF_LEN length of the string it will overwrite the last character (usually it is '\0' from the initialization). And, because the read operation uses the character in the message buffer ('\0') to stop the read loop. It will cause the read operation will read out of the message buffer when the length parameter of read() is not lower than or equal to BUF_LEN. So add one more byte space to avoid this problem. --- examples/chardev.c | 2 +- examples/chardev2.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/chardev.c b/examples/chardev.c index c2f0858..a337d19 100644 --- a/examples/chardev.c +++ b/examples/chardev.c @@ -36,7 +36,7 @@ enum { /* Is device open? Used to prevent multiple access to device */ static atomic_t already_open = ATOMIC_INIT(CDEV_NOT_USED); -static char msg[BUF_LEN]; /* The msg the device will give when asked */ +static char msg[BUF_LEN + 1]; /* The msg the device will give when asked */ static struct class *cls; diff --git a/examples/chardev2.c b/examples/chardev2.c index db5e24d..58ed57b 100644 --- a/examples/chardev2.c +++ b/examples/chardev2.c @@ -28,7 +28,7 @@ enum { static atomic_t already_open = ATOMIC_INIT(CDEV_NOT_USED); /* The message the device will give when asked */ -static char message[BUF_LEN]; +static char message[BUF_LEN + 1]; static struct class *cls;