Skip to contentSkip to conversation

ksmbd: clean up a type in ksmbd_vfs_stream_write()

PATCH In review Dan Carpenter 2 participants 5 messages Jun 2, 2022 – Jun 3, 2022

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ksmbd: clean up a type in ksmbd_vfs_stream_write()

The existing code works fine because ksmbd_vfs_getcasexattr() isn't going to return values greater than INT_MAX. But it's ugly to do the casting and using a ssize_t makes everything cleaner.

---
 fs/ksmbd/vfs.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
fs/ksmbd/vfs.c +4 -3
index dcdd07c6efff..efdc35717f6d 100644
@@ -411,7 +411,8 @@ static int ksmbd_vfs_stream_write(struct ksmbd_file *fp, char *buf, loff_t *pos,
 {
 	char *stream_buf = NULL, *wbuf;
 	struct user_namespace *user_ns = file_mnt_user_ns(fp->filp);
-	size_t size, v_len;
+	ssize_t v_len;
+	size_t size;
 	int err = 0;
 
 	ksmbd_debug(VFS, "write stream data pos : %llu, count : %zd\n",
@@ -428,9 +429,9 @@ static int ksmbd_vfs_stream_write(struct ksmbd_file *fp, char *buf, loff_t *pos,
 				       fp->stream.name,
 				       fp->stream.size,
 				       &stream_buf);
-	if ((int)v_len < 0) {
+	if (v_len < 0) {
 		pr_err("not found stream in xattr : %zd\n", v_len);
-		err = (int)v_len;
+		err = v_len;
 		goto out;
 	}
 
Signature · 1 line
2.35.1
maintainer
#1 lore raw

2022-06-02 19:51 GMT+09:00, Dan Carpenter <[email protected]>:

Quoted 25 lines

The existing code works fine because ksmbd_vfs_getcasexattr() isn't going to return values greater than INT_MAX. But it's ugly to do the casting and using a ssize_t makes everything cleaner.

---
 fs/ksmbd/vfs.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
fs/ksmbd/vfs.c +2 -1
index dcdd07c6efff..efdc35717f6d 100644
@@ -411,7 +411,8 @@ static int ksmbd_vfs_stream_write(struct ksmbd_file *fp,
 char *buf, loff_t *pos,
 {
 	char *stream_buf = NULL, *wbuf;
 	struct user_namespace *user_ns = file_mnt_user_ns(fp->filp);
-	size_t size, v_len;
+	ssize_t v_len;
+	size_t size;
 	int err = 0;
 
fs/ksmbd/vfs.c +0 -0
 	ksmbd_debug(VFS, "write stream data pos : %llu, count : %zd\n",

fs/ksmbd/vfs.c static int ksmbd_vfs_stream_write(struct ksmbd_file *fp, · line 435

Quoted 9 lines
fs/ksmbd/vfs.c +2 -2
@@ -428,9 +429,9 @@ static int ksmbd_vfs_stream_write(struct ksmbd_file *fp,
 char *buf, loff_t *pos,
 				       fp->stream.name,
 				       fp->stream.size,
 				       &stream_buf);
-	if ((int)v_len < 0) {
+	if (v_len < 0) {
 		pr_err("not found stream in xattr : %zd\n", v_len);
-		err = (int)v_len;
+		err = v_len;

Data type of ssize_t is long. Wouldn't some static checker warn us that this is a problem?

Thanks!

Quoted 7 lines
fs/ksmbd/vfs.c +0 -1
 		goto out;
 	}
 
--

2.35.1

#2 lore raw
Quoted 13 lines · Namjae Jeon

2022-06-02 19:51 GMT+09:00, Dan Carpenter <[email protected]>:

fs/ksmbd/vfs.c static int ksmbd_vfs_stream_write(struct ksmbd_file *fp, · line 435

Quoted 10 lines
fs/ksmbd/vfs.c +2 -2
@@ -428,9 +429,9 @@ static int ksmbd_vfs_stream_write(struct ksmbd_file *fp,
 char *buf, loff_t *pos,
 				       fp->stream.name,
 				       fp->stream.size,
 				       &stream_buf);
-	if ((int)v_len < 0) {
+	if (v_len < 0) {
 		pr_err("not found stream in xattr : %zd\n", v_len);
-		err = (int)v_len;
+		err = v_len;

Data type of ssize_t is long. Wouldn't some static checker warn us that this is a problem?

None that I know of.

To a human reader, the cast isn't needed because when a function returns ssize_t the negatives can only be error codes in the (-4095)-(-1) range. No other negative sizes make sense.

On the other hand, the "if ((int)v_len < 0) {" line really should trigger a static checker because there are times when sizes could be over 2GB. I will write down that I need to create that checker.

regards,
dan carpenter

maintainer
#3 lore raw

2022-06-03 9:10 GMT+09:00, Dan Carpenter <[email protected]>:

Quoted 18 lines · Namjae Jeon
Quoted 14 lines

2022-06-02 19:51 GMT+09:00, Dan Carpenter <[email protected]>:

fs/ksmbd/vfs.c static int ksmbd_vfs_stream_write(struct ksmbd_file · line 436

Quoted 11 lines
fs/ksmbd/vfs.c +2 -2
@@ -428,9 +429,9 @@ static int ksmbd_vfs_stream_write(struct ksmbd_file
 *fp,
 char *buf, loff_t *pos,
 				       fp->stream.name,
 				       fp->stream.size,
 				       &stream_buf);
-	if ((int)v_len < 0) {
+	if (v_len < 0) {
 		pr_err("not found stream in xattr : %zd\n", v_len);
-		err = (int)v_len;
+		err = v_len;

Data type of ssize_t is long. Wouldn't some static checker warn us that this is a problem?

None that I know of.

Quoted 7 lines

To a human reader, the cast isn't needed because when a function returns ssize_t the negatives can only be error codes in the (-4095)-(-1) range. No other negative sizes make sense.

On the other hand, the "if ((int)v_len < 0) {" line really should trigger a static checker because there are times when sizes could be over 2GB. I will write down that I need to create that checker.

Okay, And there is a similar type casting in ksmbd_vfs_stream_read(). Is it not necessary to change together?

Quoted 5 lines

regards,
dan carpenter

#4 lore raw
Quoted 23 lines · Namjae Jeon

2022-06-03 9:10 GMT+09:00, Dan Carpenter <[email protected]>:

Quoted 22 lines · Namjae Jeon
Quoted 14 lines

2022-06-02 19:51 GMT+09:00, Dan Carpenter <[email protected]>:

fs/ksmbd/vfs.c static int ksmbd_vfs_stream_write(struct ksmbd_file · line 436

Quoted 11 lines
fs/ksmbd/vfs.c +2 -2
@@ -428,9 +429,9 @@ static int ksmbd_vfs_stream_write(struct ksmbd_file
 *fp,
 char *buf, loff_t *pos,
 				       fp->stream.name,
 				       fp->stream.size,
 				       &stream_buf);
-	if ((int)v_len < 0) {
+	if (v_len < 0) {
 		pr_err("not found stream in xattr : %zd\n", v_len);
-		err = (int)v_len;
+		err = v_len;

Data type of ssize_t is long. Wouldn't some static checker warn us that this is a problem?

None that I know of.

To a human reader, the cast isn't needed because when a function returns ssize_t the negatives can only be error codes in the (-4095)-(-1) range. No other negative sizes make sense.

Quoted 5 lines
Quoted 3 lines

On the other hand, the "if ((int)v_len < 0) {" line really should trigger a static checker because there are times when sizes could be over 2GB. I will write down that I need to create that checker.

Okay, And there is a similar type casting in ksmbd_vfs_stream_read(). Is it not necessary to change together?

The motivation for this change was that I was looking for places which save negative error codes in a positive value. Since the ksmbd_vfs_stream_read() is using ssize_t then it didn't trigger that warning. But ksmbd_vfs_stream_read() has some minor issues.

	if ((int)v_len <= 0)

This cast is "buggy". Harmless though.

		return (int)v_len;

This is fine, but unnecessary.

	if (v_len <= *pos) {
		count = -EINVAL;

I feel like this should return 0 bytes instead of -EINVAL; It probably doesn't matter.

I am going to try write a static checker that complains specifically about casting size_t (not long) to int. I will make it complain about ssize_t as well, but that's kind of unusual because ssize_t can already store negatives.

regards,
dan carpenter