RAKWireless / rak_common_for_gateway

215 stars 126 forks source link

Fix lora_pkt_fwd.c strncpy bounds compiler warning message #31

Closed hdtodd closed 3 years ago

hdtodd commented 3 years ago

This may seem trivial, but compiler warnings concern me, since they're flagging potential problems that could arise when the code is executing. The "strncpy" warning messages in lora_pkt_fwd.c, for example, alert you that the strncpy could end up with a string that is not null-terminated, and in execution the error would be hard to trace down.

The following patch just reduces the max number of characters to be copied by 1, leaving room for the null termination, and eliminating the compiler warning:

/opt/ttn-gateway/packet_forwarder/lora_pkt_fwd/src# diff -u /home/hdtodd/Installs/rak_common_for_gateway-4.2.3/lora/rak2245/lora_pkt_fwd.c lora_pkt_fwd.c

--- /home/hdtodd/Installs/rak_common_for_gateway-4.2.3/lora/rak2245/lora_pkt_fwd.c 2020-08-05 15:51:03.954335595 -0400 +++ lora_pkt_fwd.c 2020-08-23 10:34:17.645247812 -0400 @@ -693,7 +693,7 @@ / server hostname or IP address (optional) / str = json_object_get_string(conf_obj, "server_address"); if (str != NULL) { - strncpy(serv_addr, str, sizeof serv_addr); + strncpy(serv_addr, str, (sizeof serv_addr)-1); MSG("INFO: server hostname or IP address is configured to \"%s\"\n", serv_addr); }

@@ -750,7 +750,7 @@ / GPS module TTY path (optional) / str = json_object_get_string(conf_obj, "gps_tty_path"); if (str != NULL) { - strncpy(gps_tty_path, str, sizeof gps_tty_path); + strncpy(gps_tty_path, str, (sizeof gps_tty_path)-1); MSG("INFO: GPS serial port path is configured to \"%s\"\n", gps_tty_path); } lora_pkt_fwd-patch.txt

RAKzhuqi commented 3 years ago

Hello @hdtodd ,Thank you very much for your suggestion, we will adopt it in the next version.

hdtodd commented 3 years ago

You're welcome ... thanks for adopting it.