From bafe246c68af1fc8ab2492b89f1d91de6f3db659 Mon Sep 17 00:00:00 2001
From: "raphael.bach" <raphael.bach@etu.hesge.ch>
Date: Mon, 13 Jun 2022 10:27:31 +0200
Subject: [PATCH] Fix variable scope bug with `fmpi_error_handler`

- Change all `fmpi_error_handler` occurrences to non-pointer variables
---
 include/internal/fmpi_ctx.h     |  2 +-
 include/internal/fmpi_error.h   |  4 ++--
 include/internal/fmpi_futhark.h |  8 ++++----
 include/internal/fmpi_mpi.h     |  4 ++--
 src/fmpi_ctx.c                  |  6 +++---
 src/fmpi_error.c                |  8 ++++----
 src/fmpi_futhark.c              | 10 ++++------
 src/fmpi_mpi.c                  | 10 ++++------
 8 files changed, 24 insertions(+), 28 deletions(-)

diff --git a/include/internal/fmpi_ctx.h b/include/internal/fmpi_ctx.h
index 845e01a..1ac74a1 100644
--- a/include/internal/fmpi_ctx.h
+++ b/include/internal/fmpi_ctx.h
@@ -51,7 +51,7 @@ typedef struct fmpi_ctx {
     size_t task_cnt;
     double timer_start;            //!< TODO
     double timer_stop;             //!< TODO
-    const struct fmpi_error_handler * err_handler; //!< TODO
+    struct fmpi_error_handler err_handler; //!< TODO
 } fmpi_ctx;
 /*==============================================================================
     PUBLIC FUNCTION
diff --git a/include/internal/fmpi_error.h b/include/internal/fmpi_error.h
index 1c08824..915e632 100644
--- a/include/internal/fmpi_error.h
+++ b/include/internal/fmpi_error.h
@@ -194,7 +194,7 @@ typedef struct fmpi_error_handler {
  * }
  */
 void fmpi_raise_error(
-    const struct fmpi_error_handler * err_handler,
+    struct fmpi_error_handler err_handler,
     const char * file, size_t line, const char * func, const char * module,
     const char * fmt, ...
 );
@@ -271,7 +271,7 @@ const char * fmpi_error_timestamp_str(time_t t, char * buf, size_t size);
  * }
  */
 #define FMPI_RAISE_ERROR(err_handler, module, ...) \
-    fmpi_raise_error((err_handler), __FILE__, __LINE__, __func__, (module), __VA_ARGS__)
+    fmpi_raise_error(err_handler, __FILE__, __LINE__, __func__, (module), __VA_ARGS__)
 /*==============================================================================
     GUARD
 ==============================================================================*/
diff --git a/include/internal/fmpi_futhark.h b/include/internal/fmpi_futhark.h
index 65c7af1..3edddb5 100644
--- a/include/internal/fmpi_futhark.h
+++ b/include/internal/fmpi_futhark.h
@@ -41,9 +41,9 @@
  * TODO
  */
 typedef struct fmpi_futhark_ctx {
-    struct futhark_context * ctx;                  //!< TODO
-    struct futhark_context_config * cfg;           //!< TODO
-    const struct fmpi_error_handler * err_handler; //!< TODO
+    struct futhark_context * ctx;          //!< TODO
+    struct futhark_context_config * cfg;   //!< TODO
+    struct fmpi_error_handler err_handler; //!< TODO
 } fmpi_futhark_ctx;
 /*==============================================================================
     PUBLIC FUNCTION
@@ -66,7 +66,7 @@ typedef struct fmpi_futhark_ctx {
  *  TODO
  * }
  */
-struct fmpi_futhark_ctx * fmpi_futhark_init(const struct fmpi_error_handler * err_handler);
+struct fmpi_futhark_ctx * fmpi_futhark_init(struct fmpi_error_handler err_handler);
 /*------------------------------------------------------------------------------
     fmpi_futhark_exit()
 ------------------------------------------------------------------------------*/
diff --git a/include/internal/fmpi_mpi.h b/include/internal/fmpi_mpi.h
index dd0119a..ca5bb46 100644
--- a/include/internal/fmpi_mpi.h
+++ b/include/internal/fmpi_mpi.h
@@ -52,7 +52,7 @@ typedef struct fmpi_mpi_ctx {
     int root; //!< TODO
     int futhark_err_class; //!< TODO
     int futhark_err_code; //!< TODO
-    const struct fmpi_error_handler * err_handler; //!< TODO
+    struct fmpi_error_handler err_handler; //!< TODO
 } fmpi_mpi_ctx;
 /*==============================================================================
     PUBLIC FUNCTION
@@ -78,7 +78,7 @@ typedef struct fmpi_mpi_ctx {
  * }
  */
 struct fmpi_mpi_ctx * fmpi_mpi_init(
-    int * argc, char ** argv[], const struct fmpi_error_handler * err_handler
+    int * argc, char ** argv[], struct fmpi_error_handler err_handler
 );
 /*------------------------------------------------------------------------------
     fmpi_mpi_exit()
diff --git a/src/fmpi_ctx.c b/src/fmpi_ctx.c
index 0d6f922..fe7030b 100644
--- a/src/fmpi_ctx.c
+++ b/src/fmpi_ctx.c
@@ -64,12 +64,12 @@ struct fmpi_ctx * fmpi_ctx_create(int * argc, char ** argv[])
         fprintf(stderr, "malloc(fmpi_ctx) failed!\n");
         return NULL;
     }
-    ctx->err_handler = &(struct fmpi_error_handler) {
+    ctx->err_handler = (struct fmpi_error_handler) {
         .func = fmpi_error_callback,
         .user_data = ctx
     };
     // MPI initialization
-    ctx->mpi = fmpi_mpi_init(argc, argv, &(struct fmpi_error_handler) {
+    ctx->mpi = fmpi_mpi_init(argc, argv, (struct fmpi_error_handler) {
         .func = fmpi_error_callback,
         .user_data = ctx
     });
@@ -79,7 +79,7 @@ struct fmpi_ctx * fmpi_ctx_create(int * argc, char ** argv[])
         return NULL;
     }
     // Futhark Initialization
-    ctx->fut = fmpi_futhark_init(&(struct fmpi_error_handler) {
+    ctx->fut = fmpi_futhark_init((struct fmpi_error_handler) {
         .func = fmpi_error_callback,
         .user_data = ctx
     });
diff --git a/src/fmpi_error.c b/src/fmpi_error.c
index eec4f56..8b8b397 100644
--- a/src/fmpi_error.c
+++ b/src/fmpi_error.c
@@ -35,7 +35,7 @@
     fmpi_raise_error()
 ------------------------------------------------------------------------------*/
 void fmpi_raise_error(
-    const struct fmpi_error_handler * const err_handler,
+    const struct fmpi_error_handler err_handler,
     const char * const file, const size_t line, const char * const func,
     const char * const module, const char * const fmt, ...
 ){
@@ -43,7 +43,7 @@ void fmpi_raise_error(
     assert(func != NULL);
     assert(module != NULL);
     assert(fmt != NULL);
-    if(err_handler->func == NULL) {
+    if(err_handler.func == NULL) {
         return;
     }
     const time_t t = time(NULL);
@@ -60,7 +60,7 @@ void fmpi_raise_error(
         }
         va_end(args);
     }
-    err_handler->func(&(const struct fmpi_error){
+    err_handler.func(&(const struct fmpi_error){
             .file = file,
             .line = line,
             .func = func,
@@ -68,7 +68,7 @@ void fmpi_raise_error(
             .module = module,
             .msg = msg_str,
         },
-        err_handler->user_data
+        err_handler.user_data
     );
 }
 /*------------------------------------------------------------------------------
diff --git a/src/fmpi_futhark.c b/src/fmpi_futhark.c
index e49b554..c3137f4 100644
--- a/src/fmpi_futhark.c
+++ b/src/fmpi_futhark.c
@@ -50,7 +50,7 @@ static const fmpi_futhark_new_data_func fmpi_futhark_new_data_func_list[] = {
 ==============================================================================*/
 #define FMPI_RAISE_FUTHARK_ERROR(ctx, ...) \
 do { \
-    if((ctx)->err_handler != NULL) { \
+    if((ctx)->err_handler.func != NULL) { \
         FMPI_RAISE_ERROR((ctx)->err_handler, "FUTHARK", __VA_ARGS__); \
     } \
 } while(0)
@@ -61,16 +61,14 @@ do { \
     fmpi_futhark_init()
 ------------------------------------------------------------------------------*/
 struct fmpi_futhark_ctx * fmpi_futhark_init(
-    const struct fmpi_error_handler * const err_handler
+    const struct fmpi_error_handler err_handler
 ){
     struct fmpi_futhark_ctx * ctx = malloc(sizeof(*ctx));
     if(ctx == NULL) {
-        if(err_handler != NULL) {
-            FMPI_RAISE_ERROR(err_handler, "FUTHARK", "malloc(fmpi_futhark_ctx) failed!");
-        }
+        FMPI_RAISE_ERROR(err_handler, "FUTHARK", "malloc(fmpi_futhark_ctx) failed!");
         return NULL;
     }
-    ctx->err_handler = (err_handler != NULL) ? err_handler : NULL;
+    ctx->err_handler = err_handler;
     ctx->cfg = futhark_context_config_new();
     if(ctx->cfg == NULL) {
         FMPI_RAISE_FUTHARK_ERROR(ctx, "futhark_context_config_new() failed!");
diff --git a/src/fmpi_mpi.c b/src/fmpi_mpi.c
index 4df0f27..11e95ff 100644
--- a/src/fmpi_mpi.c
+++ b/src/fmpi_mpi.c
@@ -41,7 +41,7 @@
 ==============================================================================*/
 #define FMPI_RAISE_MPI_ERROR(ctx, ...) \
 do { \
-    if((ctx)->err_handler != NULL) { \
+    if((ctx)->err_handler.func != NULL) { \
         FMPI_RAISE_ERROR((ctx)->err_handler, "MPI", __VA_ARGS__); \
     } \
 } while(0)
@@ -53,17 +53,15 @@ do { \
 ------------------------------------------------------------------------------*/
 struct fmpi_mpi_ctx * fmpi_mpi_init(
     int * const argc, char *** const argv,
-    const struct fmpi_error_handler * const err_handler
+    const struct fmpi_error_handler err_handler
 ){
     struct fmpi_mpi_ctx * ctx = malloc(sizeof(*ctx));
     if(ctx == NULL) {
-        if(err_handler != NULL) {
-            FMPI_RAISE_ERROR(err_handler, "MPI", "malloc(fmpi_mpi_ctx) failed!");
-        }
+        FMPI_RAISE_ERROR(err_handler, "MPI", "malloc(fmpi_mpi_ctx) failed!");
         return NULL;
     }
     ctx->root = FMPI_MPI_ROOT;
-    ctx->err_handler = (err_handler != NULL) ? err_handler : NULL;
+    ctx->err_handler = err_handler;
 
     int err_id = MPI_Init(argc, argv);
     if(fmpi_mpi_check_error(ctx, err_id, "MPI_Init") == true) {
-- 
GitLab