Skip to content

Commit

Permalink
kfifo: fix ternary sign extension bugs
Browse files Browse the repository at this point in the history
The intent with this code was to return negative error codes but instead
it returns positives.

The problem is how type promotion works with ternary operations.  These
functions return long, "ret" is an int and "copied" is a u32.  The
negative error code is first cast to u32 so it becomes a high positive and
then cast to long where it's still a positive.

We could fix this by declaring "ret" as a ssize_t but let's just get rid
of the ternaries instead.

Link: https://lkml.kernel.org/r/YIE+/cK1tBzSuQPU@mwanda
Fixes: 5bf2b19 ("kfifo: add example files to the kernel sample directory")
Signed-off-by: Dan Carpenter <[email protected]>
Cc: Stefani Seibold <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
Dan Carpenter authored and torvalds committed Apr 30, 2021
1 parent ccf33ec commit 926ee00
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
8 changes: 6 additions & 2 deletions samples/kfifo/bytestream-example.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,10 @@ static ssize_t fifo_write(struct file *file, const char __user *buf,
ret = kfifo_from_user(&test, buf, count, &copied);

mutex_unlock(&write_lock);
if (ret)
return ret;

return ret ? ret : copied;
return copied;
}

static ssize_t fifo_read(struct file *file, char __user *buf,
Expand All @@ -138,8 +140,10 @@ static ssize_t fifo_read(struct file *file, char __user *buf,
ret = kfifo_to_user(&test, buf, count, &copied);

mutex_unlock(&read_lock);
if (ret)
return ret;

return ret ? ret : copied;
return copied;
}

static const struct proc_ops fifo_proc_ops = {
Expand Down
8 changes: 6 additions & 2 deletions samples/kfifo/inttype-example.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,10 @@ static ssize_t fifo_write(struct file *file, const char __user *buf,
ret = kfifo_from_user(&test, buf, count, &copied);

mutex_unlock(&write_lock);
if (ret)
return ret;

return ret ? ret : copied;
return copied;
}

static ssize_t fifo_read(struct file *file, char __user *buf,
Expand All @@ -131,8 +133,10 @@ static ssize_t fifo_read(struct file *file, char __user *buf,
ret = kfifo_to_user(&test, buf, count, &copied);

mutex_unlock(&read_lock);
if (ret)
return ret;

return ret ? ret : copied;
return copied;
}

static const struct proc_ops fifo_proc_ops = {
Expand Down
8 changes: 6 additions & 2 deletions samples/kfifo/record-example.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,10 @@ static ssize_t fifo_write(struct file *file, const char __user *buf,
ret = kfifo_from_user(&test, buf, count, &copied);

mutex_unlock(&write_lock);
if (ret)
return ret;

return ret ? ret : copied;
return copied;
}

static ssize_t fifo_read(struct file *file, char __user *buf,
Expand All @@ -145,8 +147,10 @@ static ssize_t fifo_read(struct file *file, char __user *buf,
ret = kfifo_to_user(&test, buf, count, &copied);

mutex_unlock(&read_lock);
if (ret)
return ret;

return ret ? ret : copied;
return copied;
}

static const struct proc_ops fifo_proc_ops = {
Expand Down

0 comments on commit 926ee00

Please sign in to comment.