Re: [PATCH 15/19] perf tools: use XSI-complaint version of strerror_r() instead of GNU-specific

From: Namhyung Kim
Date: Wed Jul 25 2012 - 20:45:32 EST


On Wed, 25 Jul 2012 14:41:30 -0300, Arnaldo Carvalho de Melo wrote:
> From: Kirill A. Shutemov <kirill@xxxxxxxxxxxxx>
>
> Perf uses GNU-specific version of strerror_r(). The GNU-specific strerror_r()
> returns a pointer to a string containing the error message. This may be either
> a pointer to a string that the function stores in buf, or a pointer to some
> (immutable) static string (in which case buf is unused).
>
> In glibc-2.16 GNU version was marked with attribute warn_unused_result. It
> triggers few warnings in perf:
>
> util/target.c: In function âperf_target__strerrorâ:
> util/target.c:114:13: error: ignoring return value of âstrerror_râ, declared with attribute warn_unused_result [-Werror=unused-result]
> ui/browsers/hists.c: In function âhist_browser__dumpâ:
> ui/browsers/hists.c:981:13: error: ignoring return value of âstrerror_râ, declared with attribute warn_unused_result [-Werror=unused-result]
>
> They are bugs.
>
> Let's fix strerror_r() usage.
>
> Signed-off-by: Kirill A. Shutemov <kirill@xxxxxxxxxxxxx>
> Acked-by: Ulrich Drepper <drepper@xxxxxxxxx>
> Cc: Ingo Molnar <mingo@xxxxxxxxxx>
> Cc: Paul Mackerras <paulus@xxxxxxxxx>
> Cc: Peter Zijlstra <a.p.zijlstra@xxxxxxxxx>
> Cc: Ulrich Drepper <drepper@xxxxxxxxx>
> Link: http://lkml.kernel.org/r/20120723210654.GA25248@xxxxxxxxxxxxx
> [ committer note: s/assert/BUG_ON/g ]
> Signed-off-by: Arnaldo Carvalho de Melo <acme@xxxxxxxxxx>
> ---
> tools/perf/ui/browsers/hists.c | 4 ++--
> tools/perf/util/target.c | 11 ++++++++++-
> 2 files changed, 12 insertions(+), 3 deletions(-)
>
> diff --git a/tools/perf/ui/browsers/hists.c b/tools/perf/ui/browsers/hists.c
> index 482f051..413bd62 100644
> --- a/tools/perf/ui/browsers/hists.c
> +++ b/tools/perf/ui/browsers/hists.c
> @@ -978,8 +978,8 @@ static int hist_browser__dump(struct hist_browser *browser)
> fp = fopen(filename, "w");
> if (fp == NULL) {
> char bf[64];
> - strerror_r(errno, bf, sizeof(bf));
> - ui_helpline__fpush("Couldn't write to %s: %s", filename, bf);
> + const char *err = strerror_r(errno, bf, sizeof(bf));
> + ui_helpline__fpush("Couldn't write to %s: %s", filename, err);
> return -1;
> }
>
> diff --git a/tools/perf/util/target.c b/tools/perf/util/target.c
> index 1064d5b..3f59c49 100644
> --- a/tools/perf/util/target.c
> +++ b/tools/perf/util/target.c
> @@ -110,8 +110,17 @@ int perf_target__strerror(struct perf_target *target, int errnum,
> int idx;
> const char *msg;
>
> + BUG_ON(buflen > 0);
> +

No! It should be

BUG_ON(buflen == 0);

Thanks,
Namhyung


> if (errnum >= 0) {
> - strerror_r(errnum, buf, buflen);
> + const char *err = strerror_r(errnum, buf, buflen);
> +
> + if (err != buf) {
> + size_t len = strlen(err);
> + char *c = mempcpy(buf, err, min(buflen - 1, len));
> + *c = '\0';
> + }
> +
> return 0;
> }
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/