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.
This commit is contained in:
Charles Mita
2018-04-23 16:18:32 +01:00
parent e4466b00fc
commit 482ab573af
4 changed files with 17 additions and 4 deletions
+12 -3
View File
@@ -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;
}
+2
View File
@@ -50,4 +50,6 @@ void reset_error_stack();
int init_h5_error_handling();
int init_error_handling();
#endif /* NXS_XDS_ERR_H */
+2
View File
@@ -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");
}
+1 -1
View File
@@ -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, "");
}