]> granicus.if.org Git - strace/commitdiff
Introduce print_quoted_cstring
authorDmitry V. Levin <ldv@altlinux.org>
Mon, 24 Jul 2017 12:10:54 +0000 (12:10 +0000)
committerDmitry V. Levin <ldv@altlinux.org>
Mon, 24 Jul 2017 12:10:54 +0000 (12:10 +0000)
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.

15 files changed:
btrfs.c
defs.h
dirent.c
dirent64.c
print_fields.h
print_ifindex.c
sysmips.c
tests/ioctl_block.c
tests/ioctl_dm.c
tests/ioctl_loop.c
tests/netlink_crypto.c
tests/print_quoted_string.c
tests/tests.h
ubi.c
util.c

diff --git a/btrfs.c b/btrfs.c
index 8bffba8b6604072dcb4b79a0a9e3249607d5465d..3c48e798f8703e713278936dcbf245547c5d1e1b 100644 (file)
--- a/btrfs.c
+++ b/btrfs.c
@@ -680,14 +680,12 @@ MPERS_PRINTER_DECL(int, btrfs_ioctl,
                                   (uint64_t) args.start.cont_reading_from_srcdev_mode);
 
                                str = (const char *) args.start.srcdev_name;
-                               print_quoted_string(str,
-                                               sizeof(args.start.srcdev_name),
-                                               QUOTE_0_TERMINATED);
+                               print_quoted_cstring(str,
+                                               sizeof(args.start.srcdev_name));
                                tprints(", tgtdev_name=");
                                str = (const char *) args.start.tgtdev_name;
-                               print_quoted_string(str,
-                                               sizeof(args.start.tgtdev_name),
-                                               QUOTE_0_TERMINATED);
+                               print_quoted_cstring(str,
+                                               sizeof(args.start.tgtdev_name));
                                tprints("}");
 
                        }
@@ -1327,7 +1325,7 @@ MPERS_PRINTER_DECL(int, btrfs_ioctl,
                tprints(", ");
                if (umove_or_printaddr(tcp, arg, &label))
                        break;
-               print_quoted_string(label, sizeof(label), QUOTE_0_TERMINATED);
+               print_quoted_cstring(label, sizeof(label));
                break;
        }
 
diff --git a/defs.h b/defs.h
index 539afa2bc098d6c129d0f8e1094bb0f516be0660..8aca80833ecbaea7a7fb352447ce95b031aaa0a3 100644 (file)
--- a/defs.h
+++ b/defs.h
@@ -556,6 +556,7 @@ str_strip_prefix_len(const char *str, const char *prefix, size_t prefix_len)
 
 extern int string_quote(const char *, char *, unsigned int, unsigned int);
 extern int print_quoted_string(const char *, unsigned int, unsigned int);
+extern int print_quoted_cstring(const char *, unsigned int);
 
 /* a refers to the lower numbered u_arg,
  * b refers to the higher numbered u_arg
index 71963ec3e5607d1ba8bfae911b29a98bc81e3afb..2ab626bf7819a8104a84dede0052945eac4c6118 100644 (file)
--- a/dirent.c
+++ b/dirent.c
@@ -132,10 +132,7 @@ SYS_FUNC(getdents)
                                zero_extend_signed_to_ull(d->d_off),
                                d->d_reclen);
 
-                       if (print_quoted_string(d->d_name, d_name_len,
-                                               QUOTE_0_TERMINATED) > 0) {
-                               tprints("...");
-                       }
+                       print_quoted_cstring(d->d_name, d_name_len);
 
                        tprints(", d_type=");
                        if (oob)
index d54cb9e7bb7f896f3519916a4d99447816d31706..4172d649a0801f8f424d85af991a59ea0267aee2 100644 (file)
@@ -105,10 +105,7 @@ SYS_FUNC(getdents64)
                        printxval(dirent_types, d->d_type, "DT_???");
 
                        tprints(", d_name=");
-                       if (print_quoted_string(d->d_name, d_name_len,
-                                               QUOTE_0_TERMINATED) > 0) {
-                               tprints("...");
-                       }
+                       print_quoted_cstring(d->d_name, d_name_len);
 
                        tprints("}");
                }
index b6b54c04984151564ded277d4f09e259eca5b54c..0aab1d7a3baffe539fb0febee9e402cb47f53a31 100644 (file)
@@ -93,9 +93,8 @@
 #define PRINT_FIELD_CSTRING(prefix_, where_, field_)                   \
        do {                                                            \
                STRACE_PRINTF("%s%s=", (prefix_), #field_);             \
-               print_quoted_string((const char *)(where_).field_,      \
-                                   sizeof((where_).field_),            \
-                                   QUOTE_0_TERMINATED);                \
+               print_quoted_cstring((const char *)(where_).field_,     \
+                                    sizeof((where_).field_));          \
        } while (0)
 
 #define PRINT_FIELD_INET_ADDR(prefix_, where_, field_, af_)            \
index 21f68f68723ffa2acb74ef8df58aefe5f25ac05b..f978a5a650fb2b90280b3a8a20b8117d07c5852b 100644 (file)
@@ -36,7 +36,7 @@ print_ifindex(const unsigned int ifindex)
 
        if (if_indextoname(ifindex, buf)) {
                tprints("if_nametoindex(");
-               print_quoted_string(buf, sizeof(buf), QUOTE_0_TERMINATED);
+               print_quoted_cstring(buf, sizeof(buf));
                tprints(")");
                return;
        }
index a0ed557f4679b5516327a5195cbf1d349fb5f6cf..e095cb3617226a98ec32f16ea678eb2e0baa17e2 100644 (file)
--- a/sysmips.c
+++ b/sysmips.c
@@ -59,8 +59,7 @@ SYS_FUNC(sysmips)
                             nodename) < 0) {
                        printaddr(tcp->u_arg[1]);
                } else {
-                       print_quoted_string(nodename, __NEW_UTS_LEN + 1,
-                                           QUOTE_0_TERMINATED);
+                       print_quoted_cstring(nodename, __NEW_UTS_LEN + 1);
                }
                return RVAL_DECODED;
        }
index 0d518643aa92901d697b7297163fc45dff883378..2bb68085744c20cdb340871341303d887f25f843 100644 (file)
@@ -171,7 +171,7 @@ main(void)
        ioctl(-1, BLKPG, blkpg);
        printf("ioctl(-1, BLKPG, {op=%s, flags=%d, datalen=%d"
               ", data={start=%lld, length=%lld, pno=%d"
-              ", devname=\"%.*s\", volname=\"%.*s\"}})"
+              ", devname=\"%.*s\"..., volname=\"%.*s\"...}})"
               " = -1 EBADF (%m)\n",
               "BLKPG_ADD_PARTITION",
               blkpg->flags, blkpg->datalen,
index 544d70b3f524361683ddc3e7255d99abebdbed8d..2f77e041bd2972c3decf46efe0d1fe15b826f621 100644 (file)
@@ -251,7 +251,7 @@ main(void)
        strncpy(dm_arg->uuid, str129, sizeof(dm_arg->uuid));
        ioctl(-1, DM_VERSION, dm_arg);
        printf("ioctl(-1, DM_VERSION, {version=4.1.2, data_size=%zu, "
-              "dev=makedev(18, 52), name=\"%.127s\", uuid=\"%.128s\", "
+              "dev=makedev(18, 52), name=\"%.127s\"..., uuid=\"%.128s\"..., "
               "flags=0}) = -1 EBADF (%m)\n",
               min_sizeof_dm_ioctl, str129, str129);
 
index 4dcbf9ade4ccc83ea7610c6725412ecb15cae9cb..3411a4d8f60db0ca2f20cb5267a1fa1b431143e8 100644 (file)
@@ -39,6 +39,7 @@
 #include <sys/sysmacros.h>
 #include <linux/ioctl.h>
 #include <linux/loop.h>
+#include "print_fields.h"
 #include "xlat/loop_cmds.h"
 
 #ifndef ABBREV
@@ -81,8 +82,7 @@ print_loop_info(struct loop_info * const info, bool print_encrypt,
        else
                printf("%#x /* LO_FLAGS_??? */", info->lo_flags);
 
-       printf(", lo_name=\"%.*s\"",
-              (int) sizeof(info->lo_name) - 1, info->lo_name);
+       PRINT_FIELD_CSTRING(", ", *info, lo_name);
 
        if (VERBOSE || print_encrypt)
                printf(", lo_encrypt_key=\"%.*s\"",
@@ -144,17 +144,16 @@ print_loop_info64(struct loop_info64 * const info64, bool print_encrypt,
                printf("%s", flags);
        else
                printf("%#x /* LO_FLAGS_??? */", info64->lo_flags);
-       printf(", lo_file_name=\"%.*s\"",
-              (int) sizeof(info64->lo_file_name) - 1, info64->lo_file_name);
+       PRINT_FIELD_CSTRING(", ", *info64, lo_file_name);
 
-       if (VERBOSE || print_encrypt)
-               printf(", lo_crypt_name=\"%.*s\", lo_encrypt_key=\"%.*s\"",
-                      (int) sizeof(info64->lo_crypt_name) - 1,
-                      info64->lo_crypt_name,
+       if (VERBOSE || print_encrypt) {
+               PRINT_FIELD_CSTRING(", ", *info64, lo_crypt_name);
+               printf(", lo_encrypt_key=\"%.*s\"",
                       encrypt_key ? (int) strlen(encrypt_key) :
                       (int) sizeof(info64->lo_encrypt_key),
                       encrypt_key ? encrypt_key :
                       (char *) info64->lo_encrypt_key);
+       }
 
 # if VERBOSE
        printf(", lo_init=[%#" PRIx64 ", %#" PRIx64 "]}",
index 96bf4c22277bce868e66b5d577b4800b07a2d727..deb1644ef262e4fe84adf05541f55d1a6e0aa55e 100644 (file)
@@ -125,13 +125,13 @@ test_crypto_msg_newalg(const int fd)
                               printf("{cru_name=");
                               print_quoted_memory(alg.cru_name,
                                       sizeof(alg.cru_name) - 1);
-                              printf(", cru_driver_name=");
+                              printf("..., cru_driver_name=");
                               print_quoted_memory(alg.cru_driver_name,
                                       sizeof(alg.cru_driver_name) - 1);
-                              printf(", cru_module_name=");
+                              printf("..., cru_module_name=");
                               print_quoted_memory(alg.cru_module_name,
                                       sizeof(alg.cru_module_name) - 1);
-                              PRINT_FIELD_X(", ", alg, cru_type);
+                              PRINT_FIELD_X("..., ", alg, cru_type);
                               PRINT_FIELD_X(", ", alg, cru_mask);
                               PRINT_FIELD_U(", ", alg, cru_refcnt);
                               PRINT_FIELD_X(", ", alg, cru_flags);
index dcfcfa0e8e332aedcead460f7edd5e3147b3c107..2894e49bc35e8edbecdb62106adc6c01c6d8ac9f 100644 (file)
@@ -15,6 +15,18 @@ 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)
 {
index 258fbd96de1bb9f03ee6ffaf993d450d7e43c47d..6fa2f402d26d6019163bd1b6bc4b0068281f5aab 100644 (file)
@@ -134,6 +134,12 @@ unsigned long inode_of_sockfd(int);
 /* Print string in a quoted form. */
 void print_quoted_string(const char *);
 
+/*
+ * Print a NUL-terminated string `str' of length up to `size' - 1
+ * in a quoted form.
+ */
+void print_quoted_cstring(const char *str, size_t size);
+
 /* Print memory in a quoted form. */
 void print_quoted_memory(const void *, size_t);
 
diff --git a/ubi.c b/ubi.c
index 65db02dadc0996a54018976a36f2300410071529..808e4a2b854296496f66a3409c4f6b34970948f7 100644 (file)
--- a/ubi.c
+++ b/ubi.c
@@ -62,11 +62,8 @@ ubi_ioctl(struct tcb *const tcp, const unsigned int code,
                        printxval(ubi_volume_types,
                                    (uint8_t) mkvol.vol_type, "UBI_???_VOLUME");
                        tprintf(", name_len=%" PRIi16 ", name=", mkvol.name_len);
-                       if (print_quoted_string(mkvol.name,
-                                       CLAMP(mkvol.name_len, 0, UBI_MAX_VOLUME_NAME),
-                                       QUOTE_0_TERMINATED) > 0) {
-                               tprints("...");
-                       }
+                       print_quoted_cstring(mkvol.name,
+                                       CLAMP(mkvol.name_len, 0, UBI_MAX_VOLUME_NAME));
                        tprints("}");
                        return 1;
                }
@@ -103,11 +100,8 @@ ubi_ioctl(struct tcb *const tcp, const unsigned int code,
                        tprintf("{vol_id=%" PRIi32 ", name_len=%" PRIi16
                                ", name=", rnvol.ents[c].vol_id,
                                rnvol.ents[c].name_len);
-                       if (print_quoted_string(rnvol.ents[c].name,
-                                       CLAMP(rnvol.ents[c].name_len, 0, UBI_MAX_VOLUME_NAME),
-                                       QUOTE_0_TERMINATED) > 0) {
-                               tprints("...");
-                       }
+                       print_quoted_cstring(rnvol.ents[c].name,
+                                       CLAMP(rnvol.ents[c].name_len, 0, UBI_MAX_VOLUME_NAME));
                        tprints("}");
                }
                tprints("]}");
diff --git a/util.c b/util.c
index 2ccfe4fe32438199a43936872d8478c68010e305..06a939e74f9833c391e76bb01089f3391eab4fb0 100644 (file)
--- a/util.c
+++ b/util.c
@@ -695,6 +695,24 @@ print_quoted_string(const char *str, unsigned int size,
        return rc;
 }
 
+/*
+ * Quote a NUL-terminated string `str' of length up to `size' - 1
+ * and print the result.
+ *
+ * Returns 0 if NUL was seen, 1 otherwise.
+ */
+int
+print_quoted_cstring(const char *str, unsigned int size)
+{
+       int unterminated =
+               print_quoted_string(str, size, QUOTE_0_TERMINATED);
+
+       if (unterminated)
+               tprints("...");
+
+       return unterminated;
+}
+
 /*
  * Print path string specified by address `addr' and length `n'.
  * If path length exceeds `n', append `...' to the output.
@@ -719,10 +737,8 @@ printpathn(struct tcb *const tcp, const kernel_ulong_t addr, unsigned int n)
        if (nul_seen < 0)
                printaddr(addr);
        else {
-               path[n++] = '\0';
-               print_quoted_string(path, n, QUOTE_0_TERMINATED);
-               if (!nul_seen)
-                       tprints("...");
+               path[n++] = !nul_seen;
+               print_quoted_cstring(path, n);
        }
 }