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
+1 -1
View File
@@ -272,7 +272,7 @@ bool FIFO_Put(FIFO_BUFFER *b, uint8_t data_byte)
*
* @return true if space available and added, false if not added
*/
bool FIFO_Add(FIFO_BUFFER *b, uint8_t *buffer, unsigned count)
bool FIFO_Add(FIFO_BUFFER *b, const uint8_t *buffer, unsigned count)
{
bool status = false; /* return value */
unsigned index;
+1 -1
View File
@@ -102,7 +102,7 @@ extern "C" {
BACNET_STACK_EXPORT
bool FIFO_Add(
FIFO_BUFFER * b,
uint8_t * data_bytes,
const uint8_t * data_bytes,
unsigned count);
BACNET_STACK_EXPORT
+3 -3
View File
@@ -9,9 +9,9 @@
#include <string.h>
#include "bacnet/basic/sys/filename.h"
char *filename_remove_path(const char *filename_in)
const char *filename_remove_path(const char *filename_in)
{
char *filename_out = (char *)filename_in;
const char *filename_out = filename_in;
/* allow the device ID to be set */
if (filename_in) {
@@ -24,7 +24,7 @@ char *filename_remove_path(const char *filename_in)
filename_out++;
} else {
/* no slash in filename */
filename_out = (char *)filename_in;
filename_out = filename_in;
}
}
+1 -1
View File
@@ -15,7 +15,7 @@ extern "C" {
#endif /* __cplusplus */
BACNET_STACK_EXPORT
char *filename_remove_path(
const char *filename_remove_path(
const char *filename_in);
#ifdef __cplusplus
+4 -4
View File
@@ -133,7 +133,7 @@ void mstimer_restart(struct mstimer *t)
* @param t A pointer to the timer
* @return Non-zero if the timer has expired, zero otherwise.
*/
int mstimer_expired(struct mstimer *t)
int mstimer_expired(const struct mstimer *t)
{
if (t->interval) {
return ((unsigned long)((mstimer_now()) - (t->start + t->interval)) <
@@ -168,7 +168,7 @@ void mstimer_expire(struct mstimer *t)
* @return The time until the timer expires
*
*/
unsigned long mstimer_remaining(struct mstimer *t)
unsigned long mstimer_remaining(const struct mstimer *t)
{
return t->start + t->interval - mstimer_now();
}
@@ -183,7 +183,7 @@ unsigned long mstimer_remaining(struct mstimer *t)
* @return The time elapsed since the last start of the timer
*
*/
unsigned long mstimer_elapsed(struct mstimer *t)
unsigned long mstimer_elapsed(const struct mstimer *t)
{
return mstimer_now() - t->start;
}
@@ -198,7 +198,7 @@ unsigned long mstimer_elapsed(struct mstimer *t)
* @return The interval value
*
*/
unsigned long mstimer_interval(struct mstimer *t)
unsigned long mstimer_interval(const struct mstimer *t)
{
return t->interval;
}
+4 -4
View File
@@ -45,15 +45,15 @@ void mstimer_reset(struct mstimer *t);
BACNET_STACK_EXPORT
void mstimer_restart(struct mstimer *t);
BACNET_STACK_EXPORT
int mstimer_expired(struct mstimer *t);
int mstimer_expired(const struct mstimer *t);
BACNET_STACK_EXPORT
void mstimer_expire(struct mstimer *t);
BACNET_STACK_EXPORT
unsigned long mstimer_remaining(struct mstimer *t);
unsigned long mstimer_remaining(const struct mstimer *t);
BACNET_STACK_EXPORT
unsigned long mstimer_elapsed(struct mstimer *t);
unsigned long mstimer_elapsed(const struct mstimer *t);
BACNET_STACK_EXPORT
unsigned long mstimer_interval(struct mstimer *t);
unsigned long mstimer_interval(const struct mstimer *t);
/* optional callback timer support for embedded systems */
BACNET_STACK_EXPORT
void mstimer_callback(
+6 -5
View File
@@ -150,7 +150,8 @@ volatile void *Ringbuf_Peek(RING_BUFFER const *b)
* @param data_element - find the next element from this one
* @return pointer to the data, or NULL if nothing in the list
*/
volatile void *Ringbuf_Peek_Next(RING_BUFFER const *b, uint8_t *data_element)
volatile void *Ringbuf_Peek_Next(
RING_BUFFER const *b, const uint8_t *data_element)
{
unsigned index; /* list index */
volatile uint8_t *this_element;
@@ -211,7 +212,7 @@ bool Ringbuf_Pop(RING_BUFFER *b, uint8_t *data_element)
* @return true if data was copied, false if list is empty
*/
bool Ringbuf_Pop_Element(
RING_BUFFER *b, uint8_t *this_element, uint8_t *data_element)
RING_BUFFER *b, const uint8_t *this_element, uint8_t *data_element)
{
bool status = false; /* return value */
volatile uint8_t *ring_data = NULL; /* used to help point ring data */
@@ -262,7 +263,7 @@ bool Ringbuf_Pop_Element(
* @param data_element - one element that is copied to the ring buffer
* @return true on successful add, false if not added
*/
bool Ringbuf_Put(RING_BUFFER *b, uint8_t *data_element)
bool Ringbuf_Put(RING_BUFFER *b, const uint8_t *data_element)
{
bool status = false; /* return value */
volatile uint8_t *ring_data = NULL; /* used to help point ring data */
@@ -296,7 +297,7 @@ bool Ringbuf_Put(RING_BUFFER *b, uint8_t *data_element)
* @param data_element - one element to copy to the front of the ring
* @return true on successful add, false if not added
*/
bool Ringbuf_Put_Front(RING_BUFFER *b, uint8_t *data_element)
bool Ringbuf_Put_Front(RING_BUFFER *b, const uint8_t *data_element)
{
bool status = false; /* return value */
volatile uint8_t *ring_data = NULL; /* used to help point ring data */
@@ -351,7 +352,7 @@ volatile void *Ringbuf_Data_Peek(RING_BUFFER *b)
* @return true if the buffer has space and the data element points to the
* same memory previously peeked.
*/
bool Ringbuf_Data_Put(RING_BUFFER *b, volatile uint8_t *data_element)
bool Ringbuf_Data_Put(RING_BUFFER *b, const volatile uint8_t *data_element)
{
bool status = false;
volatile uint8_t *ring_data = NULL; /* used to help point ring data */
+6 -5
View File
@@ -76,23 +76,24 @@ extern "C" {
uint8_t * data_element);
BACNET_STACK_EXPORT
bool Ringbuf_Pop_Element(RING_BUFFER * b,
uint8_t * this_element,
const uint8_t * this_element,
uint8_t * data_element);
BACNET_STACK_EXPORT
bool Ringbuf_Put_Front(RING_BUFFER * b,
uint8_t * data_element);
const uint8_t * data_element);
/* head */
BACNET_STACK_EXPORT
bool Ringbuf_Put(RING_BUFFER * b,
uint8_t * data_element);
const uint8_t * data_element);
/* pair of functions to use head memory directly */
BACNET_STACK_EXPORT
volatile void *Ringbuf_Data_Peek(RING_BUFFER * b);
BACNET_STACK_EXPORT
volatile void *Ringbuf_Peek_Next(RING_BUFFER const *b,
uint8_t * data_element);
const uint8_t * data_element);
BACNET_STACK_EXPORT
bool Ringbuf_Data_Put(RING_BUFFER * b, volatile uint8_t *data_element);
bool Ringbuf_Data_Put(
RING_BUFFER * b, const volatile uint8_t *data_element);
/* Note: element_count must be a power of two */
BACNET_STACK_EXPORT
bool Ringbuf_Init(RING_BUFFER * b,
+4 -4
View File
@@ -34,12 +34,12 @@ char *sbuf_data(STATIC_BUFFER const *b)
return (b ? b->data : NULL);
}
unsigned sbuf_size(STATIC_BUFFER *b)
unsigned sbuf_size(STATIC_BUFFER const *b)
{
return (b ? b->size : 0);
}
unsigned sbuf_count(STATIC_BUFFER *b)
unsigned sbuf_count(STATIC_BUFFER const *b)
{
return (b ? b->count : 0);
}
@@ -47,7 +47,7 @@ unsigned sbuf_count(STATIC_BUFFER *b)
/* returns true if successful, false if not enough room to append data */
bool sbuf_put(STATIC_BUFFER *b, /* static buffer structure */
unsigned offset, /* where to start */
char *data, /* data to place in buffer */
const char *data, /* data to place in buffer */
unsigned data_size)
{ /* how many bytes to add */
bool status = false; /* return value */
@@ -70,7 +70,7 @@ bool sbuf_put(STATIC_BUFFER *b, /* static buffer structure */
/* returns true if successful, false if not enough room to append data */
bool sbuf_append(STATIC_BUFFER *b, /* static buffer structure */
char *data, /* data to place in buffer */
const char *data, /* data to place in buffer */
unsigned data_size)
{ /* how many bytes to add */
unsigned count = 0;
+4 -4
View File
@@ -40,23 +40,23 @@ extern "C" {
/* returns the max size of the data block */
BACNET_STACK_EXPORT
unsigned sbuf_size(
STATIC_BUFFER * b);
STATIC_BUFFER const *b);
/* returns the number of bytes used in the data block */
BACNET_STACK_EXPORT
unsigned sbuf_count(
STATIC_BUFFER * b);
STATIC_BUFFER const *b);
/* returns true if successful, false if not enough room to append data */
BACNET_STACK_EXPORT
bool sbuf_put(
STATIC_BUFFER * b, /* static buffer structure */
unsigned offset, /* where to start */
char *data, /* data to add */
const char *data, /* data to add */
unsigned data_size); /* how many to add */
/* returns true if successful, false if not enough room to append data */
BACNET_STACK_EXPORT
bool sbuf_append(
STATIC_BUFFER * b, /* static buffer structure */
char *data, /* data to append */
const char *data, /* data to append */
unsigned data_size); /* how many to append */
/* returns true if successful, false if count is bigger than size */
BACNET_STACK_EXPORT