git/list[1] front-page[2] threads[3] people[4] search[5] about
 

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

From
Jeff King <peff@peff.net>
Date
Apr 1, 2026, 19:24 UTC
Message-ID
<20260401192423.GA2905896@coredump.intra.peff.net>
In-Reply-To
<b77be594-83b4-4d9b-9f89-569f1f335d61@gmail.com>
On Wed, Apr 01, 2026 at 03:04:10PM +0100, Phillip Wood wrote:
Show 26 quoted lines
> 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.

Yeah, exactly. I was curious if the dead branch would be left in place, but gcc seems to prune it even at -O0.

I also pondered whether:
  (*out = in,out)

might be a problem, but I think it is OK. The "," is a sequence point, so it is well defined (of course we would never run this code anyway, but if we have undefined behavior in the code at all, it may cause confusing effects).

For reference, this is the more complicated one I came up with:
  /*
   * Note that builtin_types_compatible_p() counts "char" and "const
   * char" as the same type. So we deref and construct our own pointer
   * with const to find out it "x" is const, and then either compare
   * x and y exactly (if it is const, they must both be) or dereferenced
   * (which lets y be either const or not).
   */
  #define CONST_COMPATIBLE(x, y) \
          (__builtin_types_compatible_p(typeof(x), const typeof(*(x)) *) ? \
           __builtin_types_compatible_p(typeof(x), typeof(y)) : \
           __builtin_types_compatible_p(typeof(*(x)), typeof(*(y))))

I also tried using a gcc statement-expression and _Static_assert to get a nicer message, like this:

  #define CONST_OUTPARAM(in, out, in_name, out_name) ({ \
          _Static_assert(CONST_COMPATIBLE((in),*(out)), \
                         in_name " is not const-compatible with " out_name); \
          (const typeof(*(in)) **)(out); \
  })
  #define skip_prefix(str, prefix, out) \
          skip_prefix(str, prefix, CONST_OUTPARAM((str), (out), #str, #out))
It does produce slightly nicer output:
  foo.c: In function ‘bad’:
  foo.c:8:9: error: static assertion failed: "my_in_var is not const-compatible with my_out_var"
      8 |         _Static_assert(CONST_COMPATIBLE((in),*(out)), \
        |         ^~~~~~~~~~~~~~
  foo.c:13:34: note: in expansion of macro ‘CONST_OUTPARAM’
     13 |         skip_prefix(str, prefix, CONST_OUTPARAM((str), (out), #str, #out))
        |                                  ^~~~~~~~~~~~~~
  foo.c:16:9: note: in expansion of macro ‘skip_prefix’
     16 |         skip_prefix(my_in_var, "foo", my_out_var);

but I don't think the extra complexity and portability headache is worth it.

-Peff
Previous: Phillip WoodNext: Junio C Hamano
Message 18 of 50 in “fixing the remainder of the C23 strchr warnings”
  1. 0/12 fixing the remainder of the C23 strchr warningsJeff King, Mar 31, 2026
  2. 01/12 convert: add const to fix strchr() warningsJeff King, Mar 31, 2026
  3. 02/12 http: add const to fix strchr() warningsJeff King, Mar 31, 2026
  4. 03/12 transport-helper: drop const to fix strchr() warningsJeff King, Mar 31, 2026
  5. Patrick SteinhardtApr 1, 2026
  6. 04/12 pager: explicitly cast away strchr() constnessJeff King, Mar 31, 2026
  7. Junio C HamanoApr 1, 2026
  8. Jeff KingApr 2, 2026
  9. 05/12 run-command: explicitly cast away constness when assigning to voidJeff King, Mar 31, 2026
  10. 06/12 find_last_dir_sep(): convert inline function to macroJeff King, Mar 31, 2026
  11. 07/12 pseudo-merge: fix disk reads from find_pseudo_merge()Jeff King, Mar 31, 2026
  12. Jeff KingMar 31, 2026
  13. Junio C HamanoApr 1, 2026
  14. Taylor BlauApr 2, 2026
  15. 08/12 skip_prefix(): check const match between in and out paramsJeff King, Mar 31, 2026
  16. Phillip WoodApr 1, 2026
  17. Phillip WoodApr 1, 2026
  18. Jeff KingApr 1, 2026
  19. Junio C HamanoApr 1, 2026
  20. Phillip WoodApr 2, 2026
  21. Patrick SteinhardtApr 1, 2026
  22. 09/12 pkt-line: make packet_reader.line non-constJeff King, Mar 31, 2026
  23. Junio C HamanoApr 1, 2026
  24. Jeff KingApr 2, 2026
  25. 10/12 range-diff: drop const to fix strstr() warningsJeff King, Mar 31, 2026
  26. 11/12 http: drop const to fix strstr() warningJeff King, Mar 31, 2026
  27. 12/12 refs/files-backend: drop const to fix strchr() warningJeff King, Mar 31, 2026
  28. Patrick SteinhardtApr 1, 2026
  29. Junio C HamanoApr 1, 2026
  30. Jeff KingApr 2, 2026
  31. 0/12 fixing the remainder of the C23 strchr warningsJeff King, Apr 2, 2026
  32. 01/12 convert: add const to fix strchr() warningsJeff King, Apr 2, 2026
  33. 02/12 http: add const to fix strchr() warningsJeff King, Apr 2, 2026
  34. 03/12 transport-helper: drop const to fix strchr() warningsJeff King, Apr 2, 2026
  35. 04/12 pager: explicitly cast away strchr() constnessJeff King, Apr 2, 2026
  36. 05/12 run-command: explicitly cast away constness when assigning to voidJeff King, Apr 2, 2026
  37. 06/12 find_last_dir_sep(): convert inline function to macroJeff King, Apr 2, 2026
  38. 07/12 pseudo-merge: fix disk reads from find_pseudo_merge()Jeff King, Apr 2, 2026
  39. 08/12 skip_prefix(): check const match between in and out paramsJeff King, Apr 2, 2026
  40. Junio C HamanoApr 2, 2026
  41. Jeff KingApr 2, 2026
  42. Junio C HamanoApr 2, 2026
  43. Junio C HamanoApr 2, 2026
  44. Toon ClaesApr 3, 2026
  45. 13/12 git-compat-util: fix CONST_OUTPARAM typo and indentationJeff King, Apr 4, 2026
  46. 09/12 pkt-line: make packet_reader.line non-constJeff King, Apr 2, 2026
  47. 10/12 range-diff: drop const to fix strstr() warningsJeff King, Apr 2, 2026
  48. 11/12 http: drop const to fix strstr() warningJeff King, Apr 2, 2026
  49. 12/12 refs/files-backend: drop const to fix strchr() warningJeff King, Apr 2, 2026
  50. Toon ClaesApr 3, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.