diff --git a/acinclude.m4 b/acinclude.m4 index fcdf6b28a..3e4231a1c 100644 --- a/acinclude.m4 +++ b/acinclude.m4 @@ -21,3 +21,4 @@ m4_include([m4.include/mc-vfs.m4]) m4_include([m4.include/mc-version.m4]) m4_include([m4.include/mc-tests.m4]) m4_include([m4.include/mc-i18n.m4]) +m4_include([m4.include/mc-assert.m4]) diff --git a/configure.ac b/configure.ac index 355459876..04423335b 100644 --- a/configure.ac +++ b/configure.ac @@ -161,7 +161,6 @@ AC_CHECK_HEADERS([string.h memory.h limits.h malloc.h \ sys/select.h sys/ioctl.h stropts.h arpa/inet.h \ sys/socket.h]) AC_HEADER_MAJOR -AC_HEADER_ASSERT dnl ############################################################################ @@ -386,6 +385,8 @@ dnl ############################################################################ dnl MC options dnl ############################################################################ +mc_ASSERT + mc_WITH_INTERNAL_EDIT dnl Diff viewer support. @@ -649,6 +650,7 @@ Configuration: Source code location: ${srcdir} Compiler: ${CC} Compiler flags: ${CFLAGS} + Assertions: ${enable_assert} File system: ${vfs_type} ${vfs_flags} Screen library: ${screen_msg} diff --git a/lib/global.h b/lib/global.h index 2ccfd9e57..45396067f 100644 --- a/lib/global.h +++ b/lib/global.h @@ -135,17 +135,6 @@ /* one caused by typing 'exit' or 'logout' in the subshell */ #define SUBSHELL_EXIT 128 -#if 0 -#ifdef MC_ENABLE_DEBUGGING_CODE -#undef NDEBUG -#else -#define NDEBUG -#endif -#ifdef HAVE_ASSERT_H -#include -#endif -#endif - #define MC_ERROR g_quark_from_static_string (PACKAGE) #define DEFAULT_CHARSET "ASCII" diff --git a/lib/strutil.h b/lib/strutil.h index 41125d196..4683850ee 100644 --- a/lib/strutil.h +++ b/lib/strutil.h @@ -6,9 +6,6 @@ #include #include #include -#ifdef HAVE_ASSERT_H -#include /* assert() */ -#endif /* Header file for strutil.c, strutilascii.c, strutil8bit.c, strutilutf8.c. * There are two sort of functions: @@ -599,9 +596,7 @@ str_move (char *dest, const char *src) { size_t n; -#ifdef HAVE_ASSERT_H - assert (dest <= src); -#endif + g_assert (dest <= src); n = strlen (src) + 1; /* + '\0' */ diff --git a/lib/strutil/xstrtol.c b/lib/strutil/xstrtol.c index 6fa4aa198..8dcbe1521 100644 --- a/lib/strutil/xstrtol.c +++ b/lib/strutil/xstrtol.c @@ -24,9 +24,6 @@ need stderr defined if assertion checking is enabled. */ #include -#ifdef HAVE_ASSERT_H -#include -#endif #include #include #include @@ -84,9 +81,7 @@ xstrtoumax (const char *s, char **ptr, int base, uintmax_t * val, const char *va uintmax_t tmp; strtol_error_t err = LONGINT_OK; -#ifdef HAVE_ASSERT_H - assert (0 <= base && base <= 36); -#endif + g_assert (0 <= base && base <= 36); p = (ptr != NULL ? ptr : &t_ptr); diff --git a/m4.include/mc-assert.m4 b/m4.include/mc-assert.m4 new file mode 100644 index 000000000..91c3937dd --- /dev/null +++ b/m4.include/mc-assert.m4 @@ -0,0 +1,21 @@ +dnl +dnl Check whether to enable/disable assertions. +dnl + +AC_DEFUN([mc_ASSERT], +[ + AC_ARG_ENABLE([assert], + AS_HELP_STRING([--enable-assert], [turn on assertions [yes]]), + [ + if test "x$enableval" = xno; then + enable_assert=no + else + enable_assert=yes + fi + ], + [enable_assert=yes]) + + if test "x$enable_assert" = xno; then + AC_DEFINE(G_DISABLE_ASSERT, 1, [Define to disable assertions]) + fi +]) diff --git a/src/editor/editcmd.c b/src/editor/editcmd.c index cdaf1731b..c7e91d741 100644 --- a/src/editor/editcmd.c +++ b/src/editor/editcmd.c @@ -35,11 +35,7 @@ #include -#ifdef HAVE_ASSERT_H -#include -#endif #include - #include #include #include @@ -384,9 +380,8 @@ edit_save_file (WEdit * edit, const vfs_path_t * filename_vpath) vfs_path_t *tmp_vpath; gboolean ok; -#ifdef HAVE_ASSERT_H - assert (option_backup_ext != NULL); -#endif + g_assert (option_backup_ext != NULL); + /* add backup extension to the path */ tmp_vpath = vfs_path_clone (real_filename_vpath); last_vpath_element = (vfs_path_element_t *) vfs_path_get_by_index (tmp_vpath, -1); @@ -1629,10 +1624,6 @@ edit_save_mode_cmd (void) N_("&Do backups with following extension:") }; -#ifdef HAVE_ASSERT_H - assert (option_backup_ext != NULL); -#endif - #ifdef ENABLE_NLS size_t i; @@ -1640,6 +1631,8 @@ edit_save_mode_cmd (void) str[i] = _(str[i]); #endif + g_assert (option_backup_ext != NULL); + { quick_widget_t quick_widgets[] = { /* *INDENT-OFF* */ diff --git a/src/viewer/coord_cache.c b/src/viewer/coord_cache.c index a8cf55aab..2824aec7e 100644 --- a/src/viewer/coord_cache.c +++ b/src/viewer/coord_cache.c @@ -146,9 +146,7 @@ mcview_ccache_find (WView * view, const coord_cache_entry_t * coord, cmp_func_t size_t base = 0; size_t limit = view->coord_cache->size; -#ifdef HAVE_ASSERT_H - assert (limit != 0); -#endif + g_assert (limit != 0); while (limit > 1) { @@ -215,9 +213,7 @@ mcview_ccache_dump (WView * view) guint i; const coord_cache_t *cache = view->coord_cache; -#ifdef HAVE_ASSERT_H - assert (cache != NULL); -#endif + g_assert (cache != NULL); filesize = mcview_get_filesize (view); diff --git a/src/viewer/datasource.c b/src/viewer/datasource.c index 0379be413..b4857ce70 100644 --- a/src/viewer/datasource.c +++ b/src/viewer/datasource.c @@ -114,9 +114,7 @@ mcview_get_filesize (WView * view) case DS_STRING: return view->ds_string_len; default: -#ifdef HAVE_ASSERT_H - assert (!"Unknown datasource type"); -#endif + g_assert (!"Unknown datasource type"); return 0; } } @@ -139,9 +137,7 @@ mcview_update_filesize (WView * view) char * mcview_get_ptr_file (WView * view, off_t byte_index) { -#ifdef HAVE_ASSERT_H - assert (view->datasource == DS_FILE); -#endif + g_assert (view->datasource == DS_FILE); mcview_file_load_data (view, byte_index); if (mcview_already_loaded (view->ds_file_offset, byte_index, view->ds_file_datalen)) @@ -225,9 +221,8 @@ mcview_get_utf (WView * view, off_t byte_index, int *ch, int *ch_len) char * mcview_get_ptr_string (WView * view, off_t byte_index) { -#ifdef HAVE_ASSERT_H - assert (view->datasource == DS_STRING); -#endif + g_assert (view->datasource == DS_STRING); + if (byte_index >= 0 && byte_index < (off_t) view->ds_string_len) return (char *) (view->ds_string_data + byte_index); return NULL; @@ -260,9 +255,7 @@ mcview_get_byte_none (WView * view, off_t byte_index, int *retval) (void) &view; (void) byte_index; -#ifdef HAVE_ASSERT_H - assert (view->datasource == DS_NONE); -#endif + g_assert (view->datasource == DS_NONE); if (retval != NULL) *retval = -1; @@ -275,13 +268,10 @@ void mcview_set_byte (WView * view, off_t offset, byte b) { (void) &b; -#ifndef HAVE_ASSERT_H - (void) offset; -#else - assert (offset < mcview_get_filesize (view)); - assert (view->datasource == DS_FILE); -#endif + g_assert (offset < mcview_get_filesize (view)); + g_assert (view->datasource == DS_FILE); + view->ds_file_datalen = 0; /* just force reloading */ } @@ -295,9 +285,7 @@ mcview_file_load_data (WView * view, off_t byte_index) ssize_t res; size_t bytes_read; -#ifdef HAVE_ASSERT_H - assert (view->datasource == DS_FILE); -#endif + g_assert (view->datasource == DS_FILE); if (mcview_already_loaded (view->ds_file_offset, byte_index, view->ds_file_datalen)) return; @@ -368,10 +356,7 @@ mcview_close_datasource (WView * view) MC_PTR_FREE (view->ds_string_data); break; default: -#ifdef HAVE_ASSERT_H - assert (!"Unknown datasource type") -#endif - ; + g_assert (!"Unknown datasource type"); } view->datasource = DS_NONE; } @@ -426,9 +411,8 @@ mcview_load_command_output (WView * view, const char *command) void mcview_set_datasource_vfs_pipe (WView * view, int fd) { -#ifdef HAVE_ASSERT_H - assert (fd != -1); -#endif + g_assert (fd != -1); + view->datasource = DS_VFS_PIPE; view->ds_vfs_pipe = fd; diff --git a/src/viewer/display.c b/src/viewer/display.c index 40e57a64e..6ac63f816 100644 --- a/src/viewer/display.c +++ b/src/viewer/display.c @@ -326,9 +326,8 @@ mcview_update_bytes_per_line (WView * view) bytes = 4; else bytes = 4 * ((cols - 9) / ((cols <= 80) ? 17 : 18)); -#ifdef HAVE_ASSERT_H - assert (bytes != 0); -#endif + + g_assert (bytes != 0); view->bytes_per_line = bytes; view->dirty = mcview_max_dirt_limit + 1; /* To force refresh */ @@ -346,9 +345,8 @@ mcview_display_toggle_ruler (WView * view) RULER_NONE }; -#ifdef HAVE_ASSERT_H - assert ((size_t) ruler < 3); -#endif + g_assert ((size_t) ruler < 3); + ruler = next[(size_t) ruler]; mcview_compute_areas (view); view->dirty++; diff --git a/src/viewer/growbuf.c b/src/viewer/growbuf.c index d080aa6a0..553774f24 100644 --- a/src/viewer/growbuf.c +++ b/src/viewer/growbuf.c @@ -94,9 +94,7 @@ mcview_growbuf_done (WView * view) void mcview_growbuf_free (WView * view) { -#ifdef HAVE_ASSERT_H - assert (view->growbuf_in_use); -#endif + g_assert (view->growbuf_in_use); g_ptr_array_foreach (view->growbuf_blockptr, (GFunc) g_free, NULL); @@ -111,9 +109,7 @@ mcview_growbuf_free (WView * view) off_t mcview_growbuf_filesize (WView * view) { -#ifdef HAVE_ASSERT_H - assert (view->growbuf_in_use); -#endif + g_assert (view->growbuf_in_use); if (view->growbuf_blockptr->len == 0) return 0; @@ -132,9 +128,7 @@ mcview_growbuf_read_until (WView * view, off_t ofs) { gboolean short_read = FALSE; -#ifdef HAVE_ASSERT_H - assert (view->growbuf_in_use); -#endif + g_assert (view->growbuf_in_use); if (view->growbuf_finished) return; @@ -226,9 +220,7 @@ mcview_growbuf_read_until (WView * view, off_t ofs) } else { -#ifdef HAVE_ASSERT_H - assert (view->datasource == DS_VFS_PIPE); -#endif + g_assert (view->datasource == DS_VFS_PIPE); do { nread = mc_read (view->ds_vfs_pipe, p, bytesfree); @@ -253,9 +245,7 @@ mcview_get_byte_growing_buffer (WView * view, off_t byte_index, int *retval) { char *p; -#ifdef HAVE_ASSERT_H - assert (view->growbuf_in_use); -#endif + g_assert (view->growbuf_in_use); if (retval != NULL) *retval = -1; @@ -280,9 +270,7 @@ mcview_get_ptr_growing_buffer (WView * view, off_t byte_index) { off_t pageno, pageindex; -#ifdef HAVE_ASSERT_H - assert (view->growbuf_in_use); -#endif + g_assert (view->growbuf_in_use); if (byte_index < 0) return NULL; diff --git a/src/viewer/hex.c b/src/viewer/hex.c index 1061530a7..f88495ed4 100644 --- a/src/viewer/hex.c +++ b/src/viewer/hex.c @@ -389,9 +389,7 @@ mcview_hexedit_save_changes (WView * view) char *text; struct hexedit_change_node *curr, *next; -#ifdef HAVE_ASSERT_H - assert (view->filename_vpath != NULL); -#endif + g_assert (view->filename_vpath != NULL); fp = mc_open (view->filename_vpath, O_WRONLY); if (fp != -1) diff --git a/src/viewer/inlines.h b/src/viewer/inlines.h index e0d66a39d..f06f42c89 100644 --- a/src/viewer/inlines.h +++ b/src/viewer/inlines.h @@ -1,10 +1,6 @@ #ifndef MC__VIEWER_INLINES_H #define MC__VIEWER_INLINES_H -#ifdef HAVE_ASSERT_H -#include -#endif - /*** typedefs(not structures) and defined constants **********************************************/ /*** enums ***************************************************************************************/ @@ -29,9 +25,7 @@ mcview_offset_doz (off_t a, off_t b) static inline off_t mcview_offset_rounddown (off_t a, off_t b) { -#ifdef HAVE_ASSERT_H - assert (b != 0); -#endif + g_assert (b != 0); return a - a % b; } @@ -77,9 +71,7 @@ mcview_already_loaded (off_t offset, off_t idx, size_t size) static inline gboolean mcview_get_byte_file (WView * view, off_t byte_index, int *retval) { -#ifdef HAVE_ASSERT_H - assert (view->datasource == DS_FILE); -#endif + g_assert (view->datasource == DS_FILE); mcview_file_load_data (view, byte_index); if (mcview_already_loaded (view->ds_file_offset, byte_index, view->ds_file_datalen)) @@ -110,9 +102,7 @@ mcview_get_byte (WView * view, off_t offset, int *retval) case DS_NONE: return mcview_get_byte_none (view, offset, retval); default: -#ifdef HAVE_ASSERT_H - assert (!"Unknown datasource type"); -#endif + g_assert (!"Unknown datasource type"); return FALSE; } } diff --git a/src/viewer/mcviewer.c b/src/viewer/mcviewer.c index 49165b4b5..caf4d1148 100644 --- a/src/viewer/mcviewer.c +++ b/src/viewer/mcviewer.c @@ -271,9 +271,7 @@ mcview_load (WView * view, const char *command, const char *file, int start_line gboolean retval = FALSE; vfs_path_t *vpath = NULL; -#ifdef HAVE_ASSERT_H - assert (view->bytes_per_line != 0); -#endif + g_assert (view->bytes_per_line != 0); view->filename_vpath = vfs_path_from_str (file); diff --git a/src/viewer/move.c b/src/viewer/move.c index c0a7bba22..a0d245c29 100644 --- a/src/viewer/move.c +++ b/src/viewer/move.c @@ -173,9 +173,9 @@ mcview_move_left (WView * view, off_t columns) if (view->hex_mode) { off_t old_cursor = view->hex_cursor; -#ifdef HAVE_ASSERT_H - assert (columns == 1); -#endif + + g_assert (columns == 1); + if (view->hexview_in_text || !view->hexedit_lownibble) { if (view->hex_cursor > 0) @@ -201,9 +201,9 @@ mcview_move_right (WView * view, off_t columns) off_t old_cursor = view->hex_cursor; last_byte = mcview_offset_doz (mcview_get_filesize (view), 1); -#ifdef HAVE_ASSERT_H - assert (columns == 1); -#endif + + g_assert (columns == 1); + if (view->hexview_in_text || view->hexedit_lownibble) { if (view->hex_cursor < last_byte)