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.
This commit is contained in:
linD026 2022-09-08 05:37:48 +08:00
parent 637e707a1a
commit 95a7ca513f
2 changed files with 2 additions and 2 deletions

View File

@ -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;

View File

@ -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;