On Tue, May 02, 2023 at 09:34:43AM -0600, Todd C. Miller wrote:
> On Tue, 02 May 2023 14:13:27 +0200, Claudio Jeker wrote:
> 
> > Add a json_do_string() a function to print a JSON string.
> > This function does the needed encoding of control chars and escape chars.
> > I skipped the optional encoding of the forward slash (/) since this is
> > only needed if the json output is embedded in HTML/SGML/XML.
> > People putting JSON into such documents need to pass this through an extra
> > step.
> 
> Unless you can guarantee that other control characters cannot occur
> you probably want to output them in unicode hex form.  For example,
> something like:
> 
> default:
>       const char hex[] = "0123456789abcdef";
>       if (iscntrl(c)) {
>               /* Escape control characters like \u0000 */
>               if (!eb)
>                       eb = fprintf(jsonfh, "\\u00%c%c", hex[c >> 4],
>                           hex[c & 0x0f]) < 0;
>       }

I think this is not really needed. All strings are previously checked for
any bad characters. In general I think covering more than \n, \r and \t makes
little sense (but I added the other ones just for completeness).
Maybe the best is to check but error out for iscntrl(c).
 
> In this case you probably want to assign c as an unsigned char.
> E.g.
> 
> while ((c = (unsigned char)*v++) != '\0') {
>       ...
> }
> 
> Or better yet just declare c as unsigned char instead of int.

Good point. I guess it is better to use unsigned char here.

-- 
:wq Claudio

Reply via email to