diff options
author | lonkaars <loek@pipeframe.xyz> | 2022-05-17 22:37:09 +0200 |
---|---|---|
committer | lonkaars <loek@pipeframe.xyz> | 2022-05-17 22:37:09 +0200 |
commit | 154df68cb2a74a3b22456e8ec5af3af54352a41f (patch) | |
tree | f9d2c83b30322d81691ae1a013813add6ec6b99e /robot/errcatch.c | |
parent | 2b75425070662b869c15673623df44e30ce43ebe (diff) |
fix buffer overflow in error handling module
- fixed ring buffer overflow in errcatch module
- fixed naming of commands in sercomm module
- added comments to macro's in consts.h
- properly handle `W2_E_WARN_UNCAUGHT_ERROR` and
`W2_E_WARN_ERR_BUFFER_FULL` (these used to cause infinite loops)
- added buffer full warning code
- added options to hide some simulation messages
- more boilerplate sercomm module code
Diffstat (limited to 'robot/errcatch.c')
-rw-r--r-- | robot/errcatch.c | 25 |
1 files changed, 19 insertions, 6 deletions
diff --git a/robot/errcatch.c b/robot/errcatch.c index 1262a38..f66b2a1 100644 --- a/robot/errcatch.c +++ b/robot/errcatch.c @@ -8,15 +8,25 @@ #include "modes.h" #include "orangutan_shim.h" -w2_s_error *g_w2_error_buffer[W2_E_BUFFER_SIZE] = {}; -uint8_t g_w2_error_index = 0; -uint8_t g_w2_error_offset = 0; +w2_s_error *g_w2_error_buffer[W2_ERROR_BUFFER_SIZE] = {}; +uint8_t g_w2_error_index = 0; +uint8_t g_w2_error_offset = 0; +uint8_t g_w2_error_buffer_full = 0; +uint8_t g_w2_error_uncaught = 0; void w2_errcatch_main() { while (g_w2_error_index != g_w2_error_offset) { w2_s_error *error = g_w2_error_buffer[g_w2_error_offset]; w2_errcatch_handle_error(error); - g_w2_error_offset = (g_w2_error_offset + 1) % W2_E_BUFFER_SIZE; + g_w2_error_offset = (g_w2_error_offset + 1) % W2_ERROR_BUFFER_SIZE; + } + if (g_w2_error_buffer_full) { + w2_errcatch_throw(W2_E_WARN_ERR_BUFFER_FULL); + g_w2_error_buffer_full = 0; + } + if (g_w2_error_uncaught) { + w2_errcatch_throw(W2_E_WARN_UNCAUGHT_ERROR); + g_w2_error_uncaught = 0; } } @@ -30,10 +40,13 @@ w2_s_error *w2_alloc_error(enum w2_e_errorcodes code, uint16_t length, const cha void w2_errcatch_throw(enum w2_e_errorcodes code) { w2_errcatch_throw_msg(code, 0, ""); } void w2_errcatch_throw_msg(enum w2_e_errorcodes code, uint16_t length, const char *message) { + uint8_t next_index = (g_w2_error_index + 1) % W2_ERROR_BUFFER_SIZE; + g_w2_error_buffer_full = next_index == g_w2_error_offset; free(g_w2_error_buffer[g_w2_error_index]); w2_s_error *error = w2_alloc_error(code, length, message); g_w2_error_buffer[g_w2_error_index] = error; - g_w2_error_index = (g_w2_error_index + 1) % W2_E_BUFFER_SIZE; + if (g_w2_error_buffer_full) return; + g_w2_error_index = next_index; } void w2_errcatch_handle_error(w2_s_error *error) { @@ -48,7 +61,7 @@ void w2_errcatch_handle_error(w2_s_error *error) { break; } default: { - w2_errcatch_throw(W2_E_WARN_UNCAUGHT_ERROR); + g_w2_error_uncaught = 1; #ifdef W2_SIM simwarn("Uncaught/unhandled error found with code 0x%02x\n", error->code); #endif |