aboutsummaryrefslogtreecommitdiff
path: root/src/core/io_moves.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/core/io_moves.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/core/io_moves.h')
-rw-r--r--src/core/io_moves.h27
1 files changed, 21 insertions, 6 deletions
diff --git a/src/core/io_moves.h b/src/core/io_moves.h
index a49a38b..be43568 100644
--- a/src/core/io_moves.h
+++ b/src/core/io_moves.h
@@ -1,6 +1,6 @@
1STATIC uint8_t readmove(char); 1STATIC uint8_t readmove(char);
2STATIC uint8_t readmodifier(char); 2STATIC uint8_t readmodifier(char);
3STATIC int writemoves(uint8_t *, int, char *); 3STATIC int64_t writemoves(uint8_t *, int, uint64_t, char *);
4 4
5STATIC uint8_t 5STATIC uint8_t
6readmove(char c) 6readmove(char c)
@@ -38,18 +38,29 @@ readmodifier(char c)
38 } 38 }
39} 39}
40 40
41STATIC int 41STATIC int64_t
42writemoves(uint8_t *m, int n, char *buf) 42writemoves(uint8_t *m, int n, uint64_t buf_size, char *buf)
43{ 43{
44 int i; 44 int i;
45 size_t len; 45 uint64_t len;
46 const char *s; 46 const char *s;
47 char *b; 47 char *b;
48 48
49 for (i = 0, b = buf; i < n; i++, b++) { 49 if (buf_size == 0) {
50 LOG("Error: cannot write moves to buffer of size 0.\n");
51 return NISSY_ERROR_BUFFER_SIZE;
52 }
53
54 for (i = 0, b = buf; i < n; i++, b++, buf_size--) {
50 s = movestr[m[i]]; 55 s = movestr[m[i]];
51 len = strlen(s); 56 len = strlen(s);
57 if (len >= buf_size) {
58 LOG("Error: the given buffer is too small for "
59 "writing the given moves.\n");
60 goto writemoves_error;
61 }
52 memcpy(b, s, len); 62 memcpy(b, s, len);
63 buf_size -= len;
53 b += len; 64 b += len;
54 *b = ' '; 65 *b = ' ';
55 } 66 }
@@ -58,5 +69,9 @@ writemoves(uint8_t *m, int n, char *buf)
58 b--; /* Remove last space */ 69 b--; /* Remove last space */
59 *b = '\0'; 70 *b = '\0';
60 71
61 return b - buf; 72 return buf_size;
73
74writemoves_error:
75 *buf = '\0';
76 return NISSY_ERROR_BUFFER_SIZE;
62} 77}

Generated with cgit - Back to sebastiano.tronto.net