linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] fs: Avoid signed overflow UBSAN warning
@ 2019-02-23  9:02 Hongbo Yao
  2019-03-05  4:26 ` Matthew Wilcox
  0 siblings, 1 reply; 2+ messages in thread
From: Hongbo Yao @ 2019-02-23  9:02 UTC (permalink / raw)
  To: viro, linux-fsdevel, linux-kernel, linuxarm

I ran into this:
	UBSAN: Undefined behaviour in fs/read_write.c:251:11
	signed integer overflow:
	9223372036854775805 + 9223372036854775805 cannot be represented in
	type 'long long int'

Use unsigned types to do math, it doesn't throw a UBSAN warning
when it does overflow.This patch doesn't change generated code.

Signed-off-by: Hongbo Yao <yaohongbo@huawei.com>
---
 fs/read_write.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/fs/read_write.c b/fs/read_write.c
index 8f08d4a2aa9a..0509e81afc8e 100644
--- a/fs/read_write.c
+++ b/fs/read_write.c
@@ -248,7 +248,7 @@ loff_t default_llseek(struct file *file, loff_t offset, int whence)
 				retval = file->f_pos;
 				goto out;
 			}
-			offset += file->f_pos;
+			offset = (u64)file->f_pos + (u64)offset;
 			break;
 		case SEEK_DATA:
 			/*
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] fs: Avoid signed overflow UBSAN warning
  2019-02-23  9:02 [PATCH] fs: Avoid signed overflow UBSAN warning Hongbo Yao
@ 2019-03-05  4:26 ` Matthew Wilcox
  0 siblings, 0 replies; 2+ messages in thread
From: Matthew Wilcox @ 2019-03-05  4:26 UTC (permalink / raw)
  To: Hongbo Yao; +Cc: viro, linux-fsdevel, linux-kernel, linuxarm

On Sat, Feb 23, 2019 at 05:02:10PM +0800, Hongbo Yao wrote:
> I ran into this:
> 	UBSAN: Undefined behaviour in fs/read_write.c:251:11
> 	signed integer overflow:
> 	9223372036854775805 + 9223372036854775805 cannot be represented in
> 	type 'long long int'
> 
> Use unsigned types to do math, it doesn't throw a UBSAN warning
> when it does overflow.This patch doesn't change generated code.

You don't need to be this verbose to fix it.

> -			offset += file->f_pos;
> +			offset = (u64)file->f_pos + (u64)offset;

			offset += (u64)file->f_pos;


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2019-03-05  4:26 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-02-23  9:02 [PATCH] fs: Avoid signed overflow UBSAN warning Hongbo Yao
2019-03-05  4:26 ` Matthew Wilcox

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).