changeset 3709:8158714ca8ba

(quotearg_buffer_restyled): Fix bug with quoting buffers containing NUL when backslashing escapes. This bug was exposed by the other changes in this patch. (quotearg_n_options): New arg ARGSIZE. All callers changed. (quoting_options_from_style): New function. (quotearg_n_style): Use it. (quotearg_n_style_mem): New function.
author Jim Meyering <jim@meyering.net>
date Tue, 22 Jan 2002 08:02:22 +0000
parents bd719c800d4b
children 2b37ca0328bd
files lib/quotearg.c
diffstat 1 files changed, 38 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/lib/quotearg.c
+++ b/lib/quotearg.c
@@ -1,5 +1,5 @@
 /* quotearg.c - quote arguments for output
-   Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
+   Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -301,6 +301,16 @@
       c = arg[i];
       switch (c)
 	{
+	case '\0':
+	  if (backslash_escapes)
+	    {
+	      STORE ('\\');
+	      STORE ('0');
+	      STORE ('0');
+	      c = '0';
+	    }
+	  break;
+
 	case '?':
 	  switch (quoting_style)
 	    {
@@ -527,14 +537,15 @@
 				   p->style, p);
 }
 
-/* Use storage slot N to return a quoted version of the string ARG.
+/* Use storage slot N to return a quoted version of argument ARG.
+   ARG is of size ARGSIZE, but if that is -1, ARG is a null-terminated string.
    OPTIONS specifies the quoting options.
    The returned value points to static storage that can be
    reused by the next call to this function with the same value of N.
    N must be nonnegative.  N is deliberately declared with type "int"
    to allow for future extensions (using negative values).  */
 static char *
-quotearg_n_options (int n, char const *arg,
+quotearg_n_options (int n, char const *arg, size_t argsize,
 		    struct quoting_options const *options)
 {
   /* Preallocate a slot 0 buffer, so that the caller can always quote
@@ -575,13 +586,13 @@
   {
     size_t size = slotvec[n].size;
     char *val = slotvec[n].val;
-    size_t qsize = quotearg_buffer (val, size, arg, (size_t) -1, options);
+    size_t qsize = quotearg_buffer (val, size, arg, argsize, options);
 
     if (size <= qsize)
       {
 	slotvec[n].size = size = qsize + 1;
 	slotvec[n].val = val = xrealloc (val == slot0 ? 0 : val, size);
-	quotearg_buffer (val, size, arg, (size_t) -1, options);
+	quotearg_buffer (val, size, arg, argsize, options);
       }
 
     return val;
@@ -591,7 +602,7 @@
 char *
 quotearg_n (int n, char const *arg)
 {
-  return quotearg_n_options (n, arg, &default_quoting_options);
+  return quotearg_n_options (n, arg, (size_t) -1, &default_quoting_options);
 }
 
 char *
@@ -600,13 +611,29 @@
   return quotearg_n (0, arg);
 }
 
+/* Return quoting options for STYLE, with no extra quoting.  */
+static struct quoting_options
+quoting_options_from_style (enum quoting_style style)
+{
+  struct quoting_options o;
+  o.style = style;
+  memset (o.quote_these_too, 0, sizeof o.quote_these_too);
+  return o;
+}
+
 char *
 quotearg_n_style (int n, enum quoting_style s, char const *arg)
 {
-  struct quoting_options o;
-  o.style = s;
-  memset (o.quote_these_too, 0, sizeof o.quote_these_too);
-  return quotearg_n_options (n, arg, &o);
+  struct quoting_options const o = quoting_options_from_style (s);
+  return quotearg_n_options (n, arg, (size_t) -1, &o);
+}
+
+char *
+quotearg_n_style_mem (int n, enum quoting_style s,
+		      char const *arg, size_t argsize)
+{
+  struct quoting_options const o = quoting_options_from_style (s);
+  return quotearg_n_options (n, arg, argsize, &o);
 }
 
 char *
@@ -621,7 +648,7 @@
   struct quoting_options options;
   options = default_quoting_options;
   set_char_quoting (&options, ch, 1);
-  return quotearg_n_options (0, arg, &options);
+  return quotearg_n_options (0, arg, (size_t) -1, &options);
 }
 
 char *