commit 182e2d45678d0487be71370c37b88daca3d2c54b
parent da8fdd4955fd24666643915a6728678e9965a0d3
Author: Sebastiano Tronto <sebastiano@tronto.net>
Date: Sat, 12 Oct 2024 16:23:05 +0200
Make gendata and co safer by checking buffer size
Diffstat:
14 files changed, 173 insertions(+), 129 deletions(-)
diff --git a/src/nissy.c b/src/nissy.c
@@ -394,8 +394,11 @@ nissy_datainfo(
{
uint8_t i;
tableinfo_t info;
+ int64_t ret;
- readtableinfo(data, &info);
+ ret = readtableinfo(data_size, data, &info);
+ if (ret != 0)
+ return ret;
write("\n---------\n\n");
write("Table information for '%s'\n", info.solver);
@@ -446,13 +449,14 @@ nissy_gendata(
return NISSY_ERROR_NULL_POINTER;
}
+ arg.buf_size = data_size;
arg.buf = data;
if (!strncmp(solver, "h48", 3)) {
p = parse_h48_solver(solver, &arg.h, &arg.k);
arg.maxdepth = 20;
if (p != 0)
return NISSY_ERROR_UNKNOWN;
- return (int64_t)gendata_h48(&arg);
+ return gendata_h48(&arg);
} else {
LOG("gendata: unknown solver %s\n", solver);
return NISSY_ERROR_INVALID_SOLVER;
@@ -468,7 +472,10 @@ nissy_checkdata(
char *buf;
tableinfo_t info;
- for (buf = (char *)data; readtableinfo(buf, &info); buf += info.next) {
+ for (buf = (char *)data;
+ readtableinfo(data_size, buf, &info);
+ buf += info.next, data_size -= info.next)
+ {
if (!checkdata(buf, &info)) {
LOG("Error: data for %s is inconsistent with info!\n",
info.solver);
@@ -547,10 +554,10 @@ nissy_solve(
return NISSY_ERROR_INVALID_SOLVER;
} else {
return THREADS > 1 ?
- solve_h48_multithread(c, minmoves,
- maxmoves, maxsols, data, sols) :
- solve_h48(c, minmoves,
- maxmoves, maxsols, data, sols);
+ solve_h48_multithread(c, minmoves, maxmoves,
+ maxsols, data_size, data, sols_size, sols) :
+ solve_h48(c, minmoves, maxmoves, maxsols,
+ data_size, data, sols_size, sols);
}
} else if (!strcmp(solver, "simple")) {
return solve_simple(
diff --git a/src/solvers/h48/gendata_cocsep.h b/src/solvers/h48/gendata_cocsep.h
@@ -1,7 +1,8 @@
STATIC_INLINE bool get_visited(const uint8_t *, int64_t);
STATIC_INLINE void set_visited(uint8_t *, int64_t);
-STATIC size_t gendata_cocsep(void *, uint64_t *, cube_t *);
+STATIC size_t gendata_cocsep(
+ char [static COCSEP_FULLSIZE+INFOSIZE], uint64_t *, cube_t *);
STATIC uint32_t gendata_cocsep_dfs(cocsep_dfs_arg_t *);
STATIC void getdistribution_cocsep(const uint32_t *, uint64_t [static 21]);
@@ -19,7 +20,11 @@ After the data as described above, more auxiliary information is appended:
of positions having that pruning value.
*/
STATIC size_t
-gendata_cocsep(void *buf, uint64_t *selfsim, cube_t *rep)
+gendata_cocsep(
+ char buf[static COCSEP_FULLSIZE+INFOSIZE],
+ uint64_t *selfsim,
+ cube_t *rep
+)
{
uint32_t *buf32, cc;
uint16_t n;
@@ -64,7 +69,7 @@ gendata_cocsep(void *buf, uint64_t *selfsim, cube_t *rep)
info.distribution[i] = cc;
}
- writetableinfo(&info, buf);
+ writetableinfo(&info, COCSEP_FULLSIZE+INFOSIZE, buf);
DBG_ASSERT(n == COCSEP_CLASSES, 0,
"cocsep: computed %" PRIu16 " symmetry classes, "
diff --git a/src/solvers/h48/gendata_h48.h b/src/solvers/h48/gendata_h48.h
@@ -1,14 +1,16 @@
STATIC uint64_t gendata_h48short(gendata_h48short_arg_t *);
-STATIC size_t gendata_h48(gendata_h48_arg_t *);
-STATIC size_t gendata_h48h0k4(gendata_h48_arg_t *);
-STATIC size_t gendata_h48k2(gendata_h48_arg_t *);
+STATIC int64_t gendata_h48(gendata_h48_arg_t *);
+STATIC void gendata_h48h0k4(gendata_h48_arg_t *);
+STATIC void gendata_h48k2(gendata_h48_arg_t *);
+STATIC void gendata_h48k2_realcoord(gendata_h48_arg_t *);
+
STATIC void * gendata_h48h0k4_runthread(void *);
+STATIC void * gendata_h48k2_runthread(void *);
+
STATIC_INLINE void gendata_h48_mark_atomic(gendata_h48_mark_t *);
STATIC_INLINE void gendata_h48_mark(gendata_h48_mark_t *);
STATIC_INLINE bool gendata_h48k2_dfs_stop(cube_t, int8_t, h48k2_dfs_arg_t *);
-STATIC size_t gendata_h48k2_realcoord(gendata_h48_arg_t *);
STATIC void gendata_h48k2_dfs(h48k2_dfs_arg_t *arg);
-STATIC void * gendata_h48k2_runthread(void *);
STATIC tableinfo_t makeinfo_h48k2(gendata_h48_arg_t *);
STATIC void getdistribution_h48(const uint8_t *,
uint64_t [static INFO_DISTRIBUTION_LEN], uint8_t, uint8_t);
@@ -62,75 +64,77 @@ gendata_h48short(gendata_h48short_arg_t *arg)
}
/* Generic function that dispatches to the data generators */
-STATIC size_t
+STATIC int64_t
gendata_h48(gendata_h48_arg_t *arg)
{
+ uint64_t size;
void *cocsepdata_offset;
- size_t cocsepsize, h48size;
+ size_t cocsepsize;
tableinfo_t cocsepinfo;
- cocsepsize = gendata_cocsep(arg->buf, arg->selfsim, arg->crep);
+ if (arg == NULL) {
+ LOG("Error computing H48 data: arg is NULL.\n");
+ return NISSY_ERROR_UNKNOWN;
+ }
- if (arg->buf == NULL) {
- cocsepdata_offset = NULL;
- arg->cocsepdata = NULL;
- arg->h48buf = NULL;
- } else {
- cocsepdata_offset = (char *)arg->buf + INFOSIZE;
- arg->cocsepdata = (uint32_t *)cocsepdata_offset;
- arg->h48buf = (char *)arg->buf + cocsepsize;
+ size = 2*INFOSIZE + COCSEP_FULLSIZE + H48_TABLESIZE(arg->h, arg->k);
+
+ if (arg->buf == NULL)
+ return size; /* Dry-run */
+
+ if (arg->buf_size < size) {
+ LOG("Error computing H48 data: buffer is too small "
+ "(needed %" PRId64 " bytes but received %" PRId64 ")\n",
+ size, arg->buf_size);
+ return NISSY_ERROR_BUFFER_SIZE;
}
- arg->base = 99; // TODO: set this somewhere else
+ cocsepsize = gendata_cocsep(arg->buf, arg->selfsim, arg->crep);
+
+ cocsepdata_offset = (char *)arg->buf + INFOSIZE;
+ arg->cocsepdata = (uint32_t *)cocsepdata_offset;
+ arg->h48buf = (char *)arg->buf + cocsepsize;
+
+ arg->base = 99; /* TODO: set this somewhere else */
if (arg->h == 0 && arg->k == 4) {
- h48size = gendata_h48h0k4(arg);
+ gendata_h48h0k4(arg);
} else if ((arg->h == 0 || arg->h == 11) && arg->k == 2) {
- h48size = gendata_h48k2_realcoord(arg);
+ gendata_h48k2_realcoord(arg);
} else if (arg->k == 2) {
- h48size = gendata_h48k2(arg);
+ gendata_h48k2(arg);
} else {
LOG("Cannot generate data for h = %" PRIu8 " and k = %" PRIu8
" (not implemented yet)\n", arg->h, arg->k);
- goto gendata_h48_error;
+ return NISSY_ERROR_INVALID_SOLVER;
}
- if (arg->buf == NULL)
- goto gendata_h48_return_size;
-
- if (!readtableinfo(arg->buf, &cocsepinfo)) {
+ if (readtableinfo(arg->buf_size, arg->buf, &cocsepinfo) != NISSY_OK) {
LOG("gendata_h48: could not read info for cocsep table\n");
- goto gendata_h48_error;
+ return NISSY_ERROR_UNKNOWN;
}
cocsepinfo.next = cocsepsize;
- if (!writetableinfo(&cocsepinfo, arg->buf)) {
+ if (writetableinfo(&cocsepinfo, arg->buf_size, arg->buf) != NISSY_OK) {
LOG("gendata_h48: could not write info for cocsep table"
" with updated 'next' value\n");
- goto gendata_h48_error;
+ return NISSY_ERROR_UNKNOWN;
}
-gendata_h48_return_size:
- return cocsepsize + h48size;
-
-gendata_h48_error:
- return 0;
+ return size;
}
-STATIC size_t
+STATIC void
gendata_h48h0k4(gendata_h48_arg_t *arg)
{
_Atomic uint8_t *table;
uint8_t val;
int64_t i, sc, done, d, h48max;
- uint64_t t, tt, isize, cc;
+ uint64_t t, tt, isize, cc, bufsize;
h48h0k4_bfs_arg_t bfsarg[THREADS];
pthread_t thread[THREADS];
pthread_mutex_t table_mutex[CHUNKS];
- if (arg->buf == NULL)
- goto gendata_h48h0k4_return_size;
-
arg->info = (tableinfo_t) {
.solver = "h48 solver h = 0, k = 4",
.type = TABLETYPE_PRUNING,
@@ -194,10 +198,8 @@ gendata_h48h0k4(gendata_h48_arg_t *arg)
}
arg->info.maxvalue = d - 1;
- writetableinfo(&arg->info, arg->h48buf);
-
-gendata_h48h0k4_return_size:
- return H48_TABLESIZE(0, 4) + INFOSIZE;
+ bufsize = arg->buf_size - COCSEP_FULLSIZE - INFOSIZE;
+ writetableinfo(&arg->info, bufsize, arg->h48buf);
}
STATIC void *
@@ -258,7 +260,7 @@ gendata_h48h0k4_runthread(void *arg)
return NULL;
}
-STATIC size_t
+STATIC void
gendata_h48k2(gendata_h48_arg_t *arg)
{
static const uint8_t shortdepth = 8;
@@ -313,19 +315,15 @@ gendata_h48k2(gendata_h48_arg_t *arg)
uint8_t t;
uint8_t *table;
int64_t j;
- uint64_t i, ii, inext, count;
+ uint64_t i, ii, inext, count, bufsize;
h48map_t shortcubes;
gendata_h48short_arg_t shortarg;
h48k2_dfs_arg_t dfsarg[THREADS];
pthread_t thread[THREADS];
pthread_mutex_t shortcubes_mutex, table_mutex[CHUNKS];
- if (arg->buf == NULL)
- goto gendata_h48k2_return_size;
-
table = (uint8_t *)arg->h48buf + INFOSIZE;
- if (arg->buf != NULL)
- memset(table, 0xFF, H48_TABLESIZE(arg->h, arg->k));
+ memset(table, 0xFF, H48_TABLESIZE(arg->h, arg->k));
LOG("Computing depth <=%" PRIu8 "\n", shortdepth)
h48map_create(&shortcubes, capacity, randomizer);
@@ -379,10 +377,8 @@ gendata_h48k2(gendata_h48_arg_t *arg)
arg->info.distribution[t]++;
}
- writetableinfo(&arg->info, arg->h48buf);
-
-gendata_h48k2_return_size:
- return H48_TABLESIZE(arg->h, 2) + INFOSIZE;
+ bufsize = arg->buf_size - COCSEP_FULLSIZE - INFOSIZE;
+ writetableinfo(&arg->info, bufsize, arg->h48buf);
}
STATIC void *
@@ -565,11 +561,11 @@ gendata_h48k2_dfs_stop(cube_t cube, int8_t depth, h48k2_dfs_arg_t *arg)
}
}
-STATIC size_t
+STATIC void
gendata_h48k2_realcoord(gendata_h48_arg_t *arg)
{
/* TODO */
- return gendata_h48k2(arg);
+ gendata_h48k2(arg);
}
STATIC void *
@@ -684,6 +680,7 @@ gendata_h48_derive(uint8_t h, const void *fulltable, void *buf)
const uint8_t *h48full;
uint8_t *h48derive;
int64_t i, j, h48max;
+ uint64_t bufsize;
gendata_h48_arg_t arg;
tableinfo_t cocsepinfo, fulltableinfo;
@@ -693,7 +690,9 @@ gendata_h48_derive(uint8_t h, const void *fulltable, void *buf)
fulltableinfo.bits = 2;
fulltableinfo.base = 8;
- readtableinfo_n(fulltable, 2, &fulltableinfo);
+ int64_t TODOlarge = 999999999999; /* TODO: cleanup here */
+
+ readtableinfo_n(TODOlarge, fulltable, 2, &fulltableinfo);
arg.h = h;
arg.k = fulltableinfo.bits;
arg.maxdepth = 20;
@@ -711,13 +710,15 @@ gendata_h48_derive(uint8_t h, const void *fulltable, void *buf)
if (buf == NULL)
goto gendata_h48_derive_return_size;
- if (!readtableinfo(buf, &cocsepinfo)) {
+ bufsize = COCSEP_FULLSIZE + INFOSIZE;
+ if (readtableinfo(bufsize, buf, &cocsepinfo) != NISSY_OK) {
LOG("gendata_h48: could not read info for cocsep table\n");
goto gendata_h48_derive_error;
}
cocsepinfo.next = cocsepsize;
- if (!writetableinfo(&cocsepinfo, buf)) {
+ bufsize = COCSEP_FULLSIZE + INFOSIZE;
+ if (writetableinfo(&cocsepinfo, bufsize, buf) != NISSY_OK) {
LOG("gendata_h48_derive: could not write info for cocsep table"
" with updated 'next' value\n");
goto gendata_h48_derive_error;
@@ -742,7 +743,8 @@ gendata_h48_derive(uint8_t h, const void *fulltable, void *buf)
getdistribution_h48(h48derive, arg.info.distribution, h, arg.k);
- if (!writetableinfo(&arg.info, arg.h48buf)) {
+ bufsize = arg.buf_size - COCSEP_FULLSIZE - INFOSIZE;
+ if (writetableinfo(&arg.info, bufsize, arg.h48buf) != NISSY_OK) {
LOG("gendata_h48_derive: could not write info for table\n");
goto gendata_h48_derive_error;
}
diff --git a/src/solvers/h48/gendata_types_macros.h b/src/solvers/h48/gendata_types_macros.h
@@ -58,6 +58,7 @@ typedef struct {
uint8_t base;
uint8_t maxdepth;
tableinfo_t info;
+ uint64_t buf_size;
void *buf;
void *h48buf;
uint32_t *cocsepdata;
diff --git a/src/solvers/h48/solve.h b/src/solvers/h48/solve.h
@@ -21,7 +21,8 @@ STATIC uint32_t allowednextmove_h48(uint8_t *, uint8_t, uint32_t);
STATIC void solve_h48_appendsolution(dfsarg_solveh48_t *);
STATIC_INLINE bool solve_h48_stop(dfsarg_solveh48_t *);
STATIC int64_t solve_h48_dfs(dfsarg_solveh48_t *);
-STATIC int64_t solve_h48(cube_t, int8_t, int8_t, int8_t, const void *, char *);
+STATIC int64_t solve_h48(cube_t, int8_t, int8_t,
+ int8_t, uint64_t, const void *, uint64_t, char *);
STATIC uint32_t
allowednextmove_h48(uint8_t *moves, uint8_t n, uint32_t h48branch)
@@ -163,7 +164,9 @@ solve_h48(
int8_t minmoves,
int8_t maxmoves,
int8_t maxsolutions,
+ uint64_t data_size,
const void *data,
+ uint64_t solutions_size,
char *solutions
)
{
@@ -171,9 +174,9 @@ solve_h48(
dfsarg_solveh48_t arg;
tableinfo_t info;
- if(!readtableinfo_n(data, 2, &info)) {
+ if(readtableinfo_n(data_size, data, 2, &info) != NISSY_OK) {
LOG("solve_h48: error reading table\n");
- return 0;
+ return NISSY_ERROR_DATA;
}
arg = (dfsarg_solveh48_t) {
diff --git a/src/solvers/h48/solve_multithread.h b/src/solvers/h48/solve_multithread.h
@@ -20,7 +20,8 @@ STATIC void copy_queue(task_queue_t *, task_queue_t *, int, _Atomic int64_t *);
STATIC void *start_thread(void *);
STATIC int64_t solve_h48_bfs(dfsarg_solveh48_t *, task_queue_t *, int8_t);
STATIC int64_t solve_h48_single(dfsarg_solveh48_t *, task_queue_t *);
-STATIC int64_t solve_h48_multithread(cube_t, int8_t, int8_t, int8_t, const void *, char *);
+STATIC int64_t solve_h48_multithread(cube_t, int8_t, int8_t, int8_t,
+ uint64_t, const void *, uint64_t, char *);
STATIC void
solve_h48_appendsolution_thread(dfsarg_solveh48_t *arg, task_queue_t *tq)
@@ -236,8 +237,11 @@ solve_h48_multithread(
int8_t minmoves,
int8_t maxmoves,
int8_t maxsolutions,
+ uint64_t data_size,
const void *data,
- char *solutions)
+ uint64_t solutions_size,
+ char *solutions
+)
{
_Atomic int64_t nsols = 0;
int p_depth = 0;
@@ -245,9 +249,9 @@ solve_h48_multithread(
tableinfo_t info;
pthread_t threads[THREADS];
- if (!readtableinfo_n(data, 2, &info)){
+ if (readtableinfo_n(data_size, data, 2, &info) != NISSY_OK) {
LOG("solve_h48: error reading table\n");
- return 0;
+ return NISSY_ERROR_DATA;
}
arg = (dfsarg_solveh48_t){
diff --git a/src/solvers/tables.h b/src/solvers/tables.h
@@ -1,14 +1,14 @@
/* Type definitions and macros are in a separate file for easier testing */
#include "tables_types_macros.h"
-STATIC uint64_t read_unaligned_u64(const void *);
-STATIC void write_unaligned_u64(void *, uint64_t);
-STATIC bool readtableinfo(const void *, tableinfo_t *);
-STATIC bool readtableinfo_n(const void *, uint8_t, tableinfo_t *);
-STATIC bool writetableinfo(const tableinfo_t *, void *);
+STATIC uint64_t read_unaligned_u64(const char *);
+STATIC void write_unaligned_u64(char *, uint64_t);
+STATIC int64_t readtableinfo(uint64_t, const char *, tableinfo_t *);
+STATIC int64_t readtableinfo_n(uint64_t, const char *, uint8_t, tableinfo_t *);
+STATIC int64_t writetableinfo(const tableinfo_t *, uint64_t, char *);
STATIC uint64_t
-read_unaligned_u64(const void *buf)
+read_unaligned_u64(const char *buf)
{
uint64_t ret;
@@ -18,24 +18,30 @@ read_unaligned_u64(const void *buf)
}
STATIC void
-write_unaligned_u64(void *buf, uint64_t x)
+write_unaligned_u64(char *buf, uint64_t x)
{
memcpy(buf, &x, sizeof(uint64_t));
}
-STATIC bool
-readtableinfo(const void *buf, tableinfo_t *info)
+STATIC int64_t
+readtableinfo(uint64_t buf_size, const char *buf, tableinfo_t *info)
{
size_t i;
if (buf == NULL) {
LOG("Error reading table: buffer is NULL\n");
- return false;
+ return NISSY_ERROR_NULL_POINTER;
+ }
+
+ if (buf_size < INFOSIZE) {
+ LOG("Error reading table: buffer size is too small "
+ "(smaller than INFOSIZE = %" PRId64 ")\n", INFOSIZE);
+ return NISSY_ERROR_BUFFER_SIZE;
}
if (info == NULL) {
LOG("Error reading table info: info struct is NULL\n");
- return false;
+ return NISSY_ERROR_UNKNOWN;
}
for (i = 0; i < INFO_DISTRIBUTION_LEN; i++)
@@ -53,39 +59,53 @@ readtableinfo(const void *buf, tableinfo_t *info)
memcpy(info->solver, OFFSET(buf, INFO_OFFSET_SOLVER),
INFO_SOLVER_STRLEN);
- info->h48h = *OFFSET(buf, INFO_OFFSET_H48H);
- info->bits = *OFFSET(buf, INFO_OFFSET_BITS);
- info->base = *OFFSET(buf, INFO_OFFSET_BASE);
- info->maxvalue = *OFFSET(buf, INFO_OFFSET_MAXVALUE);
+ info->h48h = *(uint8_t *)OFFSET(buf, INFO_OFFSET_H48H);
+ info->bits = *(uint8_t *)OFFSET(buf, INFO_OFFSET_BITS);
+ info->base = *(uint8_t *)OFFSET(buf, INFO_OFFSET_BASE);
+ info->maxvalue = *(uint8_t *)OFFSET(buf, INFO_OFFSET_MAXVALUE);
- return true;
+ return NISSY_OK;
}
-STATIC bool
-readtableinfo_n(const void *buf, uint8_t n, tableinfo_t *info)
+STATIC int64_t
+readtableinfo_n(
+ uint64_t buf_size,
+ const char *buf,
+ uint8_t n,
+ tableinfo_t *info
+)
{
- for ( ; n > 0; n--, buf = (char *)buf + info->next)
- if (!readtableinfo(buf, info))
- return false;
+ int64_t ret;
+
+ for (; n > 0; n--, buf = buf + info->next, buf_size -= info->next)
+ if ((ret = readtableinfo(buf_size, buf, info)) != 0)
+ return ret;
- return true;
+ return NISSY_OK;
}
-STATIC bool
-writetableinfo(const tableinfo_t *info, void *buf)
+STATIC int64_t
+writetableinfo(const tableinfo_t *info, uint64_t data_size, char *buf)
{
size_t i;
bool end;
- uint8_t *c;
+ char *c;
if (buf == NULL) {
LOG("Error writing table: buffer is NULL\n");
- return false;
+ return NISSY_ERROR_NULL_POINTER;
}
if (info == NULL) {
LOG("Error writing table info: provided info is NULL\n");
- return false;
+ return NISSY_ERROR_UNKNOWN;
+ }
+
+ if (data_size < info->fullsize) {
+ LOG("Error writing table: buffer size is too small "
+ "(given %" PRId64 " but table requires %" PRId64 ")\n",
+ data_size, info->fullsize);
+ return NISSY_ERROR_BUFFER_SIZE;
}
for (i = 0; i < INFO_DISTRIBUTION_LEN; i++)
@@ -112,10 +132,10 @@ writetableinfo(const tableinfo_t *info, void *buf)
*c = 0;
}
- *OFFSET(buf, INFO_OFFSET_H48H) = info->h48h;
- *OFFSET(buf, INFO_OFFSET_BITS) = info->bits;
- *OFFSET(buf, INFO_OFFSET_BASE) = info->base;
- *OFFSET(buf, INFO_OFFSET_MAXVALUE) = info->maxvalue;
+ *(uint8_t *)OFFSET(buf, INFO_OFFSET_H48H) = info->h48h;
+ *(uint8_t *)OFFSET(buf, INFO_OFFSET_BITS) = info->bits;
+ *(uint8_t *)OFFSET(buf, INFO_OFFSET_BASE) = info->base;
+ *(uint8_t *)OFFSET(buf, INFO_OFFSET_MAXVALUE) = info->maxvalue;
- return true;
+ return NISSY_OK;
}
diff --git a/src/solvers/tables_types_macros.h b/src/solvers/tables_types_macros.h
@@ -1,11 +1,11 @@
-#define OFFSET(B, K) (((uint8_t *)B) + K)
+#define OFFSET(B, K) (((char *)B) + K)
-#define INFOSIZE 512
-#define INFO_SOLVER_STRLEN 100
-#define INFO_DISTRIBUTION_LEN 21
+#define INFOSIZE INT64_C(512)
+#define INFO_SOLVER_STRLEN INT64_C(100)
+#define INFO_DISTRIBUTION_LEN INT64_C(21)
-#define TABLETYPE_PRUNING 0
-#define TABLETYPE_SPECIAL 1
+#define TABLETYPE_PRUNING UINT64_C(0)
+#define TABLETYPE_SPECIAL UINT64_C(1)
#define INFO_OFFSET_DISTRIBUTION 0
#define INFO_OFFSET_TYPE (INFO_DISTRIBUTION_LEN * sizeof(uint64_t))
diff --git a/test/090_tables_readwrite/00_table.in b/test/090_tables_readwrite/00_table.in
@@ -2,7 +2,7 @@ Test solver
0
512
-100000000000
+512
12345678912345
399999999998
3393
diff --git a/test/090_tables_readwrite/00_table.out b/test/090_tables_readwrite/00_table.out
@@ -2,7 +2,7 @@ Test solver
0
512
-100000000000
+512
12345678912345
399999999998
3393
diff --git a/test/090_tables_readwrite/tables_readwrite_tests.c b/test/090_tables_readwrite/tables_readwrite_tests.c
@@ -1,7 +1,7 @@
#include "../test.h"
-bool readtableinfo(const void *, tableinfo_t *);
-bool writetableinfo(const tableinfo_t *, void *);
+bool readtableinfo(uint64_t, const char *, tableinfo_t *);
+bool writetableinfo(const tableinfo_t *, uint64_t, char *);
uint64_t readn(void) {
char str[STRLENMAX];
@@ -70,7 +70,7 @@ void run(void) {
tableinfo_t expected, actual;
expected = test_readinfo();
- writetableinfo(&expected, buf);
- readtableinfo(buf, &actual);
+ writetableinfo(&expected, INFOSIZE, buf);
+ readtableinfo(INFOSIZE, buf, &actual);
test_writeinfo(actual);
}
diff --git a/test/100_gendata_cocsep/gendata_cocsep_tests.c b/test/100_gendata_cocsep/gendata_cocsep_tests.c
@@ -1,10 +1,12 @@
#include "../test.h"
+#define BUF_SIZE 2000000
+
size_t gendata_cocsep(void *, uint64_t *, cube_t *);
-bool readtableinfo(const void *, tableinfo_t *);
+bool readtableinfo(uint64_t, const char *, tableinfo_t *);
void run(void) {
- char buf[2000000];
+ char buf[BUF_SIZE];
uint32_t i;
uint64_t selfsim[COCSEP_CLASSES];
cube_t rep[COCSEP_CLASSES];
@@ -12,7 +14,7 @@ void run(void) {
tableinfo_t info;
result = gendata_cocsep(buf, selfsim, rep);
- if (!readtableinfo(buf, &info)) {
+ if (readtableinfo(BUF_SIZE, buf, &info) != NISSY_OK) {
printf("Error reading info from table\n");
return;
}
diff --git a/tools/000_gendata/gendata.c b/tools/000_gendata/gendata.c
@@ -19,7 +19,7 @@ run(void) {
default:
nissy_datainfo(size, buf, write_stdout);
consistent = nissy_checkdata(size, buf) == 0;
- expected = check_distribution(solver, buf);
+ expected = check_distribution(solver, size, buf);
if (consistent && expected) {
printf("\n");
printf("Generated %" PRId64 " bytes.\n", size);
diff --git a/tools/expected_distributions.h b/tools/expected_distributions.h
@@ -129,11 +129,11 @@ distribution_equal(const uint64_t *expected, const uint64_t *actual, int n)
}
static bool
-check_cocsep(const void *data)
+check_cocsep(uint64_t data_size, const void *data)
{
tableinfo_t info;
- readtableinfo(data, &info);
+ readtableinfo(data_size, data, &info);
return distribution_equal(
expected_cocsep, info.distribution, info.maxvalue);
}
@@ -151,12 +151,12 @@ unknown_h48(uint8_t h, uint8_t k)
}
STATIC bool
-check_distribution(const char *solver, const void *data)
+check_distribution(const char *solver, uint64_t data_size, const void *data)
{
tableinfo_t info = {0};
if (!strncmp(solver, "h48", 3)) {
- readtableinfo(data, &info);
+ readtableinfo(data_size, data, &info);
if (!distribution_equal(
expected_cocsep, info.distribution, info.maxvalue)) {
printf("ERROR! cocsep distribution is incorrect\n");
@@ -164,7 +164,7 @@ check_distribution(const char *solver, const void *data)
}
printf("cocsep distribution is correct\n");
- readtableinfo_n(data, 2, &info);
+ readtableinfo_n(data_size, data, 2, &info);
if (unknown_h48(info.h48h, info.bits))
goto check_distribution_unknown;