Change naming convention for the error context array to avoid confusion about whether it is dynamic or static
This commit is contained in:
@@ -325,10 +325,10 @@ Consider the `tests/err_trace.c` program which intentionally triggers this behav
|
||||
```
|
||||
tests/err_trace.c:func2:7: 1 (Null Pointer Error) : This is a failure in func2
|
||||
tests/err_trace.c:func2:10
|
||||
tests/err_trace.c:func1:18: Detected error 0 from heap (refcount 1)
|
||||
tests/err_trace.c:func1:18: Detected error 0 from array (refcount 1)
|
||||
tests/err_trace.c:func1:18
|
||||
tests/err_trace.c:func1:21
|
||||
tests/err_trace.c:main:30: Detected error 0 from heap (refcount 1)
|
||||
tests/err_trace.c:main:30: Detected error 0 from array (refcount 1)
|
||||
tests/err_trace.c:main:30
|
||||
tests/err_trace.c:main:33: Unhandled Error 1 (Null Pointer Error): This is a failure in func2
|
||||
```
|
||||
|
||||
@@ -35,13 +35,13 @@
|
||||
|
||||
extern char __ERROR_NAMES[MAX_ERR_VALUE+1][MAX_ERROR_NAME_LENGTH];
|
||||
|
||||
#define MAX_HEAP_ERROR 128
|
||||
#define MAX_ARRAY_ERROR 128
|
||||
|
||||
|
||||
typedef struct
|
||||
{
|
||||
char message[MAX_ERROR_CONTEXT_STRING_LENGTH];
|
||||
int heapid;
|
||||
int arrayid;
|
||||
int status;
|
||||
bool handled;
|
||||
int refcount;
|
||||
@@ -58,13 +58,13 @@ typedef struct
|
||||
typedef void (*ErrorUnhandledErrorHandler)(ErrorContext *errctx);
|
||||
typedef void (*ErrorLogFunction)(const char *f, ...);
|
||||
|
||||
extern ErrorContext HEAP_ERROR[MAX_HEAP_ERROR];
|
||||
extern ErrorContext ARRAY_ERROR[MAX_ARRAY_ERROR];
|
||||
extern ErrorUnhandledErrorHandler error_handler_unhandled_error;
|
||||
extern ErrorLogFunction error_log_method;
|
||||
extern ErrorContext *__error_last_ignored;
|
||||
|
||||
ErrorContext ERROR_NOIGNORE *heap_release_error(ErrorContext *ptr);
|
||||
ErrorContext ERROR_NOIGNORE *heap_next_error();
|
||||
ErrorContext ERROR_NOIGNORE *array_release_error(ErrorContext *ptr);
|
||||
ErrorContext ERROR_NOIGNORE *array_next_error();
|
||||
char *error_name_for_status(int status, char *name);
|
||||
void error_init();
|
||||
void error_default_handler_unhandled_error(ErrorContext *ptr);
|
||||
@@ -78,7 +78,7 @@ void error_default_logger(const char *f, ...);
|
||||
|
||||
#define RELEASE_ERROR(__err_context) \
|
||||
if ( __err_context != NULL ) { \
|
||||
__err_context = heap_release_error(__err_context); \
|
||||
__err_context = array_release_error(__err_context); \
|
||||
}
|
||||
|
||||
#define PREPARE_ERROR(__err_context) \
|
||||
@@ -87,9 +87,9 @@ void error_default_logger(const char *f, ...);
|
||||
|
||||
#define ENSURE_ERROR_READY(__err_context) \
|
||||
if ( __err_context == NULL ) { \
|
||||
__err_context = heap_next_error(); \
|
||||
__err_context = array_next_error(); \
|
||||
if ( __err_context == NULL ) { \
|
||||
error_log_method("%s:%s:%d: Unable to pull an ErrorContext from the heap!", __FILE__, (char *)__func__, __LINE__); \
|
||||
error_log_method("%s:%s:%d: Unable to pull an ErrorContext from the array!", __FILE__, (char *)__func__, __LINE__); \
|
||||
exit(1); \
|
||||
} \
|
||||
} \
|
||||
@@ -172,7 +172,7 @@ void error_default_logger(const char *f, ...);
|
||||
#define DETECT(__err_context, __stmt) \
|
||||
__stmt; \
|
||||
if ( __err_context != NULL ) { \
|
||||
__err_context->stacktracebufptr += snprintf(__err_context->stacktracebufptr, MAX_ERROR_STACKTRACE_BUF_LENGTH, "%s:%s:%d: Detected error %d from heap (refcount %d)\n", (char *)__FILE__, (char *)__func__, __LINE__, __err_context->heapid, __err_context->refcount); \
|
||||
__err_context->stacktracebufptr += snprintf(__err_context->stacktracebufptr, MAX_ERROR_STACKTRACE_BUF_LENGTH, "%s:%s:%d: Detected error %d from array (refcount %d)\n", (char *)__FILE__, (char *)__func__, __LINE__, __err_context->arrayid, __err_context->refcount); \
|
||||
if ( __err_context->status != 0 ) { \
|
||||
__err_context->stacktracebufptr += snprintf(__err_context->stacktracebufptr, MAX_ERROR_STACKTRACE_BUF_LENGTH, "%s:%s:%d\n", (char *)__FILE__, (char *)__func__, __LINE__); \
|
||||
break; \
|
||||
|
||||
26
src/error.c
26
src/error.c
@@ -12,7 +12,7 @@ ErrorLogFunction error_log_method = NULL;
|
||||
|
||||
char __ERROR_NAMES[MAX_ERR_VALUE+1][MAX_ERROR_NAME_LENGTH];
|
||||
|
||||
ErrorContext HEAP_ERROR[MAX_HEAP_ERROR];
|
||||
ErrorContext ARRAY_ERROR[MAX_ARRAY_ERROR];
|
||||
|
||||
void error_default_logger(const char *fmt, ...)
|
||||
{
|
||||
@@ -31,10 +31,10 @@ void error_init()
|
||||
{
|
||||
static int inited = 0;
|
||||
if ( inited == 0 ) {
|
||||
for (int i = 0; i < MAX_HEAP_ERROR; i++ ) {
|
||||
memset((void *)&HEAP_ERROR[i], 0x00, sizeof(ErrorContext));
|
||||
HEAP_ERROR[i].heapid = i;
|
||||
HEAP_ERROR[i].stacktracebufptr = (char *)&HEAP_ERROR[i].stacktracebuf;
|
||||
for (int i = 0; i < MAX_ARRAY_ERROR; i++ ) {
|
||||
memset((void *)&ARRAY_ERROR[i], 0x00, sizeof(ErrorContext));
|
||||
ARRAY_ERROR[i].arrayid = i;
|
||||
ARRAY_ERROR[i].stacktracebufptr = (char *)&ARRAY_ERROR[i].stacktracebuf;
|
||||
}
|
||||
__error_last_ignored = NULL;
|
||||
memset((void *)&__error_last_ditch, 0x00, sizeof(ErrorContext));
|
||||
@@ -72,31 +72,31 @@ void error_default_handler_unhandled_error(ErrorContext *errctx)
|
||||
exit(errctx->status);
|
||||
}
|
||||
|
||||
ErrorContext *heap_next_error()
|
||||
ErrorContext *array_next_error()
|
||||
{
|
||||
for (int i = 0; i < MAX_HEAP_ERROR; i++ ) {
|
||||
if ( HEAP_ERROR[i].refcount == 0 ) {
|
||||
return &HEAP_ERROR[i];
|
||||
for (int i = 0; i < MAX_ARRAY_ERROR; i++ ) {
|
||||
if ( ARRAY_ERROR[i].refcount == 0 ) {
|
||||
return &ARRAY_ERROR[i];
|
||||
}
|
||||
}
|
||||
return (ErrorContext *)NULL;
|
||||
}
|
||||
|
||||
ErrorContext *heap_release_error(ErrorContext *err)
|
||||
ErrorContext *array_release_error(ErrorContext *err)
|
||||
{
|
||||
int oldid = 0;
|
||||
if ( err == NULL ) {
|
||||
ErrorContext *errctx = &__error_last_ditch;
|
||||
FAIL_RETURN(errctx, ERR_NULLPOINTER, "heap_release_error got NULL context pointer");
|
||||
FAIL_RETURN(errctx, ERR_NULLPOINTER, "array_release_error got NULL context pointer");
|
||||
}
|
||||
if ( err->refcount > 0 ) {
|
||||
err->refcount -= 1;
|
||||
}
|
||||
if ( err->refcount == 0 ) {
|
||||
oldid = err->heapid;
|
||||
oldid = err->arrayid;
|
||||
memset(err, 0x00, sizeof(ErrorContext));
|
||||
err->stacktracebufptr = (char *)&err->stacktracebuf;
|
||||
err->heapid = oldid;
|
||||
err->arrayid = oldid;
|
||||
return NULL;
|
||||
}
|
||||
return err;
|
||||
|
||||
Reference in New Issue
Block a user