aboutsummaryrefslogtreecommitdiff
path: root/src/solvers/h48
diff options
context:
space:
mode:
authorSebastiano Tronto <sebastiano@tronto.net>2024-10-12 17:06:50 +0200
committerSebastiano Tronto <sebastiano@tronto.net>2024-10-12 17:06:50 +0200
commitef087c3849cfbe58f4f77e09367d6fbf152e5498 (patch)
tree50887156f53a784f5d8f9bdadc5e5ed727365fe6 /src/solvers/h48
parent37a48208d419a6c2797f5705ba37d7b362fcb8fe (diff)
downloadnissy-core-ef087c3849cfbe58f4f77e09367d6fbf152e5498.tar.gz
nissy-core-ef087c3849cfbe58f4f77e09367d6fbf152e5498.zip
Make writemoves (and solver) safer by checking buffer size
Diffstat (limited to 'src/solvers/h48')
-rw-r--r--src/solvers/h48/solve.h22
-rw-r--r--src/solvers/h48/solve_multithread.h23
2 files changed, 38 insertions, 7 deletions
diff --git a/src/solvers/h48/solve.h b/src/solvers/h48/solve.h
index f2babdf..bcc8b33 100644
--- a/src/solvers/h48/solve.h
+++ b/src/solvers/h48/solve.h
@@ -10,6 +10,7 @@ typedef struct {
10 uint8_t k; 10 uint8_t k;
11 const uint32_t *cocsepdata; 11 const uint32_t *cocsepdata;
12 const uint8_t *h48data; 12 const uint8_t *h48data;
13 uint64_t solutions_size;
13 char **nextsol; 14 char **nextsol;
14 uint8_t nissbranch; 15 uint8_t nissbranch;
15 int8_t npremoves; 16 int8_t npremoves;
@@ -55,26 +56,40 @@ allowednextmove_h48(uint8_t *moves, uint8_t n, uint32_t h48branch)
55STATIC void 56STATIC void
56solve_h48_appendsolution(dfsarg_solveh48_t *arg) 57solve_h48_appendsolution(dfsarg_solveh48_t *arg)
57{ 58{
58 int strl; 59 int64_t strl;
59 uint8_t invertedpremoves[MAXLEN]; 60 uint8_t invertedpremoves[MAXLEN];
60 char *solution = *arg->nextsol; 61 char *solution = *arg->nextsol;
61 62
62 strl = writemoves(arg->moves, arg->nmoves, *arg->nextsol); 63 strl = writemoves(
64 arg->moves, arg->nmoves, arg->solutions_size, *arg->nextsol);
65
66 if (strl < 0)
67 goto solve_h48_appendsolution_error;
63 *arg->nextsol += strl; 68 *arg->nextsol += strl;
69 arg->solutions_size -= strl;
64 70
65 if (arg->npremoves) { 71 if (arg->npremoves) {
66 **arg->nextsol = ' '; 72 **arg->nextsol = ' ';
67 (*arg->nextsol)++; 73 (*arg->nextsol)++;
68 74
69 invertmoves(arg->premoves, arg->npremoves, invertedpremoves); 75 invertmoves(arg->premoves, arg->npremoves, invertedpremoves);
70 strl = writemoves(invertedpremoves, arg->npremoves, *arg->nextsol); 76 strl = writemoves(invertedpremoves,
77 arg->npremoves, arg->solutions_size, *arg->nextsol);
78
79 if (strl < 0)
80 goto solve_h48_appendsolution_error;
71 *arg->nextsol += strl; 81 *arg->nextsol += strl;
82 arg->solutions_size -= strl;
72 } 83 }
73 LOG("Solution found: %s\n", solution); 84 LOG("Solution found: %s\n", solution);
74 85
75 **arg->nextsol = '\n'; 86 **arg->nextsol = '\n';
76 (*arg->nextsol)++; 87 (*arg->nextsol)++;
77 (*arg->nsols)++; 88 (*arg->nsols)++;
89
90solve_h48_appendsolution_error:
91 /* We could add some logging, but writemoves() already does */
92 return;
78} 93}
79 94
80STATIC_INLINE bool 95STATIC_INLINE bool
@@ -188,6 +203,7 @@ solve_h48(
188 .k = info.bits, 203 .k = info.bits,
189 .cocsepdata = get_cocsepdata_constptr(data), 204 .cocsepdata = get_cocsepdata_constptr(data),
190 .h48data = get_h48data_constptr(data), 205 .h48data = get_h48data_constptr(data),
206 .solutions_size = solutions_size,
191 .nextsol = &solutions 207 .nextsol = &solutions
192 }; 208 };
193 209
diff --git a/src/solvers/h48/solve_multithread.h b/src/solvers/h48/solve_multithread.h
index 3ba914a..8e00404 100644
--- a/src/solvers/h48/solve_multithread.h
+++ b/src/solvers/h48/solve_multithread.h
@@ -27,12 +27,17 @@ STATIC void
27solve_h48_appendsolution_thread(dfsarg_solveh48_t *arg, task_queue_t *tq) 27solve_h48_appendsolution_thread(dfsarg_solveh48_t *arg, task_queue_t *tq)
28{ 28{
29 pthread_mutex_lock(&tq->mutex); 29 pthread_mutex_lock(&tq->mutex);
30 int strl = 0; 30 int64_t strl = 0;
31 uint8_t invertedpremoves[MAXLEN]; 31 uint8_t invertedpremoves[MAXLEN];
32 char *solution = *arg->nextsol; 32 char *solution = *arg->nextsol;
33 33
34 strl = writemoves(arg->moves, arg->nmoves, *arg->nextsol); 34 strl = writemoves(
35 arg->moves, arg->nmoves, arg->solutions_size, *arg->nextsol);
36
37 if (strl < 0)
38 goto solve_h48_appendsolution_thread_error;
35 *arg->nextsol += strl; 39 *arg->nextsol += strl;
40 arg->solutions_size -= strl;
36 41
37 if (arg->npremoves) 42 if (arg->npremoves)
38 { 43 {
@@ -40,14 +45,22 @@ solve_h48_appendsolution_thread(dfsarg_solveh48_t *arg, task_queue_t *tq)
40 (*arg->nextsol)++; 45 (*arg->nextsol)++;
41 46
42 invertmoves(arg->premoves, arg->npremoves, invertedpremoves); 47 invertmoves(arg->premoves, arg->npremoves, invertedpremoves);
43 strl = writemoves(invertedpremoves, arg->npremoves, *arg->nextsol); 48 strl = writemoves(invertedpremoves,
49 arg->npremoves, arg->solutions_size, *arg->nextsol);
50
51 if (strl < 0)
52 goto solve_h48_appendsolution_thread_error;
44 *arg->nextsol += strl; 53 *arg->nextsol += strl;
54 arg->solutions_size -= strl;
45 } 55 }
46 LOG("Solution found: %s\n", solution); 56 LOG("Solution found: %s\n", solution);
47 57
48 **arg->nextsol = '\n'; 58 **arg->nextsol = '\n';
49 (*arg->nextsol)++; 59 (*arg->nextsol)++;
50 (*arg->nsols)++; 60 (*arg->nsols)++;
61
62solve_h48_appendsolution_thread_error:
63 /* We could add some logging, but writemoves() already does */
51 pthread_mutex_unlock(&tq->mutex); 64 pthread_mutex_unlock(&tq->mutex);
52} 65}
53 66
@@ -264,7 +277,9 @@ solve_h48_multithread(
264 .k = info.bits, 277 .k = info.bits,
265 .cocsepdata = get_cocsepdata_constptr(data), 278 .cocsepdata = get_cocsepdata_constptr(data),
266 .h48data = get_h48data_constptr(data), 279 .h48data = get_h48data_constptr(data),
267 .nextsol = &solutions}; 280 .solutions_size = solutions_size,
281 .nextsol = &solutions
282 };
268 283
269 task_queue_t q; 284 task_queue_t q;
270 init_queue(&q); 285 init_queue(&q);

Generated with cgit - Back to sebastiano.tronto.net