Make most of functions const correct (#714)

* Make most of the functions const correct

Used clang-tidy and sonarlint to help find places where const could
pretty easily applied. Also lot of hand work.

This commit does not yet touch handlers and typedefs of those.

* Fix Arduino uno handler_who_is() has extra parenthesis

For some reason there is extra parenthesis. Remove it this is more
likely buildable.

* Bugfix/bacapp: Fix uninitilized array_index

We have changed bacapp_snprintf_value() to be const correct. After that
we got

```
/home/runner/work/bacnet-stack/bacnet-stack/src/bacnet/bacapp.c:3183:27: warning: 4th function call argument is an uninitialized value [core.CallAndMessage]
                ret_val = bacapp_snprintf_weeklyschedule(
                          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 warning generated.
```

So analyzer could now spot that we do not actually initilize array_index
at all. Fix this by setting array_index to zero. Note that I actually do
not know if zeroing is right thing to do here. I choose zero as if this
has worked before it is most likely that it will work with zero value.

* cmake: Add and ignore Wwrite-strings compiler option

Wwrite-strings helps find places where const correctness is broken.

Example it will warn about these

```C

void func1(char* str);

func("test") /* "test" is const so we should not pass it to func1().

char* func2()
{
  return "test"; /* func2() should return const char*.
}
```

We still need to ignore it as not all are fixed but let's add it already
so we remember that it should be opened at some point.

---------

Co-authored-by: Kari Argillander <kari.argillander@fidelix.com>
This commit is contained in:
Kari Argillander
2024-08-29 22:08:02 +03:00
committed by GitHub
parent 0177c59f4a
commit a2f1d6959d
408 changed files with 2608 additions and 2336 deletions
+18 -16
View File
@@ -114,7 +114,7 @@ void bvlc6_maintenance_timer(uint16_t seconds)
* @return true if the address was set
*/
static bool bbmd6_address_from_vmac(
BACNET_IP6_ADDRESS *addr, struct vmac_data *vmac)
BACNET_IP6_ADDRESS *addr, const struct vmac_data *vmac)
{
bool status = false;
unsigned int i = 0;
@@ -139,7 +139,7 @@ static bool bbmd6_address_from_vmac(
* @return true if the address was set
*/
static bool bbmd6_address_to_vmac(
struct vmac_data *vmac, BACNET_IP6_ADDRESS *addr)
struct vmac_data *vmac, const BACNET_IP6_ADDRESS *addr)
{
bool status = false;
unsigned int i = 0;
@@ -162,7 +162,7 @@ static bool bbmd6_address_to_vmac(
* @param device_id - device ID used as the key-pair
* @param addr - IPv6 source address
*/
static void bbmd6_add_vmac(uint32_t device_id, BACNET_IP6_ADDRESS *addr)
static void bbmd6_add_vmac(uint32_t device_id, const BACNET_IP6_ADDRESS *addr)
{
bool found = false;
uint32_t list_device_id = 0;
@@ -224,7 +224,7 @@ static void bbmd6_add_vmac(uint32_t device_id, BACNET_IP6_ADDRESS *addr)
*
* @return true if the IPv6 from sin match me
*/
static bool bbmd6_address_match_self(BACNET_IP6_ADDRESS *addr)
static bool bbmd6_address_match_self(const BACNET_IP6_ADDRESS *addr)
{
BACNET_IP6_ADDRESS my_addr = { 0 };
bool status = false;
@@ -249,7 +249,7 @@ static bool bbmd6_address_match_self(BACNET_IP6_ADDRESS *addr)
* @return true if the address was in the VMAC table
*/
static bool bbmd6_address_from_bacnet_address(
BACNET_IP6_ADDRESS *addr, uint32_t *vmac_src, BACNET_ADDRESS *baddr)
BACNET_IP6_ADDRESS *addr, uint32_t *vmac_src, const BACNET_ADDRESS *baddr)
{
struct vmac_data *vmac;
bool status = false;
@@ -285,9 +285,9 @@ static bool bbmd6_address_from_bacnet_address(
* @return Upon successful completion, returns the number of bytes sent.
* Otherwise, -1 shall be returned and errno set to indicate the error.
*/
int bvlc6_send_pdu(BACNET_ADDRESS *dest,
BACNET_NPDU_DATA *npdu_data,
uint8_t *pdu,
int bvlc6_send_pdu(const BACNET_ADDRESS *dest,
const BACNET_NPDU_DATA *npdu_data,
const uint8_t *pdu,
unsigned pdu_len)
{
BACNET_IP6_ADDRESS bvlc_dest = { 0 };
@@ -451,7 +451,9 @@ static void bbmd6_send_forward_npdu(BACNET_IP6_ADDRESS *address,
* Otherwise, -1 shall be returned and errno set to indicate the error.
*/
static int bvlc6_send_result(
BACNET_IP6_ADDRESS *dest_addr, uint32_t vmac_src, uint16_t result_code)
const BACNET_IP6_ADDRESS *dest_addr,
uint32_t vmac_src,
uint16_t result_code)
{
uint8_t mtu[BIP6_MPDU_MAX] = { 0 };
uint16_t mtu_len = 0;
@@ -473,7 +475,7 @@ static int bvlc6_send_result(
* Otherwise, -1 shall be returned and errno set to indicate the error.
*/
static int bvlc6_send_address_resolution_ack(
BACNET_IP6_ADDRESS *dest_addr, uint32_t vmac_src, uint32_t vmac_dst)
const BACNET_IP6_ADDRESS *dest_addr, uint32_t vmac_src, uint32_t vmac_dst)
{
uint8_t mtu[BIP6_MPDU_MAX] = { 0 };
uint16_t mtu_len = 0;
@@ -497,7 +499,7 @@ static int bvlc6_send_address_resolution_ack(
* Otherwise, -1 shall be returned and errno set to indicate the error.
*/
static int bvlc6_send_virtual_address_resolution_ack(
BACNET_IP6_ADDRESS *dest_addr, uint32_t vmac_src, uint32_t vmac_dst)
const BACNET_IP6_ADDRESS *dest_addr, uint32_t vmac_src, uint32_t vmac_dst)
{
uint8_t mtu[BIP6_MPDU_MAX] = { 0 };
uint16_t mtu_len = 0;
@@ -516,7 +518,7 @@ static int bvlc6_send_virtual_address_resolution_ack(
* @param pdu_len - How many bytes in NPDU+APDU buffer.
*/
static void bbmd6_virtual_address_resolution_handler(
BACNET_IP6_ADDRESS *addr, uint8_t *pdu, uint16_t pdu_len)
const BACNET_IP6_ADDRESS *addr, const uint8_t *pdu, uint16_t pdu_len)
{
int function_len = 0;
uint32_t vmac_src = 0;
@@ -550,7 +552,7 @@ static void bbmd6_virtual_address_resolution_handler(
* @param pdu_len - How many bytes in NPDU+APDU buffer.
*/
static void bbmd6_virtual_address_resolution_ack_handler(
BACNET_IP6_ADDRESS *addr, uint8_t *pdu, uint16_t pdu_len)
const BACNET_IP6_ADDRESS *addr, const uint8_t *pdu, uint16_t pdu_len)
{
int function_len = 0;
uint32_t vmac_src = 0;
@@ -578,7 +580,7 @@ static void bbmd6_virtual_address_resolution_ack_handler(
* @param pdu_len - How many bytes in NPDU+APDU buffer.
*/
static void bbmd6_address_resolution_handler(
BACNET_IP6_ADDRESS *addr, uint8_t *pdu, uint16_t pdu_len)
const BACNET_IP6_ADDRESS *addr, const uint8_t *pdu, uint16_t pdu_len)
{
int function_len = 0;
uint32_t vmac_src = 0;
@@ -614,7 +616,7 @@ static void bbmd6_address_resolution_handler(
* @param pdu_len - How many bytes in NPDU+APDU buffer.
*/
static void bbmd6_address_resolution_ack_handler(
BACNET_IP6_ADDRESS *addr, uint8_t *pdu, uint16_t pdu_len)
const BACNET_IP6_ADDRESS *addr, const uint8_t *pdu, uint16_t pdu_len)
{
int function_len = 0;
uint32_t vmac_src = 0;
@@ -1039,7 +1041,7 @@ int bvlc6_handler(BACNET_IP6_ADDRESS *addr,
* 0 if no registration request is sent, or
* -1 if registration fails.
*/
int bvlc6_register_with_bbmd(BACNET_IP6_ADDRESS *bbmd_addr,
int bvlc6_register_with_bbmd(const BACNET_IP6_ADDRESS *bbmd_addr,
uint16_t ttl_seconds)
{
uint8_t mtu[BIP6_MPDU_MAX] = { 0 };
+4 -4
View File
@@ -41,14 +41,14 @@ extern "C" {
uint16_t mtu_len);
BACNET_STACK_EXPORT
int bvlc6_send_pdu(BACNET_ADDRESS *dest,
BACNET_NPDU_DATA *npdu_data,
uint8_t *pdu,
int bvlc6_send_pdu(const BACNET_ADDRESS *dest,
const BACNET_NPDU_DATA *npdu_data,
const uint8_t *pdu,
unsigned pdu_len);
BACNET_STACK_EXPORT
int bvlc6_register_with_bbmd(
BACNET_IP6_ADDRESS *bbmd_addr,
const BACNET_IP6_ADDRESS *bbmd_addr,
uint16_t time_to_live_seconds);
BACNET_STACK_EXPORT
+8 -4
View File
@@ -52,7 +52,7 @@ unsigned int VMAC_Count(void)
*
* @return true if the device ID and MAC are added
*/
bool VMAC_Add(uint32_t device_id, struct vmac_data *src)
bool VMAC_Add(uint32_t device_id, const struct vmac_data *src)
{
bool status = false;
struct vmac_data *pVMAC = NULL;
@@ -126,7 +126,9 @@ struct vmac_data *VMAC_Find_By_Key(uint32_t device_id)
*
* @return true if the addresses are different
*/
bool VMAC_Different(struct vmac_data *vmac1, struct vmac_data *vmac2)
bool VMAC_Different(
const struct vmac_data *vmac1,
const struct vmac_data *vmac2)
{
bool status = false;
unsigned int i = 0;
@@ -157,7 +159,9 @@ bool VMAC_Different(struct vmac_data *vmac1, struct vmac_data *vmac2)
*
* @return true if the addresses are the same
*/
bool VMAC_Match(struct vmac_data *vmac1, struct vmac_data *vmac2)
bool VMAC_Match(
const struct vmac_data *vmac1,
const struct vmac_data *vmac2)
{
bool status = false;
unsigned int i = 0;
@@ -190,7 +194,7 @@ bool VMAC_Match(struct vmac_data *vmac1, struct vmac_data *vmac2)
*
* @return true if the VMAC address was found
*/
bool VMAC_Find_By_Data(struct vmac_data *vmac, uint32_t *device_id)
bool VMAC_Find_By_Data(const struct vmac_data *vmac, uint32_t *device_id)
{
bool status = false;
struct vmac_data *list_vmac;
+6 -6
View File
@@ -35,19 +35,19 @@ extern "C" {
BACNET_STACK_EXPORT
struct vmac_data *VMAC_Find_By_Key(uint32_t device_id);
BACNET_STACK_EXPORT
bool VMAC_Find_By_Data(struct vmac_data *vmac, uint32_t *device_id);
bool VMAC_Find_By_Data(const struct vmac_data *vmac, uint32_t *device_id);
BACNET_STACK_EXPORT
bool VMAC_Add(uint32_t device_id, struct vmac_data *pVMAC);
bool VMAC_Add(uint32_t device_id, const struct vmac_data *pVMAC);
BACNET_STACK_EXPORT
bool VMAC_Delete(uint32_t device_id);
BACNET_STACK_EXPORT
bool VMAC_Different(
struct vmac_data *vmac1,
struct vmac_data *vmac2);
const struct vmac_data *vmac1,
const struct vmac_data *vmac2);
BACNET_STACK_EXPORT
bool VMAC_Match(
struct vmac_data *vmac1,
struct vmac_data *vmac2);
const struct vmac_data *vmac1,
const struct vmac_data *vmac2);
BACNET_STACK_EXPORT
void VMAC_Cleanup(void);
BACNET_STACK_EXPORT