diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2025-03-22 06:43:11 +0100 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2025-03-22 18:45:47 +0100 |
| commit | ce3f1cc0ef9f46d70ab5387b1458e9098b40711d (patch) | |
| tree | 4949745670b829f2211381bf14b8440dcc6ca7b1 /src/core/io_moves.h | |
| parent | 0550a16c1cce868bbc3f3b5ad59f80e35cf2a6cd (diff) | |
| download | nissy-core-ce3f1cc0ef9f46d70ab5387b1458e9098b40711d.tar.gz nissy-core-ce3f1cc0ef9f46d70ab5387b1458e9098b40711d.zip | |
Some safety with move arrays, small refactor appendchar
Diffstat (limited to 'src/core/io_moves.h')
| -rw-r--r-- | src/core/io_moves.h | 17 |
1 files changed, 10 insertions, 7 deletions
diff --git a/src/core/io_moves.h b/src/core/io_moves.h index 806887b..fa91e8b 100644 --- a/src/core/io_moves.h +++ b/src/core/io_moves.h | |||
| @@ -1,6 +1,6 @@ | |||
| 1 | STATIC uint8_t readmove(char); | 1 | STATIC uint8_t readmove(char); |
| 2 | STATIC uint8_t readmodifier(char); | 2 | STATIC uint8_t readmodifier(char); |
| 3 | STATIC int64_t writemoves(uint8_t *, int, uint64_t, char *); | 3 | STATIC int64_t writemoves(size_t n, uint8_t [n], size_t m, char [m]); |
| 4 | 4 | ||
| 5 | STATIC uint8_t | 5 | STATIC uint8_t |
| 6 | readmove(char c) | 6 | readmove(char c) |
| @@ -39,11 +39,14 @@ readmodifier(char c) | |||
| 39 | } | 39 | } |
| 40 | 40 | ||
| 41 | STATIC int64_t | 41 | STATIC int64_t |
| 42 | writemoves(uint8_t *m, int n, uint64_t buf_size, char *buf) | 42 | writemoves( |
| 43 | size_t nmoves, | ||
| 44 | uint8_t m[nmoves], | ||
| 45 | size_t buf_size, | ||
| 46 | char buf[buf_size] | ||
| 47 | ) | ||
| 43 | { | 48 | { |
| 44 | int i; | 49 | size_t i, len, written; |
| 45 | uint64_t len; | ||
| 46 | int64_t written; | ||
| 47 | const char *s; | 50 | const char *s; |
| 48 | char *b; | 51 | char *b; |
| 49 | 52 | ||
| @@ -52,7 +55,7 @@ writemoves(uint8_t *m, int n, uint64_t buf_size, char *buf) | |||
| 52 | return NISSY_ERROR_BUFFER_SIZE; | 55 | return NISSY_ERROR_BUFFER_SIZE; |
| 53 | } | 56 | } |
| 54 | 57 | ||
| 55 | for (i = 0, b = buf, written = 0; i < n; i++, b++, written++) { | 58 | for (i = 0, b = buf, written = 0; i < nmoves; i++, b++, written++) { |
| 56 | s = movestr[m[i]]; | 59 | s = movestr[m[i]]; |
| 57 | len = strlen(s); | 60 | len = strlen(s); |
| 58 | if (len + written >= buf_size) { | 61 | if (len + written >= buf_size) { |
| @@ -70,7 +73,7 @@ writemoves(uint8_t *m, int n, uint64_t buf_size, char *buf) | |||
| 70 | b--; /* Remove last space */ | 73 | b--; /* Remove last space */ |
| 71 | *b = '\0'; | 74 | *b = '\0'; |
| 72 | 75 | ||
| 73 | return written; | 76 | return (int64_t)written; |
| 74 | 77 | ||
| 75 | writemoves_error: | 78 | writemoves_error: |
| 76 | *buf = '\0'; | 79 | *buf = '\0'; |
