summaryrefslogtreecommitdiffstats
path: root/samples/kfifo/record-example.c
diff options
context:
space:
mode:
authorDan Carpenter <dan.carpenter@oracle.com>2021-04-29 22:54:15 -0700
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>2021-05-22 10:59:38 +0200
commit5350ad4401eee47f75f8b1d2567d422b4bc5502b (patch)
tree669f1c9c9f899ea2d0f872b2b47b9b30b4ac2560 /samples/kfifo/record-example.c
parent8de8d1d052dd5bf1d5556a33c2927fdfe5d6bf7e (diff)
downloadlinux-stable-5350ad4401eee47f75f8b1d2567d422b4bc5502b.tar.gz
linux-stable-5350ad4401eee47f75f8b1d2567d422b4bc5502b.tar.bz2
linux-stable-5350ad4401eee47f75f8b1d2567d422b4bc5502b.zip
kfifo: fix ternary sign extension bugs
[ Upstream commit 926ee00ea24320052b46745ef4b00d91c05bd03d ] 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: 5bf2b19320ec ("kfifo: add example files to the kernel sample directory") Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com> Cc: Stefani Seibold <stefani@seibold.net> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Sasha Levin <sashal@kernel.org>
Diffstat (limited to 'samples/kfifo/record-example.c')
-rw-r--r--samples/kfifo/record-example.c8
1 files changed, 6 insertions, 2 deletions
diff --git a/samples/kfifo/record-example.c b/samples/kfifo/record-example.c
index 2d7529eeb294..deb87a2e4e6b 100644
--- a/samples/kfifo/record-example.c
+++ b/samples/kfifo/record-example.c
@@ -131,8 +131,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,
@@ -147,8 +149,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 file_operations fifo_fops = {