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:
@@ -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;
|
||||
}
|
||||
|
||||
@@ -50,4 +50,6 @@ void reset_error_stack();
|
||||
|
||||
int init_h5_error_handling();
|
||||
|
||||
int init_error_handling();
|
||||
|
||||
#endif /* NXS_XDS_ERR_H */
|
||||
|
||||
@@ -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
@@ -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, "");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user