bug-hurd
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH 4/4] utils/rpctrace: escape non-printable characters in strin


From: Samuel Thibault
Subject: Re: [PATCH 4/4] utils/rpctrace: escape non-printable characters in strings
Date: Sun, 15 Dec 2013 21:36:38 +0100
User-agent: Mutt/1.5.21+34 (58baf7c9f32f) (2010-12-30)

Justus Winter, le Fri 13 Dec 2013 13:03:07 +0100, a écrit :
> * utils/rpctrace.c (escape_sequences): New char array mapping
> characters to their escape sequence.
> (print_data): Escape non-printable characters when printing strings.

Ack on the principle for a fixed patch :)

> ---
>  utils/rpctrace.c |   48 ++++++++++++++++++++++++++++++++++++++++++++++--
>  1 file changed, 46 insertions(+), 2 deletions(-)
> 
> diff --git a/utils/rpctrace.c b/utils/rpctrace.c
> index b39b2e3..f6e255b 100644
> --- a/utils/rpctrace.c
> +++ b/utils/rpctrace.c
> @@ -1528,6 +1528,20 @@ print_reply_header (struct send_once_info *info, 
> mig_reply_header_t *reply,
>      }
>  }
>  
> +static char escape_sequences[0xff] =
> +  {
> +    ['\0'] = '0',
> +    ['\a'] = 'a',
> +    ['\b'] = 'b',
> +    ['\f'] = 'f',
> +    ['\n'] = 'n',
> +    ['\r'] = 'r',
> +    ['\t'] = 't',
> +    ['\v'] = 'v',
> +    ['\\'] = '\\',
> +    ['\''] = '\'',
> +    ['"'] = '"',
> +  };
>  
>  static void
>  print_data (mach_msg_type_name_t type,
> @@ -1555,8 +1569,38 @@ print_data (mach_msg_type_name_t type,
>      case MACH_MSG_TYPE_CHAR:
>        if (nelt > strsize)
>       nelt = strsize;
> -      fprintf (ostream, "\"%.*s\"",
> -            (int) (nelt * eltsize), (const char *) data);
> +      fprintf (ostream, "\"");
> +      /* Scan data for non-printable characters.  p always points to
> +      the first character that has not yet been printed.  */
> +      const char *p, *q;
> +      p = q = (const char *) data;
> +      while (*q && q - (const char *) data < (int) (nelt * eltsize))
> +     {
> +       if (isgraph (*q) || *q == ' ')
> +         {
> +           q += 1;
> +           continue;
> +         }
> +
> +       /* We encountered a non-printable character.  Print anything
> +          that has not been printed so far.  */
> +       if (p < q)
> +         fprintf (ostream, "%.*s", q - p, p);
> +
> +       char c = escape_sequences[*((const unsigned char *) q)];
> +       if (c)
> +         fprintf (ostream, "\\%c", c);
> +       else
> +         fprintf (ostream, "\\x%02x", *((const unsigned char *) q));
> +
> +       q += 1;
> +       p = q;
> +     }
> +
> +      /* Print anything that has not been printed so far.  */
> +      if (p < q)
> +     fprintf (ostream, "%.*s", q - p, p);
> +      fprintf (ostream, "\"");
>        return;
>  
>  #if 0
> -- 
> 1.7.10.4
> 
> 

-- 
Samuel
What's this script do?
    unzip ; touch ; finger ; mount ; gasp ; yes ; umount ; sleep
Hint for the answer: not everything is computer-oriented. Sometimes you're
in a sleeping bag, camping out.
(Contributed by Frans van der Zande.)



reply via email to

[Prev in Thread] Current Thread [Next in Thread]