From 3467b9f087d84ececa2721235208ee9aa21b628d Mon Sep 17 00:00:00 2001 From: Bart House Date: Sat, 12 May 2018 13:59:06 -0700 Subject: [PATCH] Added ntassert() mechanism for Windows based port use. --- include/ntconf.h | 10 ++++++++++ sys/winnt/nttty.c | 16 ++++++++++++++++ sys/winnt/winnt.c | 15 +++++++++++++++ 3 files changed, 41 insertions(+) diff --git a/include/ntconf.h b/include/ntconf.h index 534fa6e5d..c9d2031f8 100644 --- a/include/ntconf.h +++ b/include/ntconf.h @@ -246,4 +246,14 @@ extern int FDECL(set_win32_option, (const char *, const char *)); extern int FDECL(alternative_palette, (char *)); #endif +#ifdef NDEBUG +#define ntassert(expression) ((void)0) +#else +extern void FDECL(ntassert_failed, (const char * exp, const char * file, + int line)); + +#define ntassert(expression) (void)((!!(expression)) || \ + (ntassert_failed(#expression, __FILE__, __LINE__), 0)) +#endif + #endif /* NTCONF_H */ diff --git a/sys/winnt/nttty.c b/sys/winnt/nttty.c index 4fbc11c63..035c2f885 100644 --- a/sys/winnt/nttty.c +++ b/sys/winnt/nttty.c @@ -322,6 +322,10 @@ static void check_buffer_size(int width, int height) static cell_t * buffer_get_cell(console_buffer_t * buffer, int x, int y) { + ntassert(x >= 0 && x < buffer_width); + ntassert(y >= 0 && ((y < buffer_height) || + (y == buffer_height && x == 0))); + return buffer->cells + (buffer_width * y) + x; } @@ -363,6 +367,9 @@ static void back_buffer_flip() static void buffer_fill_to_end(console_buffer_t * buffer, cell_t * src, int x, int y) { + ntassert(x >= 0 && x < buffer_width); + ntassert(y >= 0 && y < buffer_height); + cell_t * dst = buffer_get_cell(buffer, x, y); cell_t * sentinel = buffer_get_cell(buffer, 0, buffer_height); while (dst != sentinel) @@ -374,6 +381,9 @@ static void buffer_fill_to_end(console_buffer_t * buffer, cell_t * src, static void back_buffer_write(cell_t * cell, int x, int y) { + ntassert(x >= 0 && x < buffer_width); + ntassert(y >= 0 && y < buffer_height); + cell_t * dst = buffer_get_cell(&back_buffer, x, y); *dst = *cell; @@ -383,6 +393,9 @@ static void back_buffer_write(cell_t * cell, int x, int y) static void back_buffer_clear_to_end_of_line(int x, int y) { + ntassert(x >= 0 && x < buffer_width); + ntassert(y >= 0 && y < buffer_height); + cell_t * cell; cell_t *sentinel; @@ -834,6 +847,9 @@ int in_ch; void cl_end() { + ntassert(ttyDisplay->curx >= 0 && ttyDisplay->curx < buffer_width); + ntassert(ttyDisplay->cury >= 0 && ttyDisplay->cury < buffer_height); + console.cursor.X = ttyDisplay->curx; console.cursor.Y = ttyDisplay->cury; diff --git a/sys/winnt/winnt.c b/sys/winnt/winnt.c index 826a02d40..023889881 100644 --- a/sys/winnt/winnt.c +++ b/sys/winnt/winnt.c @@ -461,6 +461,21 @@ char *buf; } #endif /* RUNTIME_PORT_ID */ +/* ntassert_failed is called when an ntassert's condition is false */ +void ntassert_failed(const char * exp, const char * file, int line) +{ + char message[128]; + _snprintf(message, sizeof(message), + "NHASSERT(%s) in '%s' at line %d\n", exp, file, line); + + if (IsDebuggerPresent()) { + OutputDebugStringA(message); + DebugBreak(); + } + + error(message); +} + #endif /* WIN32 */ /*winnt.c*/