Jim Meyering wrote:
> Here's what happened when I started adjusting the APIs in confdb.h.
> Changing void* to const void* propagated, of course.
> Changing int to size_t exposed the problem that
> tail_key_changed (and two other functions nearby)
> would mistakenly print non-printing (aka garbage)
> or incomplete object, key, value strings.
>
> I fixed that by adding a small helper function.
>
> If you like this, I'll also fix the two similar
> functions right below tail_key_changed that have
> the same problem.
...

> +/* Print "?" in place of any non-printable byte of OBJ. */
> +static void print_name (FILE *fp, const void *obj, size_t obj_len)
> +{
> +     const char *p = obj;
> +     size_t i;
> +     for (i = 0; i < obj_len; i++) {
> +             int c = *p++;
> +             if (!isprint (c)) {
> +                     c = '?';
> +             }
> +             fputc (c, fp);
> +     }
> +}
> +
>  static void tail_key_changed(confdb_handle_t handle,
>       confdb_change_type_t change_type,
>       hdb_handle_t parent_object_handle,
>       hdb_handle_t object_handle,
>       const void *object_name_pt,
> -     int  object_name_len,
> +     size_t  object_name_len,
>       const void *key_name_pt,
> -     int key_name_len,
> +     size_t key_name_len,
>       const void *key_value_pt,
> -     int key_value_len)
> +     size_t key_value_len)
>  {
> -     printf("key_changed> %.*s.%.*s=%.*s\n",
> -            object_name_len, (const char *)object_name_pt,
> -            key_name_len, (const char *)key_value_pt,
> -            key_value_len, (const char *)key_value_pt);
> +     /* printf("key_changed> %.*s.%.*s=%.*s\n", */
> +     fputs("key_changed> ", stdout);
> +     print_name (stdout, object_name_pt, object_name_len);
> +     fputs(".", stdout);
> +     print_name (stdout, key_name_pt, key_name_len);
> +     fputs("=", stdout);
> +     print_name (stdout, key_value_pt, key_value_len);
> +     fputs("\n", stdout);

Note the change above.
Before, it was using printf, now it's using a sequence of fputs calls.
We *could* continue to use printf, but at a price.
Either change the interface to allow/handle failure, and then
malloc space in which to perform the 3 conversions (handle malloc
failure and adjust callers to handle it, too), then use printf
on the converted values, or ...

use 3 local buffers, each a large as the maximum obj/key/val
name may be, convert into that, and then use printf on those.

But each of those is ugly in its own way, so I chose the implementation
above.
_______________________________________________
Openais mailing list
[email protected]
https://lists.linux-foundation.org/mailman/listinfo/openais

Reply via email to