Skip to content

Commit

Permalink
Use PCRE2_UNREACHABLE assertion for unreachable points in code
Browse files Browse the repository at this point in the history
  • Loading branch information
alexdowad committed Aug 27, 2024
1 parent a769e1a commit 712a651
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 46 deletions.
9 changes: 5 additions & 4 deletions src/pcre2_auto_possess.c
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,8 @@ switch(ptype)
default:
return (PRIV(ucp_gentype)[prop->chartype] == ucp_Z) == negated;
}
break; /* Control never reaches here */
PCRE2_UNREACHABLE(); /* Control never reaches here */
break;

case PT_WORD:
return (PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
Expand All @@ -259,7 +260,8 @@ switch(ptype)
if (c < *p) return !negated;
if (c == *p++) return negated;
}
break; /* Control never reaches here */
PCRE2_UNREACHABLE(); /* Control never reaches here */
break;

/* Haven't yet thought these through. */

Expand Down Expand Up @@ -1119,8 +1121,7 @@ for(;;)
if (list[1] == 0) return TRUE;
}

/* Control never reaches here. There used to be a fail-save return FALSE; here,
but some compilers complain about an unreachable statement. */
PCRE2_UNREACHABLE(); /* Control never reaches here */
}


Expand Down
12 changes: 7 additions & 5 deletions src/pcre2_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -5233,7 +5233,8 @@ for (;;)
return code;
}
}
/* Control never reaches here */

PCRE2_UNREACHABLE(); /* Control never reaches here */
}


Expand Down Expand Up @@ -8482,7 +8483,7 @@ for (;; pptr++)
} /* End of big switch */
} /* End of big loop */

/* Control never reaches here. */
PCRE2_UNREACHABLE(); /* Control never reaches here */
}


Expand Down Expand Up @@ -8787,7 +8788,8 @@ for (;;)
lookbehindlength = META_DATA(*pptr);
pptr++;
}
/* Control never reaches here */

PCRE2_UNREACHABLE(); /* Control never reaches here */
}


Expand Down Expand Up @@ -9472,8 +9474,8 @@ for (;; pptr++)
if (meta >= sizeof(meta_extra_lengths)) return NULL;
pptr += meta_extra_lengths[meta];
}
/* Control never reaches here */
return pptr;

PCRE2_UNREACHABLE(); /* Control never reaches here */
}


Expand Down
4 changes: 1 addition & 3 deletions src/pcre2_convert.c
Original file line number Diff line number Diff line change
Expand Up @@ -1158,9 +1158,7 @@ for (i = 0; i < 2; i++)
use_length = *bufflenptr + 1;
}

/* Control should never get here. */

return PCRE2_ERROR_INTERNAL;
PCRE2_UNREACHABLE(); /* Control never reaches here */
}


Expand Down
78 changes: 49 additions & 29 deletions src/pcre2_match.c
Original file line number Diff line number Diff line change
Expand Up @@ -1329,7 +1329,7 @@ fprintf(stderr, "++ %2ld op=%3d %s\n", Fecode - mb->start_code, *Fecode,
RRETURN(MATCH_NOMATCH);
}
}
/* Control never gets here */
PCRE2_UNREACHABLE(); /* Control never reaches here */
}

else /* Maximize */
Expand Down Expand Up @@ -1430,7 +1430,7 @@ fprintf(stderr, "++ %2ld op=%3d %s\n", Fecode - mb->start_code, *Fecode,
if (Lc != cc && Loc != cc) RRETURN(MATCH_NOMATCH);
Feptr++;
}
/* Control never gets here */
PCRE2_UNREACHABLE(); /* Control never reaches here */
}

else /* Maximize */
Expand Down Expand Up @@ -1488,7 +1488,7 @@ fprintf(stderr, "++ %2ld op=%3d %s\n", Fecode - mb->start_code, *Fecode,
}
if (Lc != UCHAR21INCTEST(Feptr)) RRETURN(MATCH_NOMATCH);
}
/* Control never gets here */
PCRE2_UNREACHABLE(); /* Control never reaches here */
}
else /* Maximize */
{
Expand Down Expand Up @@ -1706,7 +1706,7 @@ fprintf(stderr, "++ %2ld op=%3d %s\n", Fecode - mb->start_code, *Fecode,
Feptr++;
}
}
/* Control never gets here */
PCRE2_UNREACHABLE(); /* Control never reaches here */
}

/* Maximize case */
Expand Down Expand Up @@ -1844,7 +1844,7 @@ fprintf(stderr, "++ %2ld op=%3d %s\n", Fecode - mb->start_code, *Fecode,
if (Lc == *Feptr++) RRETURN(MATCH_NOMATCH);
}
}
/* Control never gets here */
PCRE2_UNREACHABLE(); /* Control never reaches here */
}

/* Maximize case */
Expand Down Expand Up @@ -2071,7 +2071,7 @@ fprintf(stderr, "++ %2ld op=%3d %s\n", Fecode - mb->start_code, *Fecode,
if ((Lbyte_map[fc/8] & (1u << (fc&7))) == 0) RRETURN(MATCH_NOMATCH);
}
}
/* Control never gets here */
PCRE2_UNREACHABLE(); /* Control never reaches here */
}

/* If maximizing, find the longest possible run, then work backwards. */
Expand Down Expand Up @@ -2151,7 +2151,8 @@ fprintf(stderr, "++ %2ld op=%3d %s\n", Fecode - mb->start_code, *Fecode,
RRETURN(MATCH_NOMATCH);
}
}
/* Control never gets here */

PCRE2_UNREACHABLE(); /* Control never reaches here */

#undef Lbyte_map_address
#undef Lbyte_map
Expand Down Expand Up @@ -2244,7 +2245,7 @@ fprintf(stderr, "++ %2ld op=%3d %s\n", Fecode - mb->start_code, *Fecode,
GETCHARINCTEST(fc, Feptr);
if (!PRIV(xclass)(fc, Lxclass_data, utf)) RRETURN(MATCH_NOMATCH);
}
/* Control never gets here */
PCRE2_UNREACHABLE(); /* Control never reaches here */
}

/* If maximizing, find the longest possible run, then work backwards. */
Expand Down Expand Up @@ -2287,7 +2288,7 @@ fprintf(stderr, "++ %2ld op=%3d %s\n", Fecode - mb->start_code, *Fecode,
RRETURN(MATCH_NOMATCH);
}

/* Control never gets here */
PCRE2_UNREACHABLE(); /* Control never reaches here */
}
#endif /* SUPPORT_WIDE_CHARS: end of XCLASS */

Expand Down Expand Up @@ -3530,7 +3531,8 @@ fprintf(stderr, "++ %2ld op=%3d %s\n", Fecode - mb->start_code, *Fecode,
GETCHARINCTEST(fc, Feptr);
if (Lctype == OP_NOTPROP) RRETURN(MATCH_NOMATCH);
}
/* Control never gets here */

PCRE2_UNREACHABLE(); /* Control never reaches here */

case PT_LAMP:
for (;;)
Expand All @@ -3551,7 +3553,8 @@ fprintf(stderr, "++ %2ld op=%3d %s\n", Fecode - mb->start_code, *Fecode,
chartype == ucp_Lt) == (Lctype == OP_NOTPROP))
RRETURN(MATCH_NOMATCH);
}
/* Control never gets here */

PCRE2_UNREACHABLE(); /* Control never reaches here */

case PT_GC:
for (;;)
Expand All @@ -3568,7 +3571,8 @@ fprintf(stderr, "++ %2ld op=%3d %s\n", Fecode - mb->start_code, *Fecode,
if ((UCD_CATEGORY(fc) == Lpropvalue) == (Lctype == OP_NOTPROP))
RRETURN(MATCH_NOMATCH);
}
/* Control never gets here */

PCRE2_UNREACHABLE(); /* Control never reaches here */

case PT_PC:
for (;;)
Expand All @@ -3585,7 +3589,8 @@ fprintf(stderr, "++ %2ld op=%3d %s\n", Fecode - mb->start_code, *Fecode,
if ((UCD_CHARTYPE(fc) == Lpropvalue) == (Lctype == OP_NOTPROP))
RRETURN(MATCH_NOMATCH);
}
/* Control never gets here */

PCRE2_UNREACHABLE(); /* Control never reaches here */

case PT_SC:
for (;;)
Expand All @@ -3602,7 +3607,8 @@ fprintf(stderr, "++ %2ld op=%3d %s\n", Fecode - mb->start_code, *Fecode,
if ((UCD_SCRIPT(fc) == Lpropvalue) == (Lctype == OP_NOTPROP))
RRETURN(MATCH_NOMATCH);
}
/* Control never gets here */

PCRE2_UNREACHABLE(); /* Control never reaches here */

case PT_SCX:
for (;;)
Expand All @@ -3624,7 +3630,8 @@ fprintf(stderr, "++ %2ld op=%3d %s\n", Fecode - mb->start_code, *Fecode,
if (ok == (Lctype == OP_NOTPROP))
RRETURN(MATCH_NOMATCH);
}
/* Control never gets here */

PCRE2_UNREACHABLE(); /* Control never reaches here */

case PT_ALNUM:
for (;;)
Expand All @@ -3643,7 +3650,8 @@ fprintf(stderr, "++ %2ld op=%3d %s\n", Fecode - mb->start_code, *Fecode,
if ((category == ucp_L || category == ucp_N) == (Lctype == OP_NOTPROP))
RRETURN(MATCH_NOMATCH);
}
/* Control never gets here */

PCRE2_UNREACHABLE(); /* Control never reaches here */

/* Perl space used to exclude VT, but from Perl 5.18 it is included,
which means that Perl space and POSIX space are now identical. PCRE
Expand Down Expand Up @@ -3675,7 +3683,8 @@ fprintf(stderr, "++ %2ld op=%3d %s\n", Fecode - mb->start_code, *Fecode,
break;
}
}
/* Control never gets here */

PCRE2_UNREACHABLE(); /* Control never reaches here */

case PT_WORD:
for (;;)
Expand All @@ -3698,7 +3707,8 @@ fprintf(stderr, "++ %2ld op=%3d %s\n", Fecode - mb->start_code, *Fecode,
chartype == ucp_Pc) == (Lctype == OP_NOTPROP))
RRETURN(MATCH_NOMATCH);
}
/* Control never gets here */

PCRE2_UNREACHABLE(); /* Control never reaches here */

case PT_CLIST:
for (;;)
Expand Down Expand Up @@ -3735,7 +3745,8 @@ fprintf(stderr, "++ %2ld op=%3d %s\n", Fecode - mb->start_code, *Fecode,
}
}
}
/* Control never gets here */

PCRE2_UNREACHABLE(); /* Control never reaches here */

case PT_UCNC:
for (;;)
Expand All @@ -3754,7 +3765,8 @@ fprintf(stderr, "++ %2ld op=%3d %s\n", Fecode - mb->start_code, *Fecode,
fc >= 0xe000) == (Lctype == OP_NOTPROP))
RRETURN(MATCH_NOMATCH);
}
/* Control never gets here */

PCRE2_UNREACHABLE(); /* Control never reaches here */

case PT_BIDICL:
for (;;)
Expand All @@ -3771,7 +3783,8 @@ fprintf(stderr, "++ %2ld op=%3d %s\n", Fecode - mb->start_code, *Fecode,
if ((UCD_BIDICLASS(fc) == Lpropvalue) == (Lctype == OP_NOTPROP))
RRETURN(MATCH_NOMATCH);
}
/* Control never gets here */

PCRE2_UNREACHABLE(); /* Control never reaches here */

case PT_BOOL:
for (;;)
Expand All @@ -3793,7 +3806,8 @@ fprintf(stderr, "++ %2ld op=%3d %s\n", Fecode - mb->start_code, *Fecode,
if (ok == (Lctype == OP_NOTPROP))
RRETURN(MATCH_NOMATCH);
}
/* Control never gets here */

PCRE2_UNREACHABLE(); /* Control never reaches here */

/* This should never occur */
default:
Expand Down Expand Up @@ -4099,7 +4113,8 @@ fprintf(stderr, "++ %2ld op=%3d %s\n", Fecode - mb->start_code, *Fecode,
}
}
}
/* Control never gets here */

PCRE2_UNREACHABLE(); /* Control never reaches here */
}

/* If maximizing, it is worth using inline code for speed, doing the type
Expand Down Expand Up @@ -5111,7 +5126,7 @@ fprintf(stderr, "++ %2ld op=%3d %s\n", Fecode - mb->start_code, *Fecode,
}
Feptr += slength;
}
/* Control never gets here */
PCRE2_UNREACHABLE(); /* Control never reaches here */
}

/* If maximizing, find the longest string and work backwards, as long as
Expand Down Expand Up @@ -5185,7 +5200,8 @@ fprintf(stderr, "++ %2ld op=%3d %s\n", Fecode - mb->start_code, *Fecode,

RRETURN(MATCH_NOMATCH);
}
/* Control never gets here */

PCRE2_UNREACHABLE(); /* Control never reaches here */

#undef Lcaseless
#undef Lmin
Expand Down Expand Up @@ -5409,7 +5425,8 @@ fprintf(stderr, "++ %2ld op=%3d %s\n", Fecode - mb->start_code, *Fecode,
Fecode += GET(Fecode, 1);
if (*Fecode != OP_ALT) RRETURN(MATCH_NOMATCH);
}
/* Control never reaches here. */

PCRE2_UNREACHABLE(); /* Control never reaches here */

#undef Lframe_type

Expand Down Expand Up @@ -5494,7 +5511,8 @@ fprintf(stderr, "++ %2ld op=%3d %s\n", Fecode - mb->start_code, *Fecode,
Lstart_branch = next_ecode;
if (*Lstart_branch != OP_ALT) RRETURN(MATCH_NOMATCH);
}
/* Control never reaches here. */

PCRE2_UNREACHABLE(); /* Control never reaches here */

#undef Lframe_type
#undef Lstart_branch
Expand Down Expand Up @@ -5959,7 +5977,8 @@ fprintf(stderr, "++ %2ld op=%3d %s\n", Fecode - mb->start_code, *Fecode,
if (utf) { FORWARDCHARTEST(Feptr, mb->end_subject); }
#endif
}
/* Control never reaches here */

PCRE2_UNREACHABLE(); /* Control never reaches here */

#undef Lmin
#undef Lmax
Expand Down Expand Up @@ -6544,7 +6563,8 @@ fprintf(stderr, "++ %2ld op=%3d %s\n", Fecode - mb->start_code, *Fecode,
loop. */

} /* End of main loop */
/* Control never reaches here */

PCRE2_UNREACHABLE(); /* Control never reaches here */


/* ========================================================================= */
Expand Down
3 changes: 2 additions & 1 deletion src/pcre2_study.c
Original file line number Diff line number Diff line change
Expand Up @@ -758,7 +758,8 @@ for (;;)
return -3;
}
}
/* Control never gets here */

PCRE2_UNREACHABLE(); /* Control never reaches here */
}


Expand Down
5 changes: 3 additions & 2 deletions src/pcre2grep.c
Original file line number Diff line number Diff line change
Expand Up @@ -900,7 +900,8 @@ for (;;)
if (strcmp(dent->d_name, ".") != 0 && strcmp(dent->d_name, "..") != 0)
return dent->d_name;
}
/* Control never reaches here */

PCRE2_UNREACHABLE(); /* Control never reaches here */
}

static void
Expand Down Expand Up @@ -1695,7 +1696,7 @@ switch(endlinetype)
while (p > startptr && p[-1] != '\n') p--;
if (p <= startptr + 1 || p[-2] == '\r') return p;
}
/* Control can never get here */
PCRE2_UNREACHABLE(); /* Control never reaches here */

case PCRE2_NEWLINE_ANY:
case PCRE2_NEWLINE_ANYCRLF:
Expand Down
Loading

0 comments on commit 712a651

Please sign in to comment.