]> granicus.if.org Git - strace/commitdiff
util: add quote_string flag signalising that string is NUL-terminated
authorEugene Syromyatnikov <evgsyr@gmail.com>
Mon, 3 Oct 2016 18:35:37 +0000 (21:35 +0300)
committerDmitry V. Levin <ldv@altlinux.org>
Mon, 3 Oct 2016 19:28:15 +0000 (19:28 +0000)
It is useful in cases strings with size provided are expected to be
NUL-terminated but are not trustworthy enough to call just plain
printstr(str, -1).

* defs.h (QUOTE_OMIT_TRAILING_0): New constant definition.
* util.c (string_quote): Swallow terminating NUL if
QUOTE_OMIT_TRAILING_0 is set.

defs.h
util.c

diff --git a/defs.h b/defs.h
index ff133a560c98024da8cdc231c120112ff27f8e3b..d35d7cf8bd113cb3ea63ee9545c20e2d7a60b462 100644 (file)
--- a/defs.h
+++ b/defs.h
@@ -520,8 +520,9 @@ extern unsigned long get_pagesize(void);
 extern int string_to_uint(const char *str);
 extern int next_set_bit(const void *bit_array, unsigned cur_bit, unsigned size_bits);
 
-#define QUOTE_0_TERMINATED                     0x01
-#define QUOTE_OMIT_LEADING_TRAILING_QUOTES     0x02
+#define QUOTE_0_TERMINATED                      0x01
+#define QUOTE_OMIT_LEADING_TRAILING_QUOTES      0x02
+#define QUOTE_OMIT_TRAILING_0                   0x08
 
 extern int string_quote(const char *, char *, unsigned int, unsigned int);
 extern int print_quoted_string(const char *, unsigned int, unsigned int);
diff --git a/util.c b/util.c
index 0dbe20c92ae42a3806b50240811f27c5869b93b9..39abcc6127cf973bb07002113662769ad7fe8f6c 100644 (file)
--- a/util.c
+++ b/util.c
@@ -619,6 +619,9 @@ string_quote(const char *instr, char *outstr, const unsigned int size,
                        /* Check for NUL-terminated string. */
                        if (c == eol)
                                goto asciz_ended;
+                       if ((i == (size - 1)) &&
+                           (style & QUOTE_OMIT_TRAILING_0) && (c == '\0'))
+                               goto asciz_ended;
                        switch (c) {
                                case '\"': case '\\':
                                        *s++ = '\\';