Skip to content

Commit a718e28

Browse files
aryabinintorvalds
authored andcommitted
mm/fadvise.c: fix signed overflow UBSAN complaint
Signed integer overflow is undefined according to the C standard. The overflow in ksys_fadvise64_64() is deliberate, but since it is signed overflow, UBSAN complains: UBSAN: Undefined behaviour in mm/fadvise.c:76:10 signed integer overflow: 4 + 9223372036854775805 cannot be represented in type 'long long int' Use unsigned types to do math. Unsigned overflow is defined so UBSAN will not complain about it. This patch doesn't change generated code. [akpm@linux-foundation.org: add comment explaining the casts] Link: http://lkml.kernel.org/r/20180629184453.7614-1-aryabinin@virtuozzo.com Signed-off-by: Andrey Ryabinin <aryabinin@virtuozzo.com> Reported-by: <icytxw@gmail.com> Reviewed-by: Andrew Morton <akpm@linux-foundation.org> Cc: Alexander Potapenko <glider@google.com> Cc: Dmitry Vyukov <dvyukov@google.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 31f21da commit a718e28

1 file changed

Lines changed: 6 additions & 2 deletions

File tree

‎mm/fadvise.c‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,12 @@ int ksys_fadvise64_64(int fd, loff_t offset, loff_t len, int advice)
7272
goto out;
7373
}
7474

75-
/* Careful about overflows. Len == 0 means "as much as possible" */
76-
endbyte = offset + len;
75+
/*
76+
* Careful about overflows. Len == 0 means "as much as possible". Use
77+
* unsigned math because signed overflows are undefined and UBSan
78+
* complains.
79+
*/
80+
endbyte = (u64)offset + (u64)len;
7781
if (!len || endbyte < len)
7882
endbyte = -1;
7983
else

0 commit comments

Comments
 (0)