diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2024-10-12 17:06:50 +0200 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2024-10-12 17:06:50 +0200 |
| commit | ef087c3849cfbe58f4f77e09367d6fbf152e5498 (patch) | |
| tree | 50887156f53a784f5d8f9bdadc5e5ed727365fe6 /src/solvers/h48/solve_multithread.h | |
| parent | 37a48208d419a6c2797f5705ba37d7b362fcb8fe (diff) | |
| download | nissy-core-ef087c3849cfbe58f4f77e09367d6fbf152e5498.tar.gz nissy-core-ef087c3849cfbe58f4f77e09367d6fbf152e5498.zip | |
Make writemoves (and solver) safer by checking buffer size
Diffstat (limited to 'src/solvers/h48/solve_multithread.h')
| -rw-r--r-- | src/solvers/h48/solve_multithread.h | 23 |
1 files changed, 19 insertions, 4 deletions
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 | |||
| 27 | solve_h48_appendsolution_thread(dfsarg_solveh48_t *arg, task_queue_t *tq) | 27 | solve_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 | |||
| 62 | solve_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); |
