Use g_assert() instead of assert(3).

Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
This commit is contained in:
Andrew Borodin 2016-11-03 10:46:11 +03:00
parent f64e8b99d4
commit 9a39e35dc3
15 changed files with 66 additions and 118 deletions

View File

@ -21,3 +21,4 @@ m4_include([m4.include/mc-vfs.m4])
m4_include([m4.include/mc-version.m4]) m4_include([m4.include/mc-version.m4])
m4_include([m4.include/mc-tests.m4]) m4_include([m4.include/mc-tests.m4])
m4_include([m4.include/mc-i18n.m4]) m4_include([m4.include/mc-i18n.m4])
m4_include([m4.include/mc-assert.m4])

View File

@ -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/select.h sys/ioctl.h stropts.h arpa/inet.h \
sys/socket.h]) sys/socket.h])
AC_HEADER_MAJOR AC_HEADER_MAJOR
AC_HEADER_ASSERT
dnl ############################################################################ dnl ############################################################################
@ -386,6 +385,8 @@ dnl ############################################################################
dnl MC options dnl MC options
dnl ############################################################################ dnl ############################################################################
mc_ASSERT
mc_WITH_INTERNAL_EDIT mc_WITH_INTERNAL_EDIT
dnl Diff viewer support. dnl Diff viewer support.
@ -649,6 +650,7 @@ Configuration:
Source code location: ${srcdir} Source code location: ${srcdir}
Compiler: ${CC} Compiler: ${CC}
Compiler flags: ${CFLAGS} Compiler flags: ${CFLAGS}
Assertions: ${enable_assert}
File system: ${vfs_type} File system: ${vfs_type}
${vfs_flags} ${vfs_flags}
Screen library: ${screen_msg} Screen library: ${screen_msg}

View File

@ -135,17 +135,6 @@
/* one caused by typing 'exit' or 'logout' in the subshell */ /* one caused by typing 'exit' or 'logout' in the subshell */
#define SUBSHELL_EXIT 128 #define SUBSHELL_EXIT 128
#if 0
#ifdef MC_ENABLE_DEBUGGING_CODE
#undef NDEBUG
#else
#define NDEBUG
#endif
#ifdef HAVE_ASSERT_H
#include <assert.h>
#endif
#endif
#define MC_ERROR g_quark_from_static_string (PACKAGE) #define MC_ERROR g_quark_from_static_string (PACKAGE)
#define DEFAULT_CHARSET "ASCII" #define DEFAULT_CHARSET "ASCII"

View File

@ -6,9 +6,6 @@
#include <sys/types.h> #include <sys/types.h>
#include <inttypes.h> #include <inttypes.h>
#include <string.h> #include <string.h>
#ifdef HAVE_ASSERT_H
#include <assert.h> /* assert() */
#endif
/* Header file for strutil.c, strutilascii.c, strutil8bit.c, strutilutf8.c. /* Header file for strutil.c, strutilascii.c, strutil8bit.c, strutilutf8.c.
* There are two sort of functions: * There are two sort of functions:
@ -599,9 +596,7 @@ str_move (char *dest, const char *src)
{ {
size_t n; size_t n;
#ifdef HAVE_ASSERT_H g_assert (dest <= src);
assert (dest <= src);
#endif
n = strlen (src) + 1; /* + '\0' */ n = strlen (src) + 1; /* + '\0' */

View File

@ -24,9 +24,6 @@
need stderr defined if assertion checking is enabled. */ need stderr defined if assertion checking is enabled. */
#include <stdio.h> #include <stdio.h>
#ifdef HAVE_ASSERT_H
#include <assert.h>
#endif
#include <ctype.h> #include <ctype.h>
#include <errno.h> #include <errno.h>
#include <inttypes.h> #include <inttypes.h>
@ -84,9 +81,7 @@ xstrtoumax (const char *s, char **ptr, int base, uintmax_t * val, const char *va
uintmax_t tmp; uintmax_t tmp;
strtol_error_t err = LONGINT_OK; strtol_error_t err = LONGINT_OK;
#ifdef HAVE_ASSERT_H g_assert (0 <= base && base <= 36);
assert (0 <= base && base <= 36);
#endif
p = (ptr != NULL ? ptr : &t_ptr); p = (ptr != NULL ? ptr : &t_ptr);

21
m4.include/mc-assert.m4 Normal file
View File

@ -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
])

View File

@ -35,11 +35,7 @@
#include <config.h> #include <config.h>
#ifdef HAVE_ASSERT_H
#include <assert.h>
#endif
#include <ctype.h> #include <ctype.h>
#include <stdio.h> #include <stdio.h>
#include <stdarg.h> #include <stdarg.h>
#include <sys/types.h> #include <sys/types.h>
@ -384,9 +380,8 @@ edit_save_file (WEdit * edit, const vfs_path_t * filename_vpath)
vfs_path_t *tmp_vpath; vfs_path_t *tmp_vpath;
gboolean ok; gboolean ok;
#ifdef HAVE_ASSERT_H g_assert (option_backup_ext != NULL);
assert (option_backup_ext != NULL);
#endif
/* add backup extension to the path */ /* add backup extension to the path */
tmp_vpath = vfs_path_clone (real_filename_vpath); tmp_vpath = vfs_path_clone (real_filename_vpath);
last_vpath_element = (vfs_path_element_t *) vfs_path_get_by_index (tmp_vpath, -1); 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:") N_("&Do backups with following extension:")
}; };
#ifdef HAVE_ASSERT_H
assert (option_backup_ext != NULL);
#endif
#ifdef ENABLE_NLS #ifdef ENABLE_NLS
size_t i; size_t i;
@ -1640,6 +1631,8 @@ edit_save_mode_cmd (void)
str[i] = _(str[i]); str[i] = _(str[i]);
#endif #endif
g_assert (option_backup_ext != NULL);
{ {
quick_widget_t quick_widgets[] = { quick_widget_t quick_widgets[] = {
/* *INDENT-OFF* */ /* *INDENT-OFF* */

View File

@ -146,9 +146,7 @@ mcview_ccache_find (WView * view, const coord_cache_entry_t * coord, cmp_func_t
size_t base = 0; size_t base = 0;
size_t limit = view->coord_cache->size; size_t limit = view->coord_cache->size;
#ifdef HAVE_ASSERT_H g_assert (limit != 0);
assert (limit != 0);
#endif
while (limit > 1) while (limit > 1)
{ {
@ -215,9 +213,7 @@ mcview_ccache_dump (WView * view)
guint i; guint i;
const coord_cache_t *cache = view->coord_cache; const coord_cache_t *cache = view->coord_cache;
#ifdef HAVE_ASSERT_H g_assert (cache != NULL);
assert (cache != NULL);
#endif
filesize = mcview_get_filesize (view); filesize = mcview_get_filesize (view);

View File

@ -114,9 +114,7 @@ mcview_get_filesize (WView * view)
case DS_STRING: case DS_STRING:
return view->ds_string_len; return view->ds_string_len;
default: default:
#ifdef HAVE_ASSERT_H g_assert (!"Unknown datasource type");
assert (!"Unknown datasource type");
#endif
return 0; return 0;
} }
} }
@ -139,9 +137,7 @@ mcview_update_filesize (WView * view)
char * char *
mcview_get_ptr_file (WView * view, off_t byte_index) mcview_get_ptr_file (WView * view, off_t byte_index)
{ {
#ifdef HAVE_ASSERT_H g_assert (view->datasource == DS_FILE);
assert (view->datasource == DS_FILE);
#endif
mcview_file_load_data (view, byte_index); mcview_file_load_data (view, byte_index);
if (mcview_already_loaded (view->ds_file_offset, byte_index, view->ds_file_datalen)) 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 * char *
mcview_get_ptr_string (WView * view, off_t byte_index) mcview_get_ptr_string (WView * view, off_t byte_index)
{ {
#ifdef HAVE_ASSERT_H g_assert (view->datasource == DS_STRING);
assert (view->datasource == DS_STRING);
#endif
if (byte_index >= 0 && byte_index < (off_t) view->ds_string_len) if (byte_index >= 0 && byte_index < (off_t) view->ds_string_len)
return (char *) (view->ds_string_data + byte_index); return (char *) (view->ds_string_data + byte_index);
return NULL; return NULL;
@ -260,9 +255,7 @@ mcview_get_byte_none (WView * view, off_t byte_index, int *retval)
(void) &view; (void) &view;
(void) byte_index; (void) byte_index;
#ifdef HAVE_ASSERT_H g_assert (view->datasource == DS_NONE);
assert (view->datasource == DS_NONE);
#endif
if (retval != NULL) if (retval != NULL)
*retval = -1; *retval = -1;
@ -275,13 +268,10 @@ void
mcview_set_byte (WView * view, off_t offset, byte b) mcview_set_byte (WView * view, off_t offset, byte b)
{ {
(void) &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 */ 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; ssize_t res;
size_t bytes_read; size_t bytes_read;
#ifdef HAVE_ASSERT_H g_assert (view->datasource == DS_FILE);
assert (view->datasource == DS_FILE);
#endif
if (mcview_already_loaded (view->ds_file_offset, byte_index, view->ds_file_datalen)) if (mcview_already_loaded (view->ds_file_offset, byte_index, view->ds_file_datalen))
return; return;
@ -368,10 +356,7 @@ mcview_close_datasource (WView * view)
MC_PTR_FREE (view->ds_string_data); MC_PTR_FREE (view->ds_string_data);
break; break;
default: default:
#ifdef HAVE_ASSERT_H g_assert (!"Unknown datasource type");
assert (!"Unknown datasource type")
#endif
;
} }
view->datasource = DS_NONE; view->datasource = DS_NONE;
} }
@ -426,9 +411,8 @@ mcview_load_command_output (WView * view, const char *command)
void void
mcview_set_datasource_vfs_pipe (WView * view, int fd) mcview_set_datasource_vfs_pipe (WView * view, int fd)
{ {
#ifdef HAVE_ASSERT_H g_assert (fd != -1);
assert (fd != -1);
#endif
view->datasource = DS_VFS_PIPE; view->datasource = DS_VFS_PIPE;
view->ds_vfs_pipe = fd; view->ds_vfs_pipe = fd;

View File

@ -326,9 +326,8 @@ mcview_update_bytes_per_line (WView * view)
bytes = 4; bytes = 4;
else else
bytes = 4 * ((cols - 9) / ((cols <= 80) ? 17 : 18)); bytes = 4 * ((cols - 9) / ((cols <= 80) ? 17 : 18));
#ifdef HAVE_ASSERT_H
assert (bytes != 0); g_assert (bytes != 0);
#endif
view->bytes_per_line = bytes; view->bytes_per_line = bytes;
view->dirty = mcview_max_dirt_limit + 1; /* To force refresh */ view->dirty = mcview_max_dirt_limit + 1; /* To force refresh */
@ -346,9 +345,8 @@ mcview_display_toggle_ruler (WView * view)
RULER_NONE RULER_NONE
}; };
#ifdef HAVE_ASSERT_H g_assert ((size_t) ruler < 3);
assert ((size_t) ruler < 3);
#endif
ruler = next[(size_t) ruler]; ruler = next[(size_t) ruler];
mcview_compute_areas (view); mcview_compute_areas (view);
view->dirty++; view->dirty++;

View File

@ -94,9 +94,7 @@ mcview_growbuf_done (WView * view)
void void
mcview_growbuf_free (WView * view) mcview_growbuf_free (WView * view)
{ {
#ifdef HAVE_ASSERT_H g_assert (view->growbuf_in_use);
assert (view->growbuf_in_use);
#endif
g_ptr_array_foreach (view->growbuf_blockptr, (GFunc) g_free, NULL); g_ptr_array_foreach (view->growbuf_blockptr, (GFunc) g_free, NULL);
@ -111,9 +109,7 @@ mcview_growbuf_free (WView * view)
off_t off_t
mcview_growbuf_filesize (WView * view) mcview_growbuf_filesize (WView * view)
{ {
#ifdef HAVE_ASSERT_H g_assert (view->growbuf_in_use);
assert (view->growbuf_in_use);
#endif
if (view->growbuf_blockptr->len == 0) if (view->growbuf_blockptr->len == 0)
return 0; return 0;
@ -132,9 +128,7 @@ mcview_growbuf_read_until (WView * view, off_t ofs)
{ {
gboolean short_read = FALSE; gboolean short_read = FALSE;
#ifdef HAVE_ASSERT_H g_assert (view->growbuf_in_use);
assert (view->growbuf_in_use);
#endif
if (view->growbuf_finished) if (view->growbuf_finished)
return; return;
@ -226,9 +220,7 @@ mcview_growbuf_read_until (WView * view, off_t ofs)
} }
else else
{ {
#ifdef HAVE_ASSERT_H g_assert (view->datasource == DS_VFS_PIPE);
assert (view->datasource == DS_VFS_PIPE);
#endif
do do
{ {
nread = mc_read (view->ds_vfs_pipe, p, bytesfree); 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; char *p;
#ifdef HAVE_ASSERT_H g_assert (view->growbuf_in_use);
assert (view->growbuf_in_use);
#endif
if (retval != NULL) if (retval != NULL)
*retval = -1; *retval = -1;
@ -280,9 +270,7 @@ mcview_get_ptr_growing_buffer (WView * view, off_t byte_index)
{ {
off_t pageno, pageindex; off_t pageno, pageindex;
#ifdef HAVE_ASSERT_H g_assert (view->growbuf_in_use);
assert (view->growbuf_in_use);
#endif
if (byte_index < 0) if (byte_index < 0)
return NULL; return NULL;

View File

@ -389,9 +389,7 @@ mcview_hexedit_save_changes (WView * view)
char *text; char *text;
struct hexedit_change_node *curr, *next; struct hexedit_change_node *curr, *next;
#ifdef HAVE_ASSERT_H g_assert (view->filename_vpath != NULL);
assert (view->filename_vpath != NULL);
#endif
fp = mc_open (view->filename_vpath, O_WRONLY); fp = mc_open (view->filename_vpath, O_WRONLY);
if (fp != -1) if (fp != -1)

View File

@ -1,10 +1,6 @@
#ifndef MC__VIEWER_INLINES_H #ifndef MC__VIEWER_INLINES_H
#define MC__VIEWER_INLINES_H #define MC__VIEWER_INLINES_H
#ifdef HAVE_ASSERT_H
#include <assert.h>
#endif
/*** typedefs(not structures) and defined constants **********************************************/ /*** typedefs(not structures) and defined constants **********************************************/
/*** enums ***************************************************************************************/ /*** enums ***************************************************************************************/
@ -29,9 +25,7 @@ mcview_offset_doz (off_t a, off_t b)
static inline off_t static inline off_t
mcview_offset_rounddown (off_t a, off_t b) mcview_offset_rounddown (off_t a, off_t b)
{ {
#ifdef HAVE_ASSERT_H g_assert (b != 0);
assert (b != 0);
#endif
return a - a % b; return a - a % b;
} }
@ -77,9 +71,7 @@ mcview_already_loaded (off_t offset, off_t idx, size_t size)
static inline gboolean static inline gboolean
mcview_get_byte_file (WView * view, off_t byte_index, int *retval) mcview_get_byte_file (WView * view, off_t byte_index, int *retval)
{ {
#ifdef HAVE_ASSERT_H g_assert (view->datasource == DS_FILE);
assert (view->datasource == DS_FILE);
#endif
mcview_file_load_data (view, byte_index); mcview_file_load_data (view, byte_index);
if (mcview_already_loaded (view->ds_file_offset, byte_index, view->ds_file_datalen)) 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: case DS_NONE:
return mcview_get_byte_none (view, offset, retval); return mcview_get_byte_none (view, offset, retval);
default: default:
#ifdef HAVE_ASSERT_H g_assert (!"Unknown datasource type");
assert (!"Unknown datasource type");
#endif
return FALSE; return FALSE;
} }
} }

View File

@ -271,9 +271,7 @@ mcview_load (WView * view, const char *command, const char *file, int start_line
gboolean retval = FALSE; gboolean retval = FALSE;
vfs_path_t *vpath = NULL; vfs_path_t *vpath = NULL;
#ifdef HAVE_ASSERT_H g_assert (view->bytes_per_line != 0);
assert (view->bytes_per_line != 0);
#endif
view->filename_vpath = vfs_path_from_str (file); view->filename_vpath = vfs_path_from_str (file);

View File

@ -173,9 +173,9 @@ mcview_move_left (WView * view, off_t columns)
if (view->hex_mode) if (view->hex_mode)
{ {
off_t old_cursor = view->hex_cursor; off_t old_cursor = view->hex_cursor;
#ifdef HAVE_ASSERT_H
assert (columns == 1); g_assert (columns == 1);
#endif
if (view->hexview_in_text || !view->hexedit_lownibble) if (view->hexview_in_text || !view->hexedit_lownibble)
{ {
if (view->hex_cursor > 0) if (view->hex_cursor > 0)
@ -201,9 +201,9 @@ mcview_move_right (WView * view, off_t columns)
off_t old_cursor = view->hex_cursor; off_t old_cursor = view->hex_cursor;
last_byte = mcview_offset_doz (mcview_get_filesize (view), 1); last_byte = mcview_offset_doz (mcview_get_filesize (view), 1);
#ifdef HAVE_ASSERT_H
assert (columns == 1); g_assert (columns == 1);
#endif
if (view->hexview_in_text || view->hexedit_lownibble) if (view->hexview_in_text || view->hexedit_lownibble)
{ {
if (view->hex_cursor < last_byte) if (view->hex_cursor < last_byte)