Kaydet (Commit) 26688cf0 authored tarafından Stephan Bergmann's avatar Stephan Bergmann

Avoid -Werror=stringop-truncation

...as emitted by at least GCC 8.2 with --enable-optimized.  These are not
actually problems, as in both cases strncpy(p,...,N-1) is used to copy a
string into an area p of length N that has initially been zero-initialized, so
p[N-1] will already contain NUL.  But add the (redundant) p[N-1]=NUL just in
case, and to silence GCC (where the documentation explicitly recommends this
additional write as a way to silence the warning).  Unfortunately, in hstyle.cxx
at least GCC 8.2 with --enable-optimized would still emit the warning, even
though it uses the recommended way of handling it, so needs to be silenced with
a #pragma.

Change-Id: Ie41f420c732c2bfb699903ebd21ce1a89dd2b236
Reviewed-on: https://gerrit.libreoffice.org/66620
Tested-by: Jenkins
Reviewed-by: 's avatarStephan Bergmann <sbergman@redhat.com>
üst 3a001a58
......@@ -47,7 +47,9 @@ void HWPFont::AddFont(int lang, const char *font)
nfonts = nFonts[lang];
if (MAXFONTS <= nfonts)
return;
strncpy(fontnames[lang].get() + FONTNAMELEN * nfonts, font, FONTNAMELEN - 1);
auto const p = fontnames[lang].get() + FONTNAMELEN * nfonts;
strncpy(p, font, FONTNAMELEN - 1);
p[FONTNAMELEN - 1] = '\0'; // just in case, even though the array is zero-initialized
nFonts[lang]++;
}
......
......@@ -66,7 +66,18 @@ void HWPStyle::SetName(int n, char const *name)
if (n >= 0 && n < nstyles)
{
if (name)
strncpy(DATA[n].name, name, MAXSTYLENAME);
{
#if defined __GNUC__ && __GNUC__ == 8 && __GNUC_MINOR__ == 2 && !defined __clang__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wstringop-truncation"
#endif
auto const p = DATA[n].name;
strncpy(p, name, MAXSTYLENAME);
p[MAXSTYLENAME] = '\0'; // just in case, even though the array is zero-initialized
#if defined __GNUC__ && __GNUC__ == 8 && __GNUC_MINOR__ == 2 && !defined __clang__
#pragma GCC diagnostic pop
#endif
}
else
DATA[n].name[0] = 0;
}
......
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