https://gcc.gnu.org/bugzilla/show_bug.cgi?id=120026
Bug ID: 120026 Summary: ICE (Segmentation fault) with -O3 when using ternary operator to assign strdup to function pointer Product: gcc Version: 13.3.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c Assignee: unassigned at gcc dot gnu.org Reporter: mario.rodriguezb1 at um dot es Target Milestone: --- The following C code triggers a segmentation fault and an internal compiler error (ICE) in GCC when compiled with -O3. The issue occurs due to assigning a function with mismatched signature (strdup) to a void (*)(char *, char *, int) function pointer via a ternary operator. ``` #include <stdlib.h> #include <string.h> int main() { char *mem1, *mem2; mem1 = (char *) malloc(3 * sizeof(char)); mem2 = (char *) malloc(3 * sizeof(char)); void (*op1)(char *, char *, int); int i = 2; op1 = (i > 0)? strdup : strcpy; if (mem1!= NULL && mem2!= NULL) { op1(mem1, mem2, sizeof(char)); } for (int i = 0; i < 2; i++) rand(); } ``` To reproduce quickly: it has been seen that it occurrs from version 12.1 to 13.3 only. https://gcc.godbolt.org/z/Pf453nsh6