Skip to content

Commit

Permalink
Fix a wierd behavior of a function
Browse files Browse the repository at this point in the history
The procfile_write prints the content what user writes into. However, when
the content size is greater than or equal to PROCFS_MAX_SIZE, procfile_write
will print nothing, because the index for appending the tail NULL character
will be modulo to 0.
  • Loading branch information
NOVBobLee committed Oct 6, 2024
1 parent 1fc5305 commit 1030e29
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions examples/procfs2.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ static ssize_t procfile_write(struct file *file, const char __user *buff,
size_t len, loff_t *off)
{
procfs_buffer_size = len;
if (procfs_buffer_size > PROCFS_MAX_SIZE)
procfs_buffer_size = PROCFS_MAX_SIZE;
if (procfs_buffer_size >= PROCFS_MAX_SIZE)
procfs_buffer_size = PROCFS_MAX_SIZE - 1;

if (copy_from_user(procfs_buffer, buff, procfs_buffer_size))
return -EFAULT;

procfs_buffer[procfs_buffer_size & (PROCFS_MAX_SIZE - 1)] = '\0';
procfs_buffer[procfs_buffer_size] = '\0';
*off += procfs_buffer_size;
pr_info("procfile write %s\n", procfs_buffer);

Expand Down

0 comments on commit 1030e29

Please sign in to comment.