|
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] Re: [PATCH 4/4] automation/eclair: deviate MISRA C Rule 11.8 for xfree() and vfree()
On 17.11.2026 23:35, Stefano Stabellini wrote: > On Tue, 17 Nov 2026, Andrew Cooper wrote: >> On 17/11/2026 8:01 pm, Dmytro Prokopchuk wrote: >>> On 11/4/26 13:24, Andrew Cooper wrote: >>>> On 03/11/2026 10:29 am, Dmytro Prokopchuk1 wrote: >>>>> Livepatch passes const-qualified pointers from struct payload to >>>>> xfree() >>>>> and vfree(), which take a 'void *' argument. The cast that removes the >>>>> const qualifier is a violation of MISRA C Rule 11.8. >>>>> >>>>> Such casts are safe. The deallocation functions neither read nor write >>>>> the content of the object being freed, and the object is not accessed >>>>> after it has been freed. >>>> >>>> I have a very firm objection to this. >>>> >>>> It is impossible for any allocation from malloc()/calloc()/etc to point >>>> to read-only memory, which is why free() takes a mutable pointer, not a >>>> const pointer. The same is true of any other heap implementation. >>>> >>>> It is impossible to make an object in read-only memory undefined. >>>> Objects in read-only memory by definition have a global lifetime, and it >>>> is absurd to argue that a specific heap implementation ought to permit >>>> making such objects undefined. >>>> >>>> >>>> The bug here is livepatch storing a heap allocation in a const pointer. >>>> >>>> This comes from deeper problem where some contributors/reviewers want >>>> the const keyword to mean something it doesn't. >>>> >>>> ~Andrew >>> >>> Jan, Andrew, >>> >>> thanks for the review. Given the disagreement, I will drop this >>> deviation for now. >>> >>> If the preferred fix is to stop storing heap allocations in >>> const-qualified members of 'struct payload' (so the casts go away), I >>> can prepare a follow-up patch. Please confirm that is the direction >>> you want for livepatch. >> >> Please remove the const qualification in struct payload. The logic >> should not have been accepted in that state to begin with. >> >> This is the only course of action here which does not create a class of >> undefined behaviour, or permit said undefined behaviour to go unnoticed. > > I agree with Andrew. xfree() does write into the freed block, so a > const parameter would be misleading. Well, this is the point: Yes, xfree() modifies the underlying memory. But no, it does not modify the object a pointer to which was passed into it. That object simply ceases to exist. And what C is concerned about are objects, not the underlying memory. I also still don't quite understand why Andrew considers such uses of const wrong: I don't think the spec says anything in this regard, and the C library's free() taking plain void * can easily be seen as heritage. Using const like this properly conveys to compiler and readers that post- allocation / -population such objects aren't supposed to be altered again. Together with forbidding the casting away of const-ness, this serves as an extra level of safety of the code overall. Jan
|
![]() |
Lists.xenproject.org is hosted with RackSpace, monitoring our |