From 5cf68f97d597dba3f717070520df4f8407154b2f Mon Sep 17 00:00:00 2001 From: Eugene Syromyatnikov Date: Mon, 3 Oct 2016 21:35:37 +0300 Subject: [PATCH] util: add quote_string flag signalising that string is NUL-terminated 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 | 5 +++-- util.c | 3 +++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/defs.h b/defs.h index ff133a56..d35d7cf8 100644 --- 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 0dbe20c9..39abcc61 100644 --- 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++ = '\\';