Compare commits

..

2 Commits

Author SHA1 Message Date
Vftdan f126a60412 Add a check for seemingly unreachable condition found by scan-build
The logic of finding the event that happend just before the created one
2024-08-15 17:06:20 +02:00
Vftdan 63dabd6fcd Typesafe wrapper around calloc 2024-08-15 16:53:02 +02:00
9 changed files with 19 additions and 12 deletions

View File

@ -35,7 +35,7 @@ load_nodes_section(const config_setting_t *config_section)
if (length <= 0) { if (length <= 0) {
return result; return result;
} }
result.items = calloc(length, sizeof(GraphNodeConfig)); result.items = T_ALLOC(length, GraphNodeConfig);
if (!result.items) { if (!result.items) {
return result; return result;
} }
@ -112,7 +112,7 @@ load_channels_section(const config_setting_t *config_section)
if (length <= 0) { if (length <= 0) {
return result; return result;
} }
result.items = calloc(length, sizeof(GraphChannelConfig)); result.items = T_ALLOC(length, GraphChannelConfig);
if (!result.items) { if (!result.items) {
return result; return result;
} }

3
defs.h
View File

@ -6,11 +6,12 @@
#include <stdlib.h> #include <stdlib.h>
#include <stddef.h> #include <stddef.h>
#define lengthof(arr) (sizeof(arr) / sizeof(*arr))
#define containerof(ptr, contype, membpath) ((contype*)(0 ? (void)(((contype*)NULL)->membpath = *(ptr)) : (void)0, ((char *)(ptr)) - offsetof(contype, membpath))) #define containerof(ptr, contype, membpath) ((contype*)(0 ? (void)(((contype*)NULL)->membpath = *(ptr)) : (void)0, ((char *)(ptr)) - offsetof(contype, membpath)))
// Assuming child type has a field for the base type // Assuming child type has a field for the base type
// So for structs it is usually actual downcast, but for unions it is an upcast // So for structs it is usually actual downcast, but for unions it is an upcast
#define DOWNCAST(contype, basename, ptr) containerof(ptr, contype, as_##basename) #define DOWNCAST(contype, basename, ptr) containerof(ptr, contype, as_##basename)
#define lengthof(arr) (sizeof(arr) / sizeof(*arr)) #define T_ALLOC(count, T) ((T*)calloc(count, sizeof(T)))
#define DEBUG_PRINT_VALUE(x, fmt) fprintf(stderr, #x " = " fmt "\n", x); fflush(stderr) #define DEBUG_PRINT_VALUE(x, fmt) fprintf(stderr, #x " = " fmt "\n", x); fflush(stderr)

View File

@ -1,3 +1,5 @@
#include <stdio.h>
#include <stdlib.h>
#include "events.h" #include "events.h"
EventNode EventNode
@ -32,7 +34,7 @@ event_replicate(EventNode * source, size_t count)
EventNode * EventNode *
event_create(const EventData * content) event_create(const EventData * content)
{ {
EventNode * event = calloc(1, sizeof(EventNode)); EventNode * event = T_ALLOC(1, EventNode);
if (content) { if (content) {
event->data = *content; event->data = *content;
event->data.modifiers = modifier_set_copy(content->modifiers); event->data.modifiers = modifier_set_copy(content->modifiers);
@ -49,6 +51,10 @@ event_create(const EventData * content)
list_pos = &other->next->prev; list_pos = &other->next->prev;
} }
EventNode * prev = *list_pos; EventNode * prev = *list_pos;
if (!prev) {
fprintf(stderr, "*list_pos is NULL\n");
abort();
}
event->next = prev->next; event->next = prev->next;
event->prev = prev; event->prev = prev;
prev->next->prev = event; prev->next->prev = event;

View File

@ -23,7 +23,7 @@ event_filter_list_extend(EventFilterList * lst)
if (lst->values) { if (lst->values) {
new_values = reallocarray(lst->values, capacity, sizeof(EventFilter)); new_values = reallocarray(lst->values, capacity, sizeof(EventFilter));
} else { } else {
new_values = calloc(capacity, sizeof(EventFilter)); new_values = T_ALLOC(capacity, EventFilter);
} }
if (!new_values) { if (!new_values) {
return false; return false;

4
main.c
View File

@ -32,7 +32,7 @@ main(int argc, char ** argv)
exit(1); exit(1);
} }
GraphNode **nodes = calloc(loaded_config.nodes.length, sizeof(GraphNode*)); GraphNode **nodes = T_ALLOC(loaded_config.nodes.length, GraphNode*);
for (size_t i = 0; i < loaded_config.nodes.length; ++i) { for (size_t i = 0; i < loaded_config.nodes.length; ++i) {
const char* type_name = loaded_config.nodes.items[i].type; const char* type_name = loaded_config.nodes.items[i].type;
if (!type_name) { if (!type_name) {
@ -57,7 +57,7 @@ main(int argc, char ** argv)
} }
} }
GraphChannel *channels = calloc(loaded_config.channels.length, sizeof(GraphChannel)); GraphChannel *channels = T_ALLOC(loaded_config.channels.length, GraphChannel);
for (size_t i = 0; i < loaded_config.channels.length; ++i) { for (size_t i = 0; i < loaded_config.channels.length; ++i) {
const char *node_names[2]; const char *node_names[2];
GraphNode *end_nodes[2] = {NULL, NULL}; GraphNode *end_nodes[2] = {NULL, NULL};

View File

@ -70,7 +70,7 @@ handle_io(EventPositionBase * self, int fd, bool is_output)
static GraphNode * static GraphNode *
create(GraphNodeSpecification * spec, GraphNodeConfig * config) create(GraphNodeSpecification * spec, GraphNodeConfig * config)
{ {
EvdevGraphNode * node = calloc(1, sizeof(EvdevGraphNode)); EvdevGraphNode * node = T_ALLOC(1, EvdevGraphNode);
if (!node) { if (!node) {
return NULL; return NULL;
} }

View File

@ -50,7 +50,7 @@ handle_io(EventPositionBase * self, int fd, bool is_output)
static GraphNode * static GraphNode *
create(GraphNodeSpecification * spec, GraphNodeConfig * config) create(GraphNodeSpecification * spec, GraphNodeConfig * config)
{ {
GetcharGraphNode * node = calloc(1, sizeof(GetcharGraphNode)); GetcharGraphNode * node = T_ALLOC(1, GetcharGraphNode);
if (!node) { if (!node) {
return NULL; return NULL;
} }

View File

@ -30,7 +30,7 @@ static GraphNode *
create(GraphNodeSpecification * spec, GraphNodeConfig * config) create(GraphNodeSpecification * spec, GraphNodeConfig * config)
{ {
(void) config; (void) config;
GraphNode * node = calloc(1, sizeof(GraphNode)); GraphNode * node = T_ALLOC(1, GraphNode);
if (!node) { if (!node) {
return node; return node;
} }

View File

@ -35,8 +35,8 @@ io_subscription_list_init(IOSubscriptionList * lst, size_t capacity)
.fds = NULL, .fds = NULL,
.subscribers = NULL, .subscribers = NULL,
}; };
result.fds = calloc(capacity, sizeof(int)); result.fds = T_ALLOC(capacity, int);
result.subscribers = calloc(capacity, sizeof(IOHandling*)); result.subscribers = T_ALLOC(capacity, IOHandling*);
if (!result.fds || !result.subscribers) if (!result.fds || !result.subscribers)
capacity = 0; capacity = 0;
result.capacity = capacity; result.capacity = capacity;