Skip to content

Commit

Permalink
drivers: fix -Wstringop-truncation
Browse files Browse the repository at this point in the history
GCC 9 complained about stringop-truncation which is a cautionary message
to prevent using strncpy with non-null terminated strings.

We can fix this by copying one byte less than the destination size and
then manually adding the null termination, as we already do.
  • Loading branch information
julianoes authored and bkueng committed May 16, 2019
1 parent 3ab75b8 commit b20feac
Show file tree
Hide file tree
Showing 6 changed files with 6 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/drivers/distance_sensor/cm8jl65/cm8jl65.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ CM8JL65::CM8JL65(const char *port, uint8_t rotation) :
_comms_errors(perf_alloc(PC_COUNT, "cm8jl65_com_err"))
{
/* store port name */
strncpy(_port, port, sizeof(_port));
strncpy(_port, port, sizeof(_port) - 1);
/* enforce null termination */
_port[sizeof(_port) - 1] = '\0';

Expand Down
2 changes: 1 addition & 1 deletion src/drivers/distance_sensor/pga460/pga460.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ extern "C" __EXPORT int pga460_main(int argc, char *argv[]);
PGA460::PGA460(const char *port)
{
// Store port name.
strncpy(_port, port, sizeof(_port));
strncpy(_port, port, sizeof(_port) - 1);
// Enforce null termination.
_port[sizeof(_port) - 1] = '\0';
}
Expand Down
2 changes: 1 addition & 1 deletion src/drivers/distance_sensor/sf0x/sf0x.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ SF0X::SF0X(const char *port, uint8_t rotation) :
_comms_errors(perf_alloc(PC_COUNT, "sf0x_com_err"))
{
/* store port name */
strncpy(_port, port, sizeof(_port));
strncpy(_port, port, sizeof(_port) - 1);
/* enforce null termination */
_port[sizeof(_port) - 1] = '\0';

Expand Down
2 changes: 1 addition & 1 deletion src/drivers/distance_sensor/tfmini/tfmini.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ TFMINI::TFMINI(const char *port, uint8_t rotation) :
_comms_errors(perf_alloc(PC_COUNT, "tfmini_com_err"))
{
/* store port name */
strncpy(_port, port, sizeof(_port));
strncpy(_port, port, sizeof(_port) - 1);
/* enforce null termination */
_port[sizeof(_port) - 1] = '\0';
}
Expand Down
2 changes: 1 addition & 1 deletion src/drivers/distance_sensor/ulanding/ulanding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ Radar::Radar(uint8_t rotation, const char *port) :

{
/* store port name */
strncpy(_port, port, sizeof(_port));
strncpy(_port, port, sizeof(_port) - 1);
/* enforce null termination */
_port[sizeof(_port) - 1] = '\0';
}
Expand Down
2 changes: 1 addition & 1 deletion src/drivers/gps/gps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ GPS::GPS(const char *path, gps_driver_mode_t mode, GPSHelper::Interface interfac
_instance(instance)
{
/* store port name */
strncpy(_port, path, sizeof(_port));
strncpy(_port, path, sizeof(_port) - 1);
/* enforce null termination */
_port[sizeof(_port) - 1] = '\0';

Expand Down

0 comments on commit b20feac

Please sign in to comment.