[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH] Avoid static local variable in fbus-phonet
From: |
Ladislav Michl |
Subject: |
[PATCH] Avoid static local variable in fbus-phonet |
Date: |
Wed, 21 Nov 2018 10:32:53 +0100 |
User-agent: |
Mutt/1.10.1 (2018-07-13) |
Function verify_max_message_len is using static local variable
making it thread unsafe. Move that variable into incoming message
instance.
Signed-off-by: Ladislav Michl <address@hidden>
Reported-by: Peter Koch <address@hidden>
---
common/links/fbus-phonet.c | 26 +++++++++++++-------------
include/links/fbus-phonet.h | 1 +
2 files changed, 14 insertions(+), 13 deletions(-)
diff --git a/common/links/fbus-phonet.c b/common/links/fbus-phonet.c
index 3300ceab..276de1d6 100644
--- a/common/links/fbus-phonet.c
+++ b/common/links/fbus-phonet.c
@@ -47,19 +47,18 @@ static gn_error phonet_send_message(unsigned int
messagesize, unsigned char mess
/*--------------------------------------------*/
-static int verify_max_message_len(int len, char **message_buffer)
+static int verify_max_message_len(int len, phonet_incoming_message *i)
{
- static int max_message_len = 0;
-
- if (len > max_message_len || !*message_buffer) {
- dprintf("overrun, reallocating: %d %d\n", len, max_message_len);
- *message_buffer = realloc(*message_buffer, len + 1);
- max_message_len = len + 1;
+ if (len > i->message_buffer_size || !i->message_buffer) {
+ dprintf("overrun, reallocating: %d %d\n", len,
i->message_buffer_size);
+ i->message_buffer_size = len + 1;
+ i->message_buffer = realloc(i->message_buffer,
i->message_buffer_size);
}
- if (*message_buffer)
- return max_message_len;
- else
- return 0;
+ if (i->message_buffer)
+ return i->message_buffer_size;
+
+ i->message_buffer_size = 0;
+ return 0;
}
@@ -171,7 +170,7 @@ static void phonet_rx_statemachine(unsigned char rx_byte,
struct gn_statemachine
i->message_length = i->message_length + rx_byte;
i->state = FBUS_RX_GetMessage;
i->buffer_count = 0;
- if (!verify_max_message_len(i->message_length,
&(i->message_buffer))) {
+ if (!verify_max_message_len(i->message_length, i)) {
dprintf("PHONET: Failed to allocate memory for larger
buffer\n");
i->message_corrupted = 1;
}
@@ -369,6 +368,7 @@ static void phonet_cleanup(struct gn_statemachine *state)
{
free(FBUSINST(state)->message_buffer);
FBUSINST(state)->message_buffer = NULL;
+ FBUSINST(state)->message_buffer_size = 0;
}
/* Initialise variables and start the link */
@@ -388,7 +388,7 @@ gn_error phonet_initialise(struct gn_statemachine *state)
if ((FBUSINST(state) = calloc(1, sizeof(phonet_incoming_message))) ==
NULL)
return GN_ERR_MEMORYFULL;
- if (!verify_max_message_len(PHONET_FRAME_MAX_LENGTH,
&(FBUSINST(state)->message_buffer))) {
+ if (!verify_max_message_len(PHONET_FRAME_MAX_LENGTH, FBUSINST(state))) {
dprintf("PHONET: Failed to initalize initial incoming buffer
for %d bytes\n", PHONET_FRAME_MAX_LENGTH);
return GN_ERR_MEMORYFULL;
}
diff --git a/include/links/fbus-phonet.h b/include/links/fbus-phonet.h
index 89eaf0ba..88eb06ca 100644
--- a/include/links/fbus-phonet.h
+++ b/include/links/fbus-phonet.h
@@ -48,6 +48,7 @@ typedef struct {
int message_type;
int message_length;
char *message_buffer;
+ int message_buffer_size;
int message_corrupted;
} phonet_incoming_message;
--
2.19.1
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [PATCH] Avoid static local variable in fbus-phonet,
Ladislav Michl <=