Re: [PATCH v5 1/3] perf report: compile tips.txt in perf binary

From: Jiri Olsa
Date: Wed May 19 2021 - 09:24:57 EST


On Mon, May 17, 2021 at 01:46:02AM -0700, Denys Zagorui wrote:

SNIP

> return hist_browser(rep->session->evlist, help, NULL, rep->min_percent);
> }
>
> +#define MAX_TIPS 60
> +
> +static const char *perf_tip(void)
> +{
> + char *str[MAX_TIPS];
> + int i = 0;
> +
> + _binary_Documentation_tips_txt_start[_binary_Documentation_tips_txt_end -
> + _binary_Documentation_tips_txt_start - 1] = 0;
> +
> + str[i] = strtok(_binary_Documentation_tips_txt_start, "\n");
> + if (!str[i])
> + return "Tips cannot be found!";
> +
> + i++;
> +
> + while (i < MAX_TIPS) {
> + str[i] = strtok(NULL, "\n");
> + if (!str[i])
> + break;
> + i++;
> + }
> +
> + return str[random() % i];

that still does not solve that we set MAX_TIPS and have
no way of checking that's still valid

how about something like below (completely untested):


static const char *perf_tip(void)
{
char *start = _binary_Documentation_tips_txt_start;
char *tok, *tmp, *prev;
int pick, size;

size = _binary_Documentation_tips_txt_end - start;
pick = random() % size;

_binary_Documentation_tips_txt_start[size - 1] = 0;

for (tok = strtok_r(start, "\n", &tmp); tok;
tok = strtok_r(NULL, "\n", &tmp)) {
if (pick < (tok - start))
return prev;
prev = tok;
}

return prev;
}

this way you wouldn't need array with MAX_TIPS defined

jirka