diff --git a/programs/escape.c b/programs/escape.c --- a/programs/escape.c +++ b/programs/escape.c @@ -23,6 +23,29 @@ #include "common.h" #include "escape.h" +#include + +/* ESCAPE-INVARIANT check (scripts/escape_invariant.diff, instrumentation + * only): strcat (d, s) appends s at the first NUL at or after d, and the + * escape loops then advance d by strlen (s), which is right only if the + * output is NUL-terminated at d. Report and exit before a strcat whose + * destination is not terminated at d. */ +static char * +escape_checked_strcat (char *d, const char *s, const char *func) +{ + if (*d != 0) + { + fprintf (stderr, + "ESCAPE-INVARIANT output not NUL-terminated at the strcat " + "position in %s (byte 0x%02x)\n", + func, (unsigned char)*d); + fflush (stderr); + _exit (86); + } + return strcat (d, s); +} +#define strcat(d, s) escape_checked_strcat ((d), (s), __func__) + char * ATTRIBUTE_MALLOC htmlescape (const char *restrict src, const int cp) {