Skip to content

Commit

Permalink
Fix missing malloc() type casting
Browse files Browse the repository at this point in the history
  • Loading branch information
phaag committed Feb 25, 2024
1 parent 07136b3 commit 4115a8b
Show file tree
Hide file tree
Showing 17 changed files with 60 additions and 59 deletions.
4 changes: 2 additions & 2 deletions src/collector/launch.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2009-2022, Peter Haag
* Copyright (c) 2009-2024, Peter Haag
* Copyright (c) 2004-2008, SWITCH - Teleinformatikdienste fuer Lehre und Forschung
* All rights reserved.
*
Expand Down Expand Up @@ -160,7 +160,7 @@ static char *cmd_expand(char *launch_process, launcher_args_t *launcher_args) {
s = NULL;
}
if (s) {
q = realloc(q, strlen(q) + strlen(s));
q = (char *)realloc(q, strlen(q) + strlen(s));
if (!q) {
LogError("realloc() error in %s:%i: %s", __FILE__, __LINE__, strerror(errno));
return NULL;
Expand Down
10 changes: 5 additions & 5 deletions src/collector/metric.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023, Peter Haag
* Copyright (c) 2024, Peter Haag
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -95,8 +95,8 @@ static inline metric_record_t *GetMetric(char *ident, uint32_t exporterID) {
}

dbg_printf("New metric: %s, %x\n", ident, exporterID);
metric_chain = malloc(sizeof(metric_chain_t));
metric_record_t *metric_record = calloc(1, sizeof(metric_record_t));
metric_chain = (metric_chain_t *)calloc(1, sizeof(metric_chain_t));
metric_record_t *metric_record = (metric_record_t *)calloc(1, sizeof(metric_record_t));
if (!metric_chain || !metric_record) {
LogError("calloc() error in %s line %d: %s", __FILE__, __LINE__, strerror(errno));
return NULL;
Expand Down Expand Up @@ -214,7 +214,7 @@ __attribute__((noreturn)) void *MetricThread(void *arg) {
dbg_printf("Started MetricThread\n");
void *message = malloc(sizeof(message_header_t) + sizeof(metric_record_t));
if (!message) {
LogError("calloc() error in %s line %d: %s", __FILE__, __LINE__, strerror(errno));
LogError("malloc() error in %s line %d: %s", __FILE__, __LINE__, strerror(errno));
pthread_exit(NULL);
}
time_t interval = 60;
Expand Down Expand Up @@ -261,7 +261,7 @@ __attribute__((noreturn)) void *MetricThread(void *arg) {
void *_message = realloc(message, numMetrics * sizeof(metric_record_t) + sizeof(message_header_t));
if (!_message) {
pthread_mutex_unlock(&mutex);
LogError("calloc() error in %s line %d: %s", __FILE__, __LINE__, strerror(errno));
LogError("realloc() error in %s line %d: %s", __FILE__, __LINE__, strerror(errno));
pthread_exit(NULL);
}
message = _message;
Expand Down
6 changes: 3 additions & 3 deletions src/collector/nfstatfile.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2009-2019, Peter Haag
* Copyright (c) 2009-2024, Peter Haag
* Copyright (c) 2004-2008, SWITCH - Teleinformatikdienste fuer Lehre und Forschung
* All rights reserved.
*
Expand Down Expand Up @@ -245,9 +245,9 @@ int ReadStatInfo(char *dirname, dirstat_t **dirstat_p, int lock) {
if (next_free >= stack_max_entries) {
dirstat_env_t *tmp;
int i;
tmp = realloc((void *)dirstat_stack, (stack_max_entries + STACK_BLOCK_SIZE) * sizeof(dirstat_env_t));
tmp = (dirstat_env_t *)realloc((void *)dirstat_stack, (stack_max_entries + STACK_BLOCK_SIZE) * sizeof(dirstat_env_t));
if (!tmp) {
LogError("ralloc() error in %s line %d: %s\n", __FILE__, __LINE__, strerror(errno));
LogError("realloc() error in %s line %d: %s\n", __FILE__, __LINE__, strerror(errno));
return ERR_FAIL;
}
dirstat_stack = tmp;
Expand Down
18 changes: 11 additions & 7 deletions src/collector/privsep.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023, Peter Haag
* Copyright (c) 2024, Peter Haag
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -60,9 +60,9 @@ static void IntHandler(int signal) {
} // End of IntHandler

messageQueue_t *NewMessageQueue(void) {
messageQueue_t *messageQueue = calloc(1, sizeof(messageQueue_t));
messageQueue_t *messageQueue = (messageQueue_t *)calloc(1, sizeof(messageQueue_t));
if (!messageQueue) {
LogError("malloc() error in %s line %d: %s\n", __FILE__, __LINE__, strerror(errno));
LogError("calloc() error in %s line %d: %s\n", __FILE__, __LINE__, strerror(errno));
return NULL;
}
int err = 0;
Expand All @@ -79,12 +79,12 @@ messageQueue_t *NewMessageQueue(void) {
} // End of NewMessageQueue

void pushMessage(messageQueue_t *messageQueue, message_t *message) {
messageList_t *listElement = malloc(sizeof(messageList_t));
messageList_t *listElement = (messageList_t *)malloc(sizeof(messageList_t));
if (!listElement) {
LogError("malloc() error in %s line %d: %s\n", __FILE__, __LINE__, strerror(errno));
return;
}
listElement->message = malloc(message->length);
listElement->message = (message_t *)malloc(message->length);
if (!listElement->message) {
LogError("malloc() error in %s line %d: %s\n", __FILE__, __LINE__, strerror(errno));
return;
Expand Down Expand Up @@ -143,7 +143,7 @@ __attribute__((noreturn)) void *pipeReader(void *arg) {

thread_arg_t *thread_arg = (thread_arg_t *)arg;

char *buffer = malloc(MAXMSGSIZE);
char *buffer = (char *)malloc(MAXMSGSIZE);
int fd = STDIN_FILENO;

struct sigaction act;
Expand Down Expand Up @@ -258,7 +258,11 @@ int PrivsepFork(int argc, char **argv, pid_t *child_pid, char *privname) {
close(0);
dup(pfd[0]);
int i;
char **privargv = calloc(argc + 3, sizeof(char *));
char **privargv = (char **)calloc(argc + 3, sizeof(char *));
if (!privargv) {
LogError("Process_v9: Panic! calloc(): %s line %d: %s", __FILE__, __LINE__, strerror(errno));
exit(255);
}
privargv[0] = argv[0];
for (i = 1; i < argc; i++) privargv[i] = argv[i];
privargv[i++] = "privsep";
Expand Down
2 changes: 1 addition & 1 deletion src/maxmind/maxmind.c
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ static FILE *checkFile(char *fileName, char **fieldNames) {
} // End of checkFile

int Init_MaxMind(void) {
mmHandle = calloc(1, sizeof(mmHandle_t));
mmHandle = (mmHandle_t *)calloc(1, sizeof(mmHandle_t));
if (!mmHandle) {
LogError("malloc() error in %s line %d: %s", __FILE__, __LINE__, strerror(errno));
return 0;
Expand Down
10 changes: 4 additions & 6 deletions src/netflow/ipfix.c
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ static exporterDomain_t *getExporter(FlowSource_t *fs, uint32_t ObservationDomai
// nothing found
*e = (exporterDomain_t *)calloc(1, sizeof(exporterDomain_t));
if (!(*e)) {
LogError("Process_ipfix: Panic! malloc() %s line %d: %s", __FILE__, __LINE__, strerror(errno));
LogError("Process_ipfix: Panic! calloc() %s line %d: %s", __FILE__, __LINE__, strerror(errno));
return NULL;
}
(*e)->info.header.type = ExporterInfoRecordType;
Expand Down Expand Up @@ -587,9 +587,7 @@ static templateList_t *getTemplate(exporterDomain_t *exporter, uint16_t id) {
} // End of getTemplate

static templateList_t *newTemplate(exporterDomain_t *exporter, uint16_t id) {
templateList_t *template;

template = calloc(1, sizeof(templateList_t));
templateList_t *template = (templateList_t *)calloc(1, sizeof(templateList_t));
if (!template) {
LogError("Process_ipfix: Panic! calloc() %s line %d: %s", __FILE__, __LINE__, strerror(errno));
return NULL;
Expand Down Expand Up @@ -805,7 +803,7 @@ static void Process_ipfix_template_add(exporterDomain_t *exporter, void *DataPtr
return;
}

sequence_t *sequenceTable = malloc((count + 4) * sizeof(sequence_t)); // + 2 for IP and time received
sequence_t *sequenceTable = (sequence_t *)malloc((count + 4) * sizeof(sequence_t)); // + 2 for IP and time received
if (!sequenceTable) {
LogError("Process_ipfix: malloc(): %s line %d: %s", __FILE__, __LINE__, strerror(errno));
return;
Expand Down Expand Up @@ -866,7 +864,7 @@ static void Process_ipfix_template_add(exporterDomain_t *exporter, void *DataPtr
LogError("Process_ipfix: abort template add: %s line %d", __FILE__, __LINE__);
return;
}
dataTemplate_t *dataTemplate = calloc(1, sizeof(dataTemplate_t));
dataTemplate_t *dataTemplate = (dataTemplate_t *)calloc(1, sizeof(dataTemplate_t));
if (!dataTemplate) {
LogError("Error calloc(): %s in %s:%d", strerror(errno), __FILE__, __LINE__);
return;
Expand Down
10 changes: 4 additions & 6 deletions src/netflow/netflow_v9.c
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ static inline exporterDomain_t *getExporter(FlowSource_t *fs, uint32_t exporter_
// nothing found
*e = (exporterDomain_t *)calloc(1, sizeof(exporterDomain_t));
if (!(*e)) {
LogError("Process_v9: Panic! malloc() %s line %d: %s", __FILE__, __LINE__, strerror(errno));
LogError("Process_v9: Panic! calloc() %s line %d: %s", __FILE__, __LINE__, strerror(errno));
return NULL;
}
(*e)->info.header.type = ExporterInfoRecordType;
Expand Down Expand Up @@ -550,9 +550,7 @@ static templateList_t *getTemplate(exporterDomain_t *exporter, uint16_t id) {
} // End of getTemplate

static templateList_t *newTemplate(exporterDomain_t *exporter, uint16_t id) {
templateList_t *template;

template = calloc(1, sizeof(templateList_t));
templateList_t *template = (templateList_t *)calloc(1, sizeof(templateList_t));
if (!template) {
LogError("Process_v9: Panic! calloc() %s line %d: %s", __FILE__, __LINE__, strerror(errno));
return NULL;
Expand Down Expand Up @@ -642,7 +640,7 @@ static inline void Process_v9_templates(exporterDomain_t *exporter, void *DataPt
return;
}

sequence_t *sequenceTable = malloc((count + 4) * sizeof(sequence_t)); // + 2 for IP and time received
sequence_t *sequenceTable = (sequence_t *)malloc((count + 4) * sizeof(sequence_t)); // + 2 for IP and time received
if (!sequenceTable) {
LogError("Process_v9: malloc(): %s line %d: %s", __FILE__, __LINE__, strerror(errno));
return;
Expand Down Expand Up @@ -713,7 +711,7 @@ static inline void Process_v9_templates(exporterDomain_t *exporter, void *DataPt
LogError("Process_v9: abort template add: %s line %d", __FILE__, __LINE__);
return;
}
dataTemplate_t *dataTemplate = calloc(1, sizeof(dataTemplate_t));
dataTemplate_t *dataTemplate = (dataTemplate_t *)calloc(1, sizeof(dataTemplate_t));
if (!dataTemplate) {
LogError("Error calloc(): %s in %s:%d", strerror(errno), __FILE__, __LINE__);
return;
Expand Down
6 changes: 4 additions & 2 deletions src/nfdump/blocksort.c
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,8 @@ static void qusort(SortRecord_t *left, SortRecord_t *right) {
if (right - left > 100000 && n_threads < max_threads) {
// start a new thread - max_threads is a soft limit
pthread_t thread;
SortRecord_t **param = malloc(2 * sizeof(SortRecord_t *));
SortRecord_t **param = (SortRecord_t **)malloc(2 * sizeof(SortRecord_t *));
if (!param) abort();
param[0] = left;
param[1] = right;
pthread_mutex_lock(&mutex);
Expand Down Expand Up @@ -256,7 +257,8 @@ void blocksort(SortRecord_t *data, int len) {
max_threads = 8;

pthread_t thread;
SortRecord_t **param = malloc(2 * sizeof(SortRecord_t *));
SortRecord_t **param = (SortRecord_t **)malloc(2 * sizeof(SortRecord_t *));
if (!param) abort();
param[0] = data;
param[1] = data + len - 1;
pthread_create(&thread, NULL, sort_thr, param);
Expand Down
6 changes: 3 additions & 3 deletions src/nfdump/exporter.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ static char *getVersionString(uint16_t nfversion) {

/* functions */
int InitExporterList(void) {
exporter_list = calloc(MAX_EXPORTERS, sizeof(exporter_t *));
exporter_list = (exporter_t **)calloc(MAX_EXPORTERS, sizeof(exporter_t *));
if (!exporter_list) {
LogError("malloc() error in %s line %d: %s\n", __FILE__, __LINE__, strerror(errno));
LogError("calloc() error in %s line %d: %s\n", __FILE__, __LINE__, strerror(errno));
return 0;
}
exporter_root = NULL;
Expand Down Expand Up @@ -259,7 +259,7 @@ int AddExporterStat(exporter_stats_record_t *stat_record) {
int use_copy;
exporter_stats_record_t *rec;
if (((ptrdiff_t)stat_record & 0x7) != 0) {
rec = malloc(stat_record->header.size);
rec = (exporter_stats_record_t *)malloc(stat_record->header.size);
if (!rec) {
LogError("malloc() error in %s line %d: %s\n", __FILE__, __LINE__, strerror(errno));
exit(255);
Expand Down
8 changes: 4 additions & 4 deletions src/nfdump/memhandle.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,15 @@ static MemHandler_t *MemHandler = NULL;
#define MaxMemBlocks 256

static int nfalloc_Init(uint32_t memBlockSize) {
MemHandler = calloc(1, sizeof(MemHandler_t));
MemHandler = (MemHandler_t *)calloc(1, sizeof(MemHandler_t));
if (!MemHandler) {
LogError("malloc() error in %s line %d: %s\n", __FILE__, __LINE__, strerror(errno));
LogError("calloc() error in %s line %d: %s\n", __FILE__, __LINE__, strerror(errno));
return 0;
}

MemHandler->memblock = (void **)calloc(MaxMemBlocks, sizeof(void *));
if (!MemHandler->memblock) {
LogError("malloc() error in %s line %d: %s\n", __FILE__, __LINE__, strerror(errno));
LogError("calloc() error in %s line %d: %s\n", __FILE__, __LINE__, strerror(errno));
return 0;
}

Expand All @@ -88,7 +88,7 @@ static int nfalloc_Init(uint32_t memBlockSize) {

MemHandler->memblock[0] = calloc(1, memBlockSize);
if (!MemHandler->memblock[0]) {
LogError("malloc() error in %s line %d: %s\n", __FILE__, __LINE__, strerror(errno));
LogError("calloc() error in %s line %d: %s\n", __FILE__, __LINE__, strerror(errno));
return 0;
}

Expand Down
8 changes: 4 additions & 4 deletions src/nfdump/nfdump.c
Original file line number Diff line number Diff line change
Expand Up @@ -291,9 +291,9 @@ static stat_record_t process_data(void *engine, char *wfile, int element_stat, i
SetIdent(nffile_w, nffile_r->ident);
}

recordHandle_t *recordHandle = calloc(1, sizeof(recordHandle_t));
recordHandle_t *recordHandle = (recordHandle_t *)calloc(1, sizeof(recordHandle_t));
if (!recordHandle) {
LogError("malloc() error in %s line %d: %s\n", __FILE__, __LINE__, strerror(errno));
LogError("calloc() error in %s line %d: %s\n", __FILE__, __LINE__, strerror(errno));
return stat_record;
}
int done = 0;
Expand Down Expand Up @@ -557,7 +557,7 @@ int main(int argc, char **argv) {
configFile = NULL;
geo_file = getenv("NFGEODB");

outputParams = calloc(1, sizeof(outputParams_t));
outputParams = (outputParams_t *)calloc(1, sizeof(outputParams_t));
if (!outputParams) {
LogError("calloc() error in %s line %d: %s\n", __FILE__, __LINE__, strerror(errno));
exit(EXIT_FAILURE);
Expand Down Expand Up @@ -811,7 +811,7 @@ int main(int argc, char **argv) {
while (argc - optind > 0) {
char *arg = argv[optind++];
CheckArgLen(arg, 128);
filter = realloc(filter, strlen(filter) + strlen(arg) + 2);
filter = (char *)realloc(filter, strlen(filter) + strlen(arg) + 2);
if (!filter) {
LogError("realloc() error in %s line %d: %s", __FILE__, __LINE__, strerror(errno));
exit(EXIT_FAILURE);
Expand Down
10 changes: 5 additions & 5 deletions src/nfdump/nflowcache.c
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,7 @@ static SortElement_t *GetSortList(size_t *size) {
if (hashSize) { // aggregated flows in khash
list = (SortElement_t *)calloc(hashSize, sizeof(SortElement_t));
if (!list) {
LogError("malloc() error in %s line %d: %s\n", __FILE__, __LINE__, strerror(errno));
LogError("calloc() error in %s line %d: %s\n", __FILE__, __LINE__, strerror(errno));
*size = 0;
return NULL;
}
Expand All @@ -722,7 +722,7 @@ static SortElement_t *GetSortList(size_t *size) {
}
list = (SortElement_t *)calloc(listSize, sizeof(SortElement_t));
if (!list) {
LogError("malloc() error in %s line %d: %s\n", __FILE__, __LINE__, strerror(errno));
LogError("calloc() error in %s line %d: %s\n", __FILE__, __LINE__, strerror(errno));
*size = 0;
return NULL;
}
Expand Down Expand Up @@ -878,7 +878,7 @@ char *ParseAggregateMask(char *arg, int hasGeoDB) {
// add format prepend and append length
fmt_len += strlen(AggrPrependFmt) + strlen(AggrAppendFmt) + 6; // +6 for 'fmt:', 2 spaces

char *aggr_fmt = malloc(fmt_len);
char *aggr_fmt = (char *)malloc(fmt_len);
if (!aggr_fmt) {
LogError("malloc() error in %s line %d: %s", __FILE__, __LINE__, strerror(errno));
return 0;
Expand Down Expand Up @@ -1029,8 +1029,8 @@ void InsertFlow(recordHandle_t *recordHandle) {

recordHeaderV3_t *recordHeaderV3 = recordHandle->recordHeaderV3;

FlowHashRecord_t *record = nfmalloc(sizeof(FlowHashRecord_t));
record->flowrecord = nfmalloc(recordHeaderV3->size);
FlowHashRecord_t *record = (FlowHashRecord_t *)nfmalloc(sizeof(FlowHashRecord_t));
record->flowrecord = (recordHeaderV3_t *)nfmalloc(recordHeaderV3->size);
memcpy((void *)record->flowrecord, (void *)recordHeaderV3, recordHeaderV3->size);

record->msecFirst = genericFlow->msecFirst;
Expand Down
6 changes: 3 additions & 3 deletions src/nfsen/nftrack.c
Original file line number Diff line number Diff line change
Expand Up @@ -162,14 +162,14 @@ static data_row *process(void *engine) {

port_table = (data_row *)calloc(65536, sizeof(data_row));
if (!port_table) {
LogError("malloc() allocation error in %s line %d: %s\n", __FILE__, __LINE__, strerror(errno));
LogError("calloc() allocation error in %s line %d: %s\n", __FILE__, __LINE__, strerror(errno));
return NULL;
}

uint32_t processed = 0;
recordHandle_t *recordHandle = calloc(1, sizeof(recordHandle_t));
recordHandle_t *recordHandle = (recordHandle_t *)calloc(1, sizeof(recordHandle_t));
if (!recordHandle) {
LogError("malloc() error in %s line %d: %s\n", __FILE__, __LINE__, strerror(errno));
LogError("calloc() error in %s line %d: %s\n", __FILE__, __LINE__, strerror(errno));
return NULL;
}

Expand Down
2 changes: 1 addition & 1 deletion src/nfsen/nftrack_rrd.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2009-2022, Peter Haag
* Copyright (c) 2009-2024, Peter Haag
* Copyright (c) 2004-2008, SWITCH - Teleinformatikdienste fuer Lehre und Forschung
* All rights reserved.
*
Expand Down
3 changes: 1 addition & 2 deletions src/sflow/sfcapd.c
Original file line number Diff line number Diff line change
Expand Up @@ -279,9 +279,8 @@ static void run(packet_function_t receive_packet, int socket, int pfd, int rfd,
time_t t_start, t_now;
uint32_t ignored_packets;
ssize_t cnt;
void *in_buff;

in_buff = malloc(NETWORK_INPUT_BUFF_SIZE);
void *in_buff = malloc(NETWORK_INPUT_BUFF_SIZE);
if (!in_buff) {
LogError("malloc() allocation error in %s line %d: %s", __FILE__, __LINE__, strerror(errno));
return;
Expand Down
6 changes: 3 additions & 3 deletions src/test/nfgen.c
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,10 @@ int main(int argc, char **argv) {
exit(255);
}

recordHeaderV3_t *record = calloc(1, 4096);
recordHandle_t *recordHandle = calloc(1, sizeof(recordHandle_t));
recordHeaderV3_t *record = (recordHeaderV3_t *)calloc(1, 4096);
recordHandle_t *recordHandle = (recordHandle_t *)calloc(1, sizeof(recordHandle_t));
if (!record || !recordHandle) {
perror("malloc() failed:");
perror("calloc() failed:");
exit(255);
}

Expand Down
Loading

0 comments on commit 4115a8b

Please sign in to comment.