aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNathanael Sensfelder <SpamShield0@MultiAgentSystems.org>2022-02-17 18:39:44 +0100
committerNathanael Sensfelder <SpamShield0@MultiAgentSystems.org>2022-02-17 18:39:44 +0100
commit2f9f45b74bf4287e50dfcf127431735c225252dd (patch)
tree464331b75fdc0934922b597fc31c086d2fce8c97
parent08af514ba3fd9c1d4083c936b677ef74be0ad86b (diff)
downloadmarkov-k-ram-master.zip
markov-k-ram-master.tar.bz2
Fixes JH_index max allocation tests.HEADmaster
-rw-r--r--Makefile1
-rw-r--r--src/core/index.h15
-rw-r--r--src/knowledge/knowledge_learn_markov_sequence.c20
-rw-r--r--src/knowledge/knowledge_learn_sequence.c4
-rw-r--r--src/knowledge/knowledge_learn_word.c9
-rw-r--r--src/knowledge/knowledge_swt_tws_modifications.c3
-rw-r--r--src/server/server_new_connection.c9
7 files changed, 46 insertions, 15 deletions
diff --git a/Makefile b/Makefile
index 40f30dd..aa771c8 100644
--- a/Makefile
+++ b/Makefile
@@ -2,6 +2,7 @@
PTHREADS_LIB_FLAGS ?= -pthread
################################################################################
CFLAGS += -O3
+CFLAGS += -Wall -Wextra -pedantic -Werror
CFLAGS += -D_POSIX_SOURCE
CFLAGS += -D_POSIX_C_SOURCE=200809L
CFLAGS += $(PTHREADS_LIB_FLAGS)
diff --git a/src/core/index.h b/src/core/index.h
index 66b2540..e791805 100644
--- a/src/core/index.h
+++ b/src/core/index.h
@@ -3,6 +3,21 @@
#include "index_types.h"
+#if JH_INDEX_MAX >= SIZE_MAX
+#define JH_index_cannot_allocate_more(type_size, counter) \
+ (\
+ (((size_t) (counter)) == SIZE_MAX) \
+ || ((counter) == JH_INDEX_MAX) \
+ || ((((size_t) (counter)) + 1) > (SIZE_MAX / (type_size))) \
+ )
+#else
+#define JH_index_cannot_allocate_more(type_size, counter) \
+ (\
+ ((counter) == JH_INDEX_MAX) \
+ || ((((size_t) (counter)) + 1) > (SIZE_MAX / (type_size))) \
+ )
+#endif
+
/*
* Returns a random JH_index.
*/
diff --git a/src/knowledge/knowledge_learn_markov_sequence.c b/src/knowledge/knowledge_learn_markov_sequence.c
index 74e29a7..ddb828d 100644
--- a/src/knowledge/knowledge_learn_markov_sequence.c
+++ b/src/knowledge/knowledge_learn_markov_sequence.c
@@ -2,6 +2,8 @@
#include <string.h>
#include <stdint.h> /* defines SIZE_MAX */
+#include "../core/index.h"
+
#include "../sequence/sequence.h"
#include "../error/error.h"
@@ -45,7 +47,14 @@ static int reallocate_sequences_list
{
JH_index ** new_sequences;
- if ((SIZE_MAX / sizeof(JH_index *)) < (size_t) k->sequences_length)
+ if
+ (
+ JH_index_cannot_allocate_more
+ (
+ sizeof(JH_index *),
+ k->sequences_length
+ )
+ )
{
JH_S_ERROR
(
@@ -88,7 +97,14 @@ static int reallocate_sequences_sorted_list
{
JH_index * new_sequences_sorted;
- if ((SIZE_MAX / sizeof(JH_index)) < (size_t) k->sequences_length)
+ if
+ (
+ JH_index_cannot_allocate_more
+ (
+ sizeof(JH_index),
+ k->sequences_length
+ )
+ )
{
JH_S_ERROR
(
diff --git a/src/knowledge/knowledge_learn_sequence.c b/src/knowledge/knowledge_learn_sequence.c
index 9502f2e..d52f4d0 100644
--- a/src/knowledge/knowledge_learn_sequence.c
+++ b/src/knowledge/knowledge_learn_sequence.c
@@ -45,7 +45,6 @@ static int add_swt_sequence
const JH_index sequence [const restrict static 1],
const size_t index,
const size_t sequence_length,
- const JH_index markov_order,
JH_index buffer [const restrict static 1],
const JH_index buffer_length,
FILE io [const restrict static 1]
@@ -132,7 +131,6 @@ static int add_tws_sequence
const JH_index sequence [const restrict static 1],
const size_t index,
const size_t sequence_length,
- const JH_index markov_order,
JH_index buffer [const restrict static 1],
const JH_index buffer_length,
FILE io [const restrict static 1]
@@ -227,7 +225,6 @@ int JH_knowledge_learn_sequence
sequence,
i,
sequence_length,
- markov_order,
buffer,
buffer_length,
io
@@ -246,7 +243,6 @@ int JH_knowledge_learn_sequence
sequence,
i,
sequence_length,
- markov_order,
buffer,
buffer_length,
io
diff --git a/src/knowledge/knowledge_learn_word.c b/src/knowledge/knowledge_learn_word.c
index 9320f10..c84c9b6 100644
--- a/src/knowledge/knowledge_learn_word.c
+++ b/src/knowledge/knowledge_learn_word.c
@@ -2,6 +2,8 @@
#include <string.h>
#include <stdint.h> /* defines SIZE_MAX */
+#include "../core/index.h"
+
#include "../error/error.h"
#include "knowledge.h"
@@ -103,8 +105,11 @@ static int reallocate_words_list
if
(
- (SIZE_MAX / sizeof(struct JH_knowledge_word))
- < (size_t) k->words_length
+ JH_index_cannot_allocate_more
+ (
+ sizeof(struct JH_knowledge_word),
+ k->words_length
+ )
)
{
JH_S_ERROR
diff --git a/src/knowledge/knowledge_swt_tws_modifications.c b/src/knowledge/knowledge_swt_tws_modifications.c
index cc8a938..dfb4705 100644
--- a/src/knowledge/knowledge_swt_tws_modifications.c
+++ b/src/knowledge/knowledge_swt_tws_modifications.c
@@ -10,7 +10,6 @@ static int add_target
(
struct JH_knowledge_sequence_data sd [const restrict static 1],
const JH_index target_id,
- const JH_index s_index,
const JH_index t_index,
FILE io [const restrict static 1]
)
@@ -208,7 +207,6 @@ int JH_knowledge_strengthen_swt
(
&(k->words[word_id].swt.sequences_ref[s_index]),
target_id,
- s_index,
t_index,
io
) < 0
@@ -308,7 +306,6 @@ int JH_knowledge_strengthen_tws
(
&(k->words[word_id].tws.sequences_ref[s_index]),
target_id,
- s_index,
t_index,
io
) < 0
diff --git a/src/server/server_new_connection.c b/src/server/server_new_connection.c
index 23a2770..e08ef56 100644
--- a/src/server/server_new_connection.c
+++ b/src/server/server_new_connection.c
@@ -7,6 +7,8 @@
#include <stdint.h>
#include <unistd.h>
+#include "../core/index.h"
+
#include "../parameters/parameters.h"
#include "server.h"
@@ -63,11 +65,10 @@ static int get_new_thread (struct JH_server server [const restrict static 1])
if
(
- (server->workers.threads_capacity == JH_INDEX_MAX)
- ||
+ JH_index_cannot_allocate_more
(
- (size_t) (server->workers.threads_capacity + 1)
- > (SIZE_MAX / sizeof(struct JH_server_thread_data))
+ sizeof(struct JH_server_thread_data),
+ server->workers.threads_capacity
)
)
{