aboutsummaryrefslogtreecommitdiff
path: root/src/solvers/h48/solve.h
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/solve.h
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/solve.h')
-rw-r--r--src/solvers/h48/solve.h22
1 files changed, 19 insertions, 3 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

Generated with cgit - Back to sebastiano.tronto.net