Subject: Re: [PATCH 08/12] skip_prefix(): check const match between in and out params

From: Phillip Wood <[email-masked]>
Date: Wed, 1 Apr 2026 15:04:10 +0100

On 01/04/2026 14:17, Phillip Wood wrote:
> On 01/04/2026 00:50, Jeff King wrote:
>>
>> +/*
>> + * Check that an out-parameter that is "at least as const as" a matching
>> + * in-parameter. For example, skip_prefix() will return "out" that is 
>> a subset
>> + * of "str". So:
>> + *
>> + *  const str, const out: ok
>> + *  non-const str, const out: ok
>> + *  non-const str, non-const out: ok
>> + *  const str, non-const out: compile error
>> + *
>> + *  See the skip_prefix macro below for an example of use.
>> + */
>> +#define CONST_OUTPARAM(in, out) \
>> +    ((const char **)(0 ? ((*(out) = (in)),(out)) : (out)))
>> +#define skip_prefix(str, prefix, out) \
>> +    skip_prefix((str), (prefix), CONST_OUTPARAM((str), (out)))
> 
> This is clever but it changes the behavior of skip_prefix() which is 
> documented as not touching out if it returns false.

Sorry, I've just realized we always take the other branch so this does 
not change the behavior and is in fact a nice solution to the problem.

Thanks

Phillip

> That may not matter 
> in practice but there are nearly 600 callers so auditing them all would 
> be quite an undertaking. Elsewhere there was some discussion about using 
> type generic macros to fix the warnings. That would be more complex as 
> we need to check if they were supported by the compiler but it would 
> avoid changing the behavior.
> 
> Thanks for working on fixing these warnings, your approach of trying to 
> fix the underlying problem rather than casting away the warning is very 
> welcome.
> 
> Phillip
> 
>