strace/tests/print_quoted_string.c
Dmitry V. Levin de484eced5 Introduce print_quoted_cstring
In many places where kernel expects a NUL-terminated string of length
up to a known fixed limit, e.g. when a NUL-terminated string is
a fixed-size field of a structure, strace does not print the last byte
assuming it is NUL, which is not always the case.

Change output format for such strings to distinguish NUL-terminated
strings from non-NUL-terminated ones: append ellipsis to the output
when the string is not NUL-terminated.

* defs.h (print_quoted_cstring): New prototype.
* util.c (print_quoted_cstring): New function.
(printpathn): Use it instead of print_quoted_string with
QUOTE_0_TERMINATED argument.
* print_fields.h (PRINT_FIELD_CSTRING): Likewise.
* btrfs.c (btrfs_ioctl): Likewise.
* dirent.c (SYS_FUNC(getdents)): Likewise.
* dirent64.c (SYS_FUNC(getdents64)): Likewise.
* print_ifindex.c (print_ifindex): Likewise.
* sysmips.c (SYS_FUNC(sysmips)): Likewise.
* ubi.c (ubi_ioctl): Likewise.
* tests/tests.h (print_quoted_cstring): New prototype.
* tests/print_quoted_string.c (print_quoted_cstring): New function.
* tests/ioctl_block.c (main): Update expected output.
* tests/ioctl_dm.c (main): Likewise.
* tests/ioctl_loop.c (print_loop_info, print_loop_info64): Likewise.
* tests/netlink_crypto.c (test_crypto_msg_newalg): Likewise.
2017-07-24 12:10:54 +00:00

102 lines
1.7 KiB
C

#include "tests.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
* Based on string_quote() from util.c.
* Assumes instr is NUL-terminated.
*/
void
print_quoted_string(const char *instr)
{
print_quoted_memory(instr, strlen(instr));
}
void
print_quoted_cstring(const char *instr, const size_t size)
{
const size_t len = strnlen(instr, size);
if (len < size) {
print_quoted_memory(instr, len);
} else {
print_quoted_memory(instr, size - 1);
printf("...");
}
}
void
print_quoted_memory(const void *const instr, const size_t len)
{
const unsigned char *str = (const unsigned char *) instr;
size_t i;
putchar('"');
for (i = 0; i < len; ++i) {
const int c = str[i];
switch (c) {
case '\"':
printf("\\\"");
break;
case '\\':
printf("\\\\");
break;
case '\f':
printf("\\f");
break;
case '\n':
printf("\\n");
break;
case '\r':
printf("\\r");
break;
case '\t':
printf("\\t");
break;
case '\v':
printf("\\v");
break;
default:
if (c >= ' ' && c <= 0x7e)
putchar(c);
else {
putchar('\\');
char c1 = '0' + (c & 0x7);
char c2 = '0' + ((c >> 3) & 0x7);
char c3 = '0' + (c >> 6);
if (*str >= '0' && *str <= '9') {
/* Print \octal */
putchar(c3);
putchar(c2);
} else {
/* Print \[[o]o]o */
if (c3 != '0')
putchar(c3);
if (c3 != '0' || c2 != '0')
putchar(c2);
}
putchar(c1);
}
break;
}
}
putchar('"');
}
void
print_quoted_hex(const void *const instr, const size_t len)
{
const unsigned char *str = instr;
size_t i;
printf("\"");
for (i = 0; i < len; i++)
printf("\\x%02x", str[i]);
printf("\"");
}