Debian Codegraph @

t_safe_arg.c

raw symbols SCIP
1 /* Unit test for KI-54: safe_arg() in filename mode (opt==NULL) must not leak an
2 * uninitialized heap byte. The escape counter and the writer disagreed on a
3 * backslash before a wildcard / a trailing backslash, leaving a gap that
4 * strlen() walks into. We poison the heap first so the leaked byte is a
5 * deterministic non-NUL; then any extra byte makes the output differ from the
6 * expected exact quoting. Exits 0 if all outputs are exact, 1 otherwise. */
7
8 /* We link the real rsync objects (see the Makefile), so safe_arg() and all its
9 * globals come from options.o/exclude.o/etc; we only declare what we touch. */
10 #include "rsync.h"
11
12 #include <stdio.h>
13
14 extern char *safe_arg(const char *opt, const char *arg);
15 extern int protect_args, old_style_args, am_sender, relative_paths;
16 extern int trust_sender_args;
17
18 static const struct { const char *arg, *exp; } cases[] = {
19 { "\\*", "\\*" }, /* backslash+wildcard: NOT doubled */
20 { "\\?", "\\?" },
21 { "\\[", "\\[" },
22 { "\\", "\\\\" }, /* trailing backslash: doubled (NUL-footgun case) */
23 { "\\*\\?", "\\*\\?" }, /* two suppressed backslashes */
24 { "a\\*b", "a\\*b" },
25 { "\\a", "\\\\a" }, /* backslash+non-wildcard: doubled */
26 };
27
28 int main(int argc, char *argv[])
29 {
30 (void)argc; (void)argv;
31 int i, fails = 0, n = (int)(sizeof cases / sizeof cases[0]);
32
33 protect_args = 0; old_style_args = 0; am_sender = 1;
34 relative_paths = 0; trust_sender_args = 1;
35
36 /* Poison the heap so an uninitialized byte reads as 0xbe, not a lucky NUL. */
37 for (i = 0; i < 256; i++) {
38 void *p = malloc(64);
39 if (p) { memset(p, 0xbe, 64); free(p); }
40 }
41
42 for (i = 0; i < n; i++) {
43 char *r = safe_arg(NULL, cases[i].arg);
44 if (!r || strcmp(r, cases[i].exp) != 0) {
45 printf("FAIL: safe_arg(NULL, \"%s\") = \"%s\" (len %zu), expected \"%s\"\n",
46 cases[i].arg, r ? r : "(null)", r ? strlen(r) : 0, cases[i].exp);
47 fails++;
48 }
49 }
50 if (fails) {
51 printf("safe_arg: %d case(s) wrong -- uninitialized-byte leak / miscount\n", fails);
52 return 1;
53 }
54 printf("safe_arg: filename-mode quoting is exact (no uninit byte)\n");
55 return 0;
56 }