3 @@ -407,6 +407,12 @@ unsigned long uid;
5 static char path[MAXPATHLEN + 1];
8 + * Quote string `instr' of length `size'
9 + * Write up to (3 + `size' * 4) bytes to `outstr' buffer.
10 + * If `len' < 0, treat `instr' as a NUL-terminated string
11 + * and quote at most (`size' - 1) bytes.
14 string_quote(const char *instr, char *outstr, int len, int size)
16 @@ -417,12 +423,18 @@ string_quote(const char *instr, char *outstr, int len, int size)
20 + /* Check for presence of symbol which require
21 + to hex-quote the whole string. */
22 for (i = 0; i < size; ++i) {
24 - if (len < 0 && i == size - 2 && c != '\0')
26 - if (len < 0 && c == '\0')
28 + /* Check for NUL-terminated string. */
32 + /* Quote at most size - 1 bytes. */
36 if (!isprint(c) && !isspace(c)) {
39 @@ -433,20 +445,31 @@ string_quote(const char *instr, char *outstr, int len, int size)
43 + /* Hex-quote the whole string. */
44 for (i = 0; i < size; ++i) {
46 - if (len < 0 && c == '\0')
48 + /* Check for NUL-terminated string. */
52 + /* Quote at most size - 1 bytes. */
56 sprintf(s, "\\x%02x", c);
60 for (i = 0; i < size; ++i) {
62 - if (len < 0 && i == size - 2 && c != '\0')
64 - if (len < 0 && c == '\0')
66 + /* Check for NUL-terminated string. */
70 + /* Quote at most size - 1 bytes. */
77 @@ -495,18 +518,25 @@ string_quote(const char *instr, char *outstr, int len, int size)
82 + * Print path string specified by address `addr' and length `n'.
83 + * If path length exceeds `n', append `...' to the output.
86 printpathn(struct tcb *tcp, long addr, int n)
88 - if (n > sizeof path - 1)
89 - n = sizeof path - 1;
97 + /* Cap path length to the path buffer size,
98 + and NUL-terminate the buffer. */
99 + if (n > sizeof path - 1)
100 + n = sizeof path - 1;
103 + /* Fetch one byte more to find out whether path length > n. */
104 if (umovestr(tcp, addr, n + 1, path) < 0)
105 tprintf("%#lx", addr);
107 @@ -515,7 +545,8 @@ printpathn(struct tcb *tcp, long addr, int n)
111 - if (string_quote(path, outstr, -1, n + 1) || trunc)
112 + (void) string_quote(path, outstr, -1, n + 1);
114 strcat(outstr, "...");
115 tprintf("%s", outstr);
117 @@ -527,6 +558,11 @@ printpath(struct tcb *tcp, long addr)
118 printpathn(tcp, addr, sizeof path - 1);
122 + * Print string specified by address `addr' and length `len'.
123 + * If `len' < 0, treat the string as a NUL-terminated string.
124 + * If string length exceeds `max_strlen', append `...' to the output.
127 printstr(struct tcb *tcp, long addr, int len)
129 @@ -538,32 +574,39 @@ printstr(struct tcb *tcp, long addr, int len)
134 - if ((str = malloc(max_strlen + 1)) == NULL
135 - || (outstr = malloc(4*max_strlen
136 - + sizeof "\"\"...")) == NULL) {
137 - fprintf(stderr, "out of memory\n");
138 - tprintf("%#lx", addr);
141 + /* Allocate static buffers if they are not allocated yet. */
143 + str = malloc(max_strlen + 1);
145 + outstr = malloc(4 * max_strlen + sizeof "\"...\"");
146 + if (!str || !outstr) {
147 + fprintf(stderr, "out of memory\n");
148 + tprintf("%#lx", addr);
154 + * Treat as a NUL-terminated string: fetch one byte more
155 + * because string_quote() quotes one byte less.
157 size = max_strlen + 1;
158 + str[max_strlen] = '\0';
159 if (umovestr(tcp, addr, size, str) < 0) {
160 tprintf("%#lx", addr);
165 - size = MIN(len, max_strlen + 1);
166 + size = MIN(len, max_strlen);
167 if (umoven(tcp, addr, size, str) < 0) {
168 tprintf("%#lx", addr);
173 - if (string_quote(str, outstr, len, size))
174 + if (string_quote(str, outstr, len, size) &&
175 + (len < 0 || len > max_strlen))
176 strcat(outstr, "...");
178 tprintf("%s", outstr);