Hardened_SEC-Bench / scripts /escape_invariant.diff
shawnnnliu's picture
Add libredwg.cve-2020-21816 to the verified config (part 3)
b2265c0 verified
Raw History Blame Contribute Delete
1.05 kB
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 <unistd.h>
+
+/* 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)
{