Re: [PATCH] mm: add `const` to lots of pointer parameters
From: Andrew Morton
Date: Wed Aug 27 2025 - 22:47:21 EST
On Wed, 27 Aug 2025 21:48:21 -0400 "Liam R. Howlett" <Liam.Howlett@xxxxxxxxxx> wrote:
> > I'd actually be in favor of making all incoming args const (C should
> > have made this the default).
> >
> > Because modifying an incoming arg is just obnoxious. That value should
> > be viewed as part of the calling environment and should not be altered.
> >
> > Try modifying a lengthy function and wanting to get at an incoming arg
> > only to find that something in the preceding 100 lines has gone and
> > messed with it. Or forget to check fr this and get a nasty surprise when
> > testing.
> >
> > Not that I'm suggesting that someone go in and make this change.
> >
> > On the other hand, it would be neat if gcc had an option to warn when
> > someone does this. I bet it would be simple to add.
>
> But what of the lazy programmer that just uses the variable at hand
> instead of declaring another one?
Sure, I'd say that's OK for a small function (home-made strcpy) but for
a larger function, expecting other developers to read back through
everything to make sure that Nobody Did That is way unacceptable. It's
laying a trap.
> I also wonder how this would affect inlining choices?
I'd be surprised if the compiler didn't trivially optimize through such
things.