Skip to content

Commit

Permalink
Modify sanitize-code.sh script
Browse files Browse the repository at this point in the history
  • Loading branch information
saulvaldelvira committed Aug 13, 2024
1 parent 09a5856 commit a9bbc94
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 33 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ TESTFILES = $(wildcard test/*)
EXAMPLES = $(wildcard example/*.c)

CC = cc
CCFLAGS += -Wall -Wextra -pedantic -std=c99 -Wstrict-prototypes -I./include -g -fPIC -O3
CCFLAGS += -Wall -Wextra -pedantic -std=c11 -Wstrict-prototypes -I./include -g -fPIC -O3

AR = ar
ARFLAGS = rcs
Expand Down
6 changes: 3 additions & 3 deletions include/vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ int vector_push_front(vector_t *vector, void *element);
* @param array_length number of elements to copy from array into the vector.
* @return 1 if the operation is successful
*/
int vector_append_array(vector_t *vector, void *array, size_t array_length);
int vector_append_array(vector_t *vector, const void *array, size_t array_length);

/**
* Inserts a batch of elements into the vector.
Expand All @@ -88,15 +88,15 @@ int vector_append_array(vector_t *vector, void *array, size_t array_length);
* @param array_length number of elements to copy from array into the vector.
* @return 1 if the operation is successful
*/
int vector_insert_array(vector_t *vector, ptrdiff_t index, void *array, size_t array_length);
int vector_insert_array(vector_t *vector, ptrdiff_t index, const void *array, size_t array_length);

/**
* Appends a batch of elements to the front of the vector.
* @param array source array
* @param array_length number of elements to copy from array into the vector.
* @return 1 if the operation is successful
*/
int vector_push_front_array(vector_t *vector, void *array, size_t array_length);
int vector_push_front_array(vector_t *vector, const void *array, size_t array_length);

/**
* @return the index of the element in the vector.
Expand Down
4 changes: 2 additions & 2 deletions src/avl_tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ struct avl {

//// INITIALIZE ////////////////////////////////////////////////////////////////

static AVLNode* init_node(void *element, size_t data_size){
static AVLNode* init_node(const void *element, size_t data_size){
AVLNode *node = gdsmalloc(offsetof(AVLNode, info) + data_size);
if (!node) return NULL;
node->left = NULL;
Expand Down Expand Up @@ -349,7 +349,7 @@ static AVLNode* get_rec(AVLNode *node, void *element, comparator_function_t comp

void* avl_get(const avl_t *tree, void *element, void *dest){
assert(tree && element && dest);
AVLNode *node = get_rec(tree->root, element, tree->compare);
const AVLNode *node = get_rec(tree->root, element, tree->compare);
if (!node)
return NULL;
memcpy(dest, node->info, tree->data_size);
Expand Down
8 changes: 4 additions & 4 deletions src/graph.c
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ void* graph_vertex_at(const graph_t *graph, ptrdiff_t index, void *dest){
assert(graph && dest);
if (index < 0 || (size_t)index >= graph->n_elements)
return NULL;
void *src = void_offset(graph->vertices, index * graph->data_size);
const void *src = void_offset(graph->vertices, index * graph->data_size);
return memcpy(dest, src, graph->data_size);
}

Expand Down Expand Up @@ -347,7 +347,7 @@ static void graph_init_dijkstra(DijkstraData_t *dijkstra, const graph_t *graph,
* @param D an array of weights
* @param n_elements the number of elements in the arrays
*/
static ptrdiff_t graph_get_pivot(uint8_t *S, float *D, size_t n_elements){
static ptrdiff_t graph_get_pivot(const uint8_t *S, const float *D, size_t n_elements){
ptrdiff_t pivot = -1;
float min = INFINITY;
for (size_t i = 0; i < n_elements; i++){
Expand Down Expand Up @@ -556,7 +556,7 @@ float graph_eccentricity(const graph_t *graph, void *vertex){
static int traverse_df_rec(graph_traversal_t *data, size_t index, uint8_t *visited, const graph_t *graph){
visited[index] = 1;
void *dst = void_offset(data->elements, data->elements_size * graph->data_size);
void *src = void_offset(graph->vertices, index * graph->data_size);
const void *src = void_offset(graph->vertices, index * graph->data_size);
memcpy(dst, src, graph->data_size);
data->elements_size++;
int s;
Expand Down Expand Up @@ -638,7 +638,7 @@ graph_traversal_t graph_traverse_BF(const graph_t *graph, void *vertex){
size_t piv = *start++;

// Copy it into result array
void *src = void_offset(graph->vertices, piv * graph->data_size);
const void *src = void_offset(graph->vertices, piv * graph->data_size);
memcpy(dst, src, graph->data_size);
dst = void_offset(dst, graph->data_size);
bf.elements_size++;
Expand Down
4 changes: 2 additions & 2 deletions src/linked_list.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ int list_set(linked_list_t *list, void *element, void *replacement){

void* list_get(const linked_list_t *list, void *element, void *dest){
assert(list && element && dest);
LLNode *aux = list->head;
const LLNode *aux = list->head;
while (aux != NULL){
if (list->compare(aux->info, element) == 0)
return memcpy(dest, aux->info, list->data_size);
Expand All @@ -158,7 +158,7 @@ void* list_get_into_array(const linked_list_t *list, void *array, size_t array_l
assert(list && array);
if (array_length > list->n_elements)
array_length = list->n_elements;
LLNode *aux = list->head;
const LLNode *aux = list->head;
void *dst = array;
while (array_length-- > 0){
memcpy(dst, aux->info, list->data_size);
Expand Down
2 changes: 1 addition & 1 deletion src/queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ void queue_set_destructor(queue_t *queue, destructor_function_t destructor){
queue->destructor = destructor;
}

static queue_tNode* queue_init_node(void *element, size_t size){
static queue_tNode* queue_init_node(const void *element, size_t size){
queue_tNode *node = gdsmalloc(offsetof(queue_tNode, info) + size);
if (!node) return NULL;
memcpy(node->info, element, size);
Expand Down
8 changes: 4 additions & 4 deletions src/vector.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ int vector_push_front(vector_t *vector, void *element){
return vector_insert_at(vector, 0, element);
}

int vector_insert_array(vector_t *vector, ptrdiff_t index, void *array, size_t array_length){
int vector_insert_array(vector_t *vector, ptrdiff_t index, const void *array, size_t array_length){
assert(vector && array);
if (vector->capacity - vector->n_elements < array_length){
if (resize_buffer(vector, vector->capacity + array_length) == GDS_ERROR)
Expand All @@ -140,13 +140,13 @@ int vector_insert_array(vector_t *vector, ptrdiff_t index, void *array, size_t a
return GDS_SUCCESS;
}

int vector_append_array(vector_t *vector, void *array, size_t array_length){
int vector_append_array(vector_t *vector, const void *array, size_t array_length){
assert(vector);
return vector_insert_array(vector, vector->n_elements, array, array_length);
}

__inline
int vector_push_front_array(vector_t *vector, void *array, size_t array_length){
int vector_push_front_array(vector_t *vector, const void *array, size_t array_length){
return vector_insert_array(vector, 0, array, array_length);
}

Expand Down Expand Up @@ -380,7 +380,7 @@ static void* __get_at(const vector_t *vector, ptrdiff_t index) {

void* vector_at(const vector_t *vector, ptrdiff_t index, void *dest){
assert(vector && dest);
void *tmp = __get_at(vector, index);
const void *tmp = __get_at(vector, index);
return tmp ? memcpy(dest, tmp, vector->data_size) : NULL;
}

Expand Down
23 changes: 7 additions & 16 deletions test/sanitize-code.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,19 @@ fi
make clean
make CC=gcc FLAGS="-fsanitize=address,undefined,leak -Werror"

failures=""
export failures
find . -name '*.c' |
if cppcheck --help | grep check-level ; then
CHECK_LEVEL=--check-level=exhaustive
fi

find src -name '*.c' |
while read file
do
cppcheck -j$(nproc) \
--enable=style,performance,portability \
--error-exitcode=1 \
--suppress=constVariable \
--suppress=constParameter \
--suppress=constParameterPointer\
--suppress=constVariablePointer \
--check-level=exhaustive \
--suppress=constParameterPointer \
--std=c99 \
$CHECK_LEVEL \
$file
if [ $? -ne 0 ] ; then
failures+="$file,"
fi
done

if [[ $failures != "" ]] ; then
echo "[CPPCHECK] Failed on: $failures" ;
else
echo "[CPPCHECK] Finished with no errors" ;
fi

0 comments on commit a9bbc94

Please sign in to comment.