Re: [PATCH 01/24] mm/swap: fix a potential undefined behavior issue

From: Matthew Wilcox
Date: Sun Nov 19 2023 - 15:56:33 EST


On Mon, Nov 20, 2023 at 03:47:17AM +0800, Kairui Song wrote:
> From: Kairui Song <kasong@xxxxxxxxxxx>
>
> When folio is NULL, taking the address of its struct member is an
> undefined behavior, the UB is caused by applying -> operator
> to a pointer not pointing to any object. Although in practice this
> won't lead to a real issue, still better to fix it, also makes the
> code less error-prone, when folio is NULL, page is also NULL,
> instead of a meanless offset value.

Um, &folio->page is NULL if folio is NULL. The offset of 'page' within
'folio' is 0. By definition; and this will never change.