2007-12-30 17:41:49 +01:00
|
|
|
/***************************************************************************
|
|
|
|
|
2012-03-29 18:43:01 +02:00
|
|
|
gb_common.c
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2013-08-03 17:38:01 +02:00
|
|
|
(c) 2000-2013 Benoît Minisini <gambas@users.sourceforge.net>
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-03-29 18:43:01 +02:00
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
any later version.
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-03-29 18:43:01 +02:00
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-03-29 18:43:01 +02:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|
|
|
MA 02110-1301, USA.
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#define __COMMON_C
|
|
|
|
#define __COMMON_CHECK_C
|
|
|
|
|
|
|
|
#include "gb_common.h"
|
|
|
|
|
|
|
|
#include "gb_common_check.h"
|
|
|
|
|
2015-11-02 04:24:44 +01:00
|
|
|
#include "gb_common_string_temp.h"
|
2007-12-30 17:41:49 +01:00
|
|
|
#include "gb_common_case_temp.h"
|
|
|
|
#include "gb_common_buffer_temp.h"
|
|
|
|
#include "gb_common_swap_temp.h"
|
|
|
|
|
2012-03-29 18:43:01 +02:00
|
|
|
#include "gbx_signal.h"
|
|
|
|
|
2009-07-08 21:57:50 +02:00
|
|
|
sigjmp_buf CHECK_jump;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-03-29 18:43:01 +02:00
|
|
|
static SIGNAL_HANDLER _SIGSEGV_handler;
|
|
|
|
static SIGNAL_HANDLER _SIGBUS_handler;
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
static int _dummy;
|
|
|
|
static volatile int _got_error;
|
|
|
|
|
|
|
|
|
2009-07-08 21:57:50 +02:00
|
|
|
void COMMON_init(void)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-29 18:43:01 +02:00
|
|
|
static void signal_error(int sig, siginfo_t *info, void *context)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2012-03-29 18:43:01 +02:00
|
|
|
SIGNAL_previous(sig == SIGSEGV ? &_SIGSEGV_handler : &_SIGBUS_handler, sig, info, context);
|
|
|
|
_got_error = TRUE;
|
|
|
|
siglongjmp(CHECK_jump, 1);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2009-07-08 21:57:50 +02:00
|
|
|
void CHECK_enter(void)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2012-03-29 18:43:01 +02:00
|
|
|
_got_error = FALSE;
|
|
|
|
SIGNAL_install(&_SIGSEGV_handler, SIGSEGV, signal_error);
|
|
|
|
SIGNAL_install(&_SIGBUS_handler, SIGBUS, signal_error);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2009-07-08 21:57:50 +02:00
|
|
|
void CHECK_leave(void)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2012-05-08 17:32:33 +02:00
|
|
|
SIGNAL_uninstall(&_SIGSEGV_handler, SIGSEGV);
|
|
|
|
SIGNAL_uninstall(&_SIGBUS_handler, SIGBUS);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2009-07-08 21:57:50 +02:00
|
|
|
bool CHECK_got_error(void)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2012-03-29 18:43:01 +02:00
|
|
|
return _got_error;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2013-01-06 17:13:31 +01:00
|
|
|
bool CHECK_address(void *ptr, ssize_t len)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2012-03-29 18:43:01 +02:00
|
|
|
offset_t i;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-05-29 23:10:36 +02:00
|
|
|
if (len < 0)
|
|
|
|
return TRUE;
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2012-03-29 18:43:01 +02:00
|
|
|
CHECK_enter();
|
|
|
|
if (sigsetjmp(CHECK_jump, TRUE) == 0)
|
|
|
|
{
|
|
|
|
for (i = 0; i < len; i += 1024)
|
|
|
|
_dummy = ((int *)ptr)[i];
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2010-05-29 23:10:36 +02:00
|
|
|
_dummy = ((int *)ptr)[len >> 2];
|
2012-03-29 18:43:01 +02:00
|
|
|
}
|
|
|
|
CHECK_leave();
|
|
|
|
return _got_error;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2013-01-06 17:13:31 +01:00
|
|
|
bool CHECK_strlen(char *ptr, ssize_t *len)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2012-03-29 18:43:01 +02:00
|
|
|
size_t l = 0;
|
|
|
|
|
|
|
|
CHECK_enter();
|
|
|
|
if (sigsetjmp(CHECK_jump, TRUE) == 0)
|
|
|
|
{
|
|
|
|
l = strlen(ptr);
|
|
|
|
*len = l;
|
|
|
|
}
|
|
|
|
CHECK_leave();
|
|
|
|
if (l != (size_t)(int)l)
|
|
|
|
_got_error = TRUE;
|
|
|
|
return _got_error;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|