Commit 37b31bc3 authored by Arnaldo Carvalho de Melo's avatar Arnaldo Carvalho de Melo Committed by Stefan Bader

perf ui helpline: Use strlcpy() as a shorter form of strncpy() + explicit set nul

BugLink: https://bugs.launchpad.net/bugs/1836668

commit 4d0f16d0 upstream.

The strncpy() function may leave the destination string buffer
unterminated, better use strlcpy() that we have a __weak fallback
implementation for systems without it.

In this case we are actually setting the null byte at the right place,
but since we pass the buffer size as the limit to strncpy() and not
it minus one, gcc ends up warning us about that, see below. So, lets
just switch to the shorter form provided by strlcpy().

This fixes this warning on an Alpine Linux Edge system with gcc 8.2:

  ui/tui/helpline.c: In function 'tui_helpline__push':
  ui/tui/helpline.c:27:2: error: 'strncpy' specified bound 512 equals destination size [-Werror=stringop-truncation]
    strncpy(ui_helpline__current, msg, sz)[sz - 1] = '\0';
    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  cc1: all warnings being treated as errors

Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Namhyung Kim <namhyung@kernel.org>
Fixes: e6e90468 ("perf ui: Introduce struct ui_helpline")
Link: https://lkml.kernel.org/n/tip-d1wz0hjjsh19xbalw69qpytj@git.kernel.orgSigned-off-by: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: default avatarKhalid Elmously <khalid.elmously@canonical.com>
Signed-off-by: default avatarKleber Sacilotto de Souza <kleber.souza@canonical.com>
parent dfb2428e
...@@ -23,7 +23,7 @@ static void tui_helpline__push(const char *msg) ...@@ -23,7 +23,7 @@ static void tui_helpline__push(const char *msg)
SLsmg_set_color(0); SLsmg_set_color(0);
SLsmg_write_nstring((char *)msg, SLtt_Screen_Cols); SLsmg_write_nstring((char *)msg, SLtt_Screen_Cols);
SLsmg_refresh(); SLsmg_refresh();
strncpy(ui_helpline__current, msg, sz)[sz - 1] = '\0'; strlcpy(ui_helpline__current, msg, sz);
} }
static int tui_helpline__show(const char *format, va_list ap) static int tui_helpline__show(const char *format, va_list ap)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment