You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by je...@apache.org on 2020/10/26 11:40:58 UTC
[mynewt-core] 01/02: hw/battery: Fix strncpy warning
This is an automated email from the ASF dual-hosted git repository.
jerzy pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-core.git
commit 7f524f5f07b8cef1b1f2441a3dd5e2f0fd3054b7
Author: Casper Meijn <ca...@meijn.net>
AuthorDate: Sat Oct 24 08:50:20 2020 +0200
hw/battery: Fix strncpy warning
GCC complains about a strncpy call when build using build_profile speed.
This warning indicates a possible string truncation. The only proper
solution I could find was to decrease the buf_size and explicitly set a
zero byte.
Error: In function 'battery_prop_get_name',
inlined from 'battery_find_property_by_name' at repos/apache-mynewt-core/hw/battery/src/battery.c:392:9:
repos/apache-mynewt-core/hw/battery/src/battery.c:379:5: error: 'strncpy' specified bound 20 equals destination size [-Werror=stringop-truncation]
379 | strncpy(buf, driver_prop->bdp_name, buf_size);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
cc1: all warnings being treated as errors
---
hw/battery/src/battery.c | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/hw/battery/src/battery.c b/hw/battery/src/battery.c
index f900d14..d4e2d08 100644
--- a/hw/battery/src/battery.c
+++ b/hw/battery/src/battery.c
@@ -376,7 +376,8 @@ battery_prop_get_name(const struct battery_property *prop, char *buf,
const struct battery_driver_property *driver_prop =
&driver->bd_driver_properties[prop->bp_prop_num - driver->bd_first_property];
- strncpy(buf, driver_prop->bdp_name, buf_size);
+ strncpy(buf, driver_prop->bdp_name, buf_size - 1);
+ buf[buf_size - 1] = '\0';
return buf;
}
@@ -389,7 +390,7 @@ battery_find_property_by_name(struct os_dev *battery, const char *name)
int i;
for (i = 0; i < bat->b_all_property_count; ++i) {
- battery_prop_get_name(&bat->b_properties[i], buf, 20);
+ battery_prop_get_name(&bat->b_properties[i], buf, sizeof(buf));
if (strcmp(buf, name) == 0) {
return &bat->b_properties[i];
}