瀏覽代碼

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.
linD026 2 年之前
父節點
當前提交
95a7ca513f
共有 2 個文件被更改,包括 2 次插入2 次删除
  1. 1 1
      examples/chardev.c
  2. 1 1
      examples/chardev2.c

+ 1 - 1
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;
 

+ 1 - 1
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;