From 482ab573af29133be2947cf1c56bb43d0131a3b6 Mon Sep 17 00:00:00 2001 From: Charles Mita Date: Mon, 23 Apr 2018 16:18:32 +0100 Subject: [PATCH] Initialise the error stack message arrays at start This helps prevent potential segfaults due to null-pointer dereferencing in the face of errors when the plugin is accessed via multiple threads. If multiple threads are pushing errors onto the error stack then its possible for elements of the various string pointer arrays to be left NULL, causing a segfault when the stack is printed. Whilst this does nothing to address the "correctness" of the plugin in a multi-threaded context, at least it shouldn't crash. Error messages may still be corrupted. --- src/err.c | 15 ++++++++++++--- src/err.h | 2 ++ src/plugin.c | 2 ++ src/test.c | 2 +- 4 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/err.c b/src/err.c index 69dd166..567f36e 100644 --- a/src/err.c +++ b/src/err.c @@ -34,9 +34,6 @@ static struct error_stack_t stack = {files, funcs, lines, errors, messages, 0}; void push_error_stack(const char *file, const char *func, int line, int err, const char *message) { if (stack.size >= ERR_MAX_STACK_SIZE) return; /* unfortunate */ int idx = stack.size; - stack.files[idx] = files_buffer + (idx * ERR_MAX_FILENAME_LENGTH); - stack.funcs[idx] = funcs_buffer + (idx * ERR_MAX_FUNCNAME_LENGTH); - stack.messages[idx] = messages_buffer + (idx * ERR_MAX_MESSAGE_LENGTH); /* subtract 1 to ensure room for null byte in buffer */ sprintf(stack.funcs[idx], "%.*s", ERR_MAX_FUNCNAME_LENGTH - 1, func); @@ -107,3 +104,15 @@ int init_h5_error_handling() { done: return retval; } + +int init_error_handling() { + int retval = 0; + int idx = 0; + while (idx < ERR_MAX_STACK_SIZE) { + stack.files[idx] = files_buffer + (idx * ERR_MAX_FILENAME_LENGTH); + stack.funcs[idx] = funcs_buffer + (idx * ERR_MAX_FUNCNAME_LENGTH); + stack.messages[idx] = messages_buffer + (idx * ERR_MAX_MESSAGE_LENGTH); + idx++; + } + return retval; +} diff --git a/src/err.h b/src/err.h index c30f6f2..5830cc1 100644 --- a/src/err.h +++ b/src/err.h @@ -50,4 +50,6 @@ void reset_error_stack(); int init_h5_error_handling(); +int init_error_handling(); + #endif /* NXS_XDS_ERR_H */ diff --git a/src/plugin.c b/src/plugin.c index 595b955..c31609a 100644 --- a/src/plugin.c +++ b/src/plugin.c @@ -55,6 +55,8 @@ void plugin_open( int retval = 0; *error_flag = 0; + init_error_handling(); + if (H5dont_atexit() < 0) { ERROR_JUMP(-2, done, "Failed configuring HDF5 library behaviour"); } diff --git a/src/test.c b/src/test.c index 93d6d32..ec6d669 100644 --- a/src/test.c +++ b/src/test.c @@ -46,7 +46,7 @@ int main(int argc, char **argv) { int *mask = NULL; int *data = NULL; - reset_error_stack(); + init_error_handling(); if (init_h5_error_handling() < 0) { ERROR_JUMP(-1, done, ""); }