Re: [PATCH] tools/lib/traceevent, perf tools: Handle %pU format correctly

From: Qu Wenruo
Date: Wed Oct 16 2019 - 19:46:49 EST




On 2019/10/16 äå10:54, Steven Rostedt wrote:
> On Wed, 16 Oct 2019 14:39:20 +0800
> Qu Wenruo <wqu@xxxxxxxx> wrote:
>
>> +static void print_uuid_arg(struct trace_seq *s, void *data, int size,
>> + struct tep_event *event, struct tep_print_arg *arg)
>> +{
>> + const char *fmt;
>> + unsigned char *buf;
>> +
>> + fmt = "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x";
>> + if (arg->type != TEP_PRINT_FIELD) {
>> + trace_seq_printf(s, "ARG TYPE NOT FIELID but %d", arg->type);
>> + return;
>> + }
>> +
>> + if (!arg->field.field) {
>> + arg->field.field = tep_find_any_field(event, arg->field.name);
>> + if (!arg->field.field) {
>> + do_warning("%s: field %s not found",
>> + __func__, arg->field.name);
>> + return;
>> + }
>> + }
>> + buf = data + arg->field.field->offset;
>
> You also need to make sure the data field is not smaller than 16 bytes.
>
> if (arg->field.field->size < 16) {
> trace_seq_puts(s, "INVALIDUUID");
> return;
> }
>

Oh, forgot that sanity check.

>> +
>> + trace_seq_printf(s, fmt, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5],
>> + buf[6], buf[7], buf[8], buf[9], buf[10], buf[11], buf[12],
>> + buf[13], buf[14], buf[15]);
>> +}
>> +
>
> Hmm, I know print_mac_addr() does something similar as this, but this
> is getting a bit extreme (too many arguments!). What about doing:
>
> for (i = 0; i < 4; i++)
> trace_seq_printf(s, "%02x", buf++);
>
> for (i = 0; i < 3; i++)
> trace_seq_printf(s, "-%02x%02x", buf[i * 2], buf[i * 2 + 1]);
>
> buf += 6;
>
> trace_seq_putc(s, '-');
>
> for (i = 0; i < 6; i++)
> trace_seq_printf(s, "%02x", buf++);
>
>
>
> Hmm, not sure the above is better, but having that many arguments just
> looks ugly to me.

Indeed, I'll update the patchset to make it more sane.

Thanks,
Qu
>
> -- Steve
>

Attachment: signature.asc
Description: OpenPGP digital signature