Skip to content

Commit 95a7ca5

Browse files
committed
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.
1 parent 637e707 commit 95a7ca5

File tree

2 files changed

+2
-2
lines changed

2 files changed

+2
-2
lines changed

examples/chardev.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ enum {
3636
/* Is device open? Used to prevent multiple access to device */
3737
static atomic_t already_open = ATOMIC_INIT(CDEV_NOT_USED);
3838

39-
static char msg[BUF_LEN]; /* The msg the device will give when asked */
39+
static char msg[BUF_LEN + 1]; /* The msg the device will give when asked */
4040

4141
static struct class *cls;
4242

examples/chardev2.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ enum {
2828
static atomic_t already_open = ATOMIC_INIT(CDEV_NOT_USED);
2929

3030
/* The message the device will give when asked */
31-
static char message[BUF_LEN];
31+
static char message[BUF_LEN + 1];
3232

3333
static struct class *cls;
3434

0 commit comments

Comments
 (0)