warning compiling chibios 20.3 with gcc 10.2

Report here problems in any of ChibiOS components. This forum is NOT for support.
User avatar
alex31
Posts: 304
Joined: Fri May 25, 2012 10:23 am
Location: toulouse, france
Has thanked: 28 times
Been thanked: 37 times
Contact:

warning compiling chibios 20.3 with gcc 10.2

Postby alex31 » Tue Sep 08, 2020 2:38 pm

Hello,

I am doing experiment with c++20, and need g++10, so i went over this bug :

Code: Select all

In function 'dyn_create_object_heap',
    inlined from 'dyn_create_object_heap' at ../../../ChibiOS_20.3_stable/os/oslib/src/chfactory.c:124:23:
../../../ChibiOS_20.3_stable/os/oslib/src/chfactory.c:146:3: error: 'strncpy' specified bound 8 equals destination size [-Werror=stringop-truncation]
  146 |   strncpy(dep->name, name, CH_CFG_FACTORY_MAX_NAMES_LENGTH);
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Compiling chcore.c
Compiling chcore_v7m.c
In function 'dyn_create_object_pool',
    inlined from 'dyn_create_object_pool' at ../../../ChibiOS_20.3_stable/os/oslib/src/chfactory.c:172:23:
../../../ChibiOS_20.3_stable/os/oslib/src/chfactory.c:194:3: error: 'strncpy' specified bound 8 equals destination size [-Werror=stringop-truncation]
  194 |   strncpy(dep->name, name, CH_CFG_FACTORY_MAX_NAMES_LENGTH);
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
cc1: all warnings being treated as errors


If it's OK that dep->name is not null terminated when length is equal to CH_CFG_FACTORY_MAX_NAMES_LENGTH, it's a false positive.

Probably a new check that gcc10 does.


Alexandre

User avatar
Giovanni
Site Admin
Posts: 13118
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 767 times
Been thanked: 651 times
Contact:

Re: warning compiling chibios 20.3 with gcc 10.2

Postby Giovanni » Tue Sep 08, 2020 3:25 pm

Hi,

It is meant to not be zero terminated if the length is equal to buffer size, not sure about how to handle the warning.

Giovanni

User avatar
alex31
Posts: 304
Joined: Fri May 25, 2012 10:23 am
Location: toulouse, france
Has thanked: 28 times
Been thanked: 37 times
Contact:

Re: warning compiling chibios 20.3 with gcc 10.2

Postby alex31 » Wed Sep 09, 2020 12:25 pm

yes, probably that

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wstringop-truncation"
#pragma GCC diagnostic pop


is a bit to much gcc specific ...

Alexandre

mikeprotts
Posts: 151
Joined: Wed Jan 09, 2019 12:37 pm
Has thanked: 19 times
Been thanked: 23 times

Re: warning compiling chibios 20.3 with gcc 10.2

Postby mikeprotts » Thu Sep 10, 2020 11:37 am

Giovanni wrote:It is meant to not be zero terminated if the length is equal to buffer size, not sure about how to handle the warning.


So the destination is a character array rather than a string.

The logic seems to be:

Code: Select all

if (strlen(name) < CH_CFG_FACTORY_MAX_NAMES_LENGTH)
{
  strncpy(dep->name, name, CH_CFG_FACTORY_MAX_NAMES_LENGTH);
}
else
{
  memcpy(dep->name, name, CH_CFG_FACTORY_MAX_NAMES_LENGTH);
}


This might also be coded as:

Code: Select all

chDbgAssert(strlen(name) <= CH_CFG_FACTORY_MAX_NAMES_LENGTH, "factory name too long");
memcpy(dep->name, name, strlen(name));
if (strlen(name) < CH_CFG_FACTORY_MAX_NAMES_LENGTH)
{
  dep->name[strlen(name)]= 0;
}


However that leaves the section of dep->name from strlen(name) to CH_CFG_FACTORY_MAX_NAMES_LENGTH as undefined, so may be better to use:

Code: Select all

chDbgAssert(strlen(name) <= CH_CFG_FACTORY_MAX_NAMES_LENGTH, "factory name too long");
memset(dep->name, 0, CH_CFG_FACTORY_MAX_NAMES_LENGTH);
memcpy(dep->name, name, strlen(name));


which is probably more efficient than

Code: Select all

chDbgAssert(strlen(name) <= CH_CFG_FACTORY_MAX_NAMES_LENGTH, "factory name too long");
memcpy(dep->name, name, strlen(name));
memset(dep->name, strlen(name), CH_CFG_FACTORY_MAX_NAMES_LENGTH - strlen(name));


Mike

User avatar
Giovanni
Site Admin
Posts: 13118
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 767 times
Been thanked: 651 times
Contact:

Re: warning compiling chibios 20.3 with gcc 10.2

Postby Giovanni » Sat Sep 12, 2020 2:31 pm

Probably the easiest way is to replace strncpy() with a local function in that module.

Giovanni


Return to “Bug Reports”

Who is online

Users browsing this forum: No registered users and 4 guests