Incompatible declarations of pg_reg_getcolor() in regcomp.c and regexec.c
Incompatible declarations of pg_reg_getcolor() in regcomp.c and regexec.c
Hi, This is a source-level problem with no user-visible effect, so there is no SQL to reproduce it, but clang can show it (see below). The regex compiler and executor declare the external function pg_reg_getcolor() with incompatible types, which is undefined behavior under C17 6.2.7p2: - regguts.h:563 declares extern color pg_reg_getcolor(struct colormap *cm, chr c); and both regcomp.c and regexec.c include regguts.h. regcomp.c defines the function (via regc_color.c), and regexec.c calls it through GETCOLOR() in rege_dfa.c. - struct colormap (regguts.h:236) has a member "struct vars *v" (regguts.h:240). - regcomp.c:281 and regexec.c:106 each define their own, unrelated struct vars. So struct colormap, and with it the type of pg_reg_getcolor(), differs between the two translation units. Compilers accept this, but tools that check declarations across translation units may reject it. (I ran into this issue while processing PostgreSQL with a C front end developed in the ACL2 theorem prover.) It dates from commit c54159d44ce (PostgreSQL 10), which made pg_reg_getcolor() external, and is still present on master (72b9498). To reproduce it with clang, run these from src/backend/regex in a configured tree: clang -I../../../src/include -emit-ast regcomp.c regexec.c clang -cc1 -ast-merge regcomp.ast -ast-merge regexec.ast -fsyntax-only -x c /dev/null Among other things, this reports: regguts.h:563:14: warning: external function 'pg_reg_getcolor' declared with incompatible types in different translation units [-Wodr] The other -Wodr warnings, about struct types that differ between translation units, are not problems in themselves in C. Suggested fix (attached): rename the executor's struct to struct exec_vars in regexec.c and rege_dfa.c. The struct vars pointer in struct colormap exists only for error reporting during compilation, and the executor never uses it, so struct vars simply stays incomplete in regexec.c. It is a pure rename. On master with clang, the regex object files are byte-identical before and after, make check and test_regex pass, and the AST merge above reports nothing. Thanks, Grant Jurgensen
Re: Incompatible declarations of pg_reg_getcolor() in regcomp.c and regexec.c
От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
Grant Jurgensen writes: > The regex compiler and executor declare the external function > pg_reg_getcolor() with incompatible types, which is undefined behavior > under C17 6.2.7p2: Since pg_reg_getcolor doesn't touch the colormap.v field, I'm fairly confused about where there is any actual undefined behavior. regards, tom lane
Re: Incompatible declarations of pg_reg_getcolor() in regcomp.c and regexec.c
От:
Grant Jurgensen <grant@kestrel.edu>
Дата:
Hi Tom, Thanks for taking a look. To address your question, the C17 standard (6.2.7 p2) says: All declarations that refer to the same object or function shall have compatible type; otherwise, the behavior is undefined. The undefined behavior comes from the declarations themselves, not from any access to the field. Other versions of the standard say the same thing. In practice you're right: neither pg_reg_getcolor() nor the executor ever touches colormap.v, so there is no observable issue, and my suggested rename doesn't change the compiled code at all. But it does remove behavior that is technically undefined, which matters for whole-program analysis tools that have to reconcile the two declarations of the same external function. And it's an easy fix. Thanks, -Grant On 11/6/26 2:32 PM, Tom Lane wrote: > Grant Jurgensen writes: >> The regex compiler and executor declare the external function >> pg_reg_getcolor() with incompatible types, which is undefined behavior >> under C17 6.2.7p2: > Since pg_reg_getcolor doesn't touch the colormap.v field, I'm > fairly confused about where there is any actual undefined behavior. > > regards, tom lane