Re: [PATCH] fs: Avoid signed overflow UBSAN warning

From: Matthew Wilcox
Date: Mon Mar 04 2019 - 23:26:51 EST


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;