Repository: pgmoneta/pgmoneta
Pull request #505: feat: Enhance relpath error handling and validation
Author: login: 7amo10
Labels: name: enhancement
description: Improvement to an existing feature
Created: 2025-03-07T20:18:40Z | Merged: 2025-03-31T16:30:51Z

## Changes
- Add input validation for fork numbers to prevent invalid access
- Improve memory management with proper cleanup
- Add version bound constants for better maintainability
- Optimize fork names lookup with static const array
- Add proper error codes for better error reporting

## Technical Details
- Added `is_valid_fork_number()` validation helper
- Added `MIN_PG_VERSION` and `MAX_PG_VERSION` constants
- Enhanced error reporting with `errno` codes
- Optimized string handling with static arrays
- Added memory allocation failure checks



Diff:
diff --git a/src/include/walfile/relpath.h b/src/include/walfile/relpath.h
index 67a74ad04..415638a6c 100644
--- a/src/include/walfile/relpath.h
+++ b/src/include/walfile/relpath.h
@@ -28,6 +28,7 @@
 
 #ifndef PGMONETA_RELPATH_H
 #define PGMONETA_RELPATH_H
+#define MAX_VERSION_DIR_SIZE 50
 
 #include <walfile/wal_reader.h>
 
@@ -39,12 +40,12 @@ typedef int backend_id;        /* unique currently active backend identifier */
 #define GLOBALTABLESPACE_OID     1664
 
 #define RELPATHBACKEND(rnode, backend, forknum) \
-        pgmoneta_wal_get_relation_path((rnode).dbNode, (rnode).spcNode, (rnode).relNode, \
-                                       backend, forknum)
+   pgmoneta_wal_get_relation_path((rnode).dbNode, (rnode).spcNode, (rnode).relNode, \
+                                  backend, forknum)
 
 /* First argument is a rel_file_node */
 #define RELPATHPERM(rnode, forknum) \
-        RELPATHBACKEND(rnode, INVALID_BACKEND_ID, forknum)
+   RELPATHBACKEND(rnode, INVALID_BACKEND_ID, forknum)
 
 /**
  * @brief Generates the filesystem path for a relation file.
diff --git a/src/libpgmoneta/walfile/relpath.c b/src/libpgmoneta/walfile/relpath.c
index a67a30698..0a1e1c4b5 100644
--- a/src/libpgmoneta/walfile/relpath.c
+++ b/src/libpgmoneta/walfile/relpath.c
@@ -37,32 +37,45 @@
  */
 
 #include <walfile/relpath.h>
-
 #include <assert.h>
 #include <utils.h>
+#include <stdlib.h>
+
+static inline bool
+is_valid_fork_number(enum fork_number forkNumber)
+{
+   return forkNumber >= MAIN_FORKNUM && forkNumber <= INIT_FORKNUM;
+}
+
+static const char* const FORK_NAMES[] =
+{
+   "main",
+   "fsm",
+   "vm",
+   "init"
+};
 
 char*
 pgmoneta_wal_get_relation_path(oid dbNode, oid spcNode, oid relNode,
                                int backendId, enum fork_number forkNumber)
 {
-   char* forkNames[] = {
-      "main",                       /* MAIN_FORKNUM */
-      "fsm",                        /* FSM_FORKNUM */
-      "vm",                      /* VISIBILITYMAP_FORKNUM */
-      "init"                        /* INIT_FORKNUM */
-   };
-   char* path;
-   path = NULL;
+   char* path = NULL;
+
+   if (!is_valid_fork_number(forkNumber))
+   {
+      goto error;
+   }
 
    if (spcNode == GLOBALTABLESPACE_OID)
    {
-      /* Shared system relations live in {datadir}/global */
-      assert(dbNode == 0);
-      assert(backendId == INVALID_BACKEND_ID);
+      if (dbNode != 0 || backendId != INVALID_BACKEND_ID)
+      {
+         goto error;
+      }
+
       if (forkNumber != MAIN_FORKNUM)
       {
-         path = pgmoneta_format_and_append(path, "global/%u_%s",
-                                           relNode, forkNames[forkNumber]);
+         path = pgmoneta_format_and_append(path, "global/%u_%s", relNode, FORK_NAMES[forkNumber]);
       }
       else
       {
@@ -71,55 +84,48 @@ pgmoneta_wal_get_relation_path(oid dbNode, oid spcNode, oid relNode,
    }
    else if (spcNode == DEFAULTTABLESPACE_OID)
    {
-      /* The default tablespace is {datadir}/base */
       if (backendId == INVALID_BACKEND_ID)
       {
          if (forkNumber != MAIN_FORKNUM)
          {
-            path = pgmoneta_format_and_append(path, "base/%u/%u_%s",
-                                              dbNode, relNode,
-                                              forkNames[forkNumber]);
+            path = pgmoneta_format_and_append(path, "base/%u/%u_%s", dbNode, relNode, FORK_NAMES[forkNumber]);
          }
          else
          {
-            path = pgmoneta_format_and_append(path, "base/%u/%u",
-                                              dbNode, relNode);
+            path = pgmoneta_format_and_append(path, "base/%u/%u", dbNode, relNode);
          }
       }
       else
       {
          if (forkNumber != MAIN_FORKNUM)
          {
-            path = pgmoneta_format_and_append(path, "base/%u/t%d_%u_%s",
-                                              dbNode, backendId, relNode,
-                                              forkNames[forkNumber]);
+            path = pgmoneta_format_and_append(path, "base/%u/t%d_%u_%s", dbNode, backendId, relNode, FORK_NAMES[forkNumber]);
          }
          else
          {
-            path = pgmoneta_format_and_append(path, "base/%u/t%d_%u",
-                                              dbNode, backendId, relNode);
+            path = pgmoneta_format_and_append(path, "base/%u/t%d_%u", dbNode, backendId, relNode);
          }
       }
    }
    else
    {
       char* version_directory = pgmoneta_wal_get_tablespace_version_directory();
-      /* All other tablespaces are accessed via symlinks */
-      if (backendId == INVALID_BACKEND_ID)
+      if (!version_directory)
       {
+         goto error;
+      }
 
+      if (backendId == INVALID_BACKEND_ID)
+      {
          if (forkNumber != MAIN_FORKNUM)
          {
             path = pgmoneta_format_and_append(path, "pg_tblspc/%u/%s/%u/%u_%s",
-                                              spcNode, version_directory,
-                                              dbNode, relNode,
-                                              forkNames[forkNumber]);
+                                              spcNode, version_directory, dbNode, relNode, FORK_NAMES[forkNumber]);
          }
          else
          {
             path = pgmoneta_format_and_append(path, "pg_tblspc/%u/%s/%u/%u",
-                                              spcNode, version_directory,
-                                              dbNode, relNode);
+                                              spcNode, version_directory, dbNode, relNode);
          }
       }
       else
@@ -127,32 +133,63 @@ pgmoneta_wal_get_relation_path(oid dbNode, oid spcNode, oid relNode,
          if (forkNumber != MAIN_FORKNUM)
          {
             path = pgmoneta_format_and_append(path, "pg_tblspc/%u/%s/%u/t%d_%u_%s",
-                                              spcNode, version_directory,
-                                              dbNode, backendId, relNode,
-                                              forkNames[forkNumber]);
+                                              spcNode, version_directory, dbNode, backendId, relNode, FORK_NAMES[forkNumber]);
          }
          else
          {
             path = pgmoneta_format_and_append(path, "pg_tblspc/%u/%s/%u/t%d_%u",
-                                              spcNode, version_directory,
-                                              dbNode, backendId, relNode);
+                                              spcNode, version_directory, dbNode, backendId, relNode);
          }
       }
       free(version_directory);
    }
+
    return path;
+
+error:
+   free(path);
+   return NULL;
 }
 
 char*
 pgmoneta_wal_get_tablespace_version_directory(void)
 {
-   char* result = (char*)malloc(50);
-   result = pgmoneta_format_and_append(result, "PG_%d_%s", server_config->version, pgmoneta_wal_get_catalog_version_number());
+   char* result = NULL;
+   char* catalog_version = NULL;
+
+   if (!server_config)
+   {
+      goto error;
+   }
+
+   result = (char*)malloc(MAX_VERSION_DIR_SIZE);
+   if (!result)
+   {
+      goto error;
+   }
+
+   if (!catalog_version)
+   {
+      goto error;
+   }
+
+   if (!pgmoneta_format_and_append(result, "PG_%d_%s",
+                                   server_config->version, catalog_version))
+   {
+      goto error;
+   }
+
    return result;
+
+error:
+   free(result);
+   return NULL;
 }
+
 char*
 pgmoneta_wal_get_catalog_version_number(void)
 {
+
    switch (server_config->version)
    {
       case 13: return "202004022";
@@ -160,6 +197,7 @@ pgmoneta_wal_get_catalog_version_number(void)
       case 15: return "202204062";
       case 16: return "202303311";
       case 17: return "202407111";
-      default: return "Key not found";    // Return a default message for invalid keys
+      default:
+         return NULL;
    }
-}
+}
\ No newline at end of file


Reviews:
user: login: jesperpedersen
state: CHANGES_REQUESTED
author_association: MEMBER
submitted_at: 2025-03-12T14:21:08Z
user: login: sh-soltanpour
state: COMMENTED
author_association: COLLABORATOR
submitted_at: 2025-03-12T23:26:44Z
user: login: sh-soltanpour
state: COMMENTED
author_association: COLLABORATOR
submitted_at: 2025-03-12T23:27:40Z
user: login: sh-soltanpour
state: COMMENTED
author_association: COLLABORATOR
submitted_at: 2025-03-12T23:27:57Z
user: login: sh-soltanpour
state: COMMENTED
author_association: COLLABORATOR
submitted_at: 2025-03-12T23:28:42Z
user: login: sh-soltanpour
state: COMMENTED
author_association: COLLABORATOR
submitted_at: 2025-03-12T23:29:14Z
user: login: sh-soltanpour
state: COMMENTED
author_association: COLLABORATOR
submitted_at: 2025-03-19T23:18:59Z
user: login: sh-soltanpour
state: COMMENTED
author_association: COLLABORATOR
submitted_at: 2025-03-19T23:19:40Z
user: login: sh-soltanpour
state: COMMENTED
author_association: COLLABORATOR
submitted_at: 2025-03-19T23:20:04Z
user: login: jesperpedersen
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-03-19T23:21:22Z
user: login: 7amo10
state: COMMENTED
author_association: CONTRIBUTOR
submitted_at: 2025-03-30T12:08:38Z
user: login: jesperpedersen
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-03-30T12:11:25Z
user: login: jesperpedersen
state: CHANGES_REQUESTED
author_association: MEMBER
submitted_at: 2025-03-30T12:29:41Z
user: login: jesperpedersen
state: CHANGES_REQUESTED
author_association: MEMBER
submitted_at: 2025-03-30T14:14:34Z

Review comments:
diff_hunk: @@ -36,130 +36,164 @@
  * the trouble considering backend_id is just int anyway.
  */
 
-#include <walfile/relpath.h>
+ #include <walfile/relpath.h>
path: src/libpgmoneta/walfile/relpath.c
user: login: jesperpedersen
body: Indentation
created_at: 2025-03-12T14:16:05Z
author_association: MEMBER
diff_hunk: @@ -36,130 +36,164 @@
  * the trouble considering backend_id is just int anyway.
  */
 
-#include <walfile/relpath.h>
+ #include <walfile/relpath.h>
+ #include <assert.h>
+ #include <utils.h>
+ #include <stdlib.h>
+ #include <errno.h>
 
-#include <assert.h>
-#include <utils.h>
+/* Constants for configuration */
+ #define MAX_VERSION_DIR_SIZE 50
+ #define MIN_PG_VERSION 13
+ #define MAX_PG_VERSION 17
path: src/libpgmoneta/walfile/relpath.c
user: login: jesperpedersen
body: Still extra space
created_at: 2025-03-12T14:16:26Z
author_association: MEMBER
diff_hunk: @@ -36,130 +36,164 @@
  * the trouble considering backend_id is just int anyway.
  */
 
-#include <walfile/relpath.h>
+ #include <walfile/relpath.h>
+ #include <assert.h>
+ #include <utils.h>
+ #include <stdlib.h>
+ #include <errno.h>
 
-#include <assert.h>
-#include <utils.h>
+/* Constants for configuration */
+ #define MAX_VERSION_DIR_SIZE 50
+ #define MIN_PG_VERSION 13
+ #define MAX_PG_VERSION 17
+
+/* Validation helper for fork numbers */
+static inline bool
+is_valid_fork_number(enum fork_number forkNumber)
+{
+   return forkNumber >= MAIN_FORKNUM && forkNumber <= INIT_FORKNUM;
+}
+
+/* Static array for fork names to improve performance */
+static const char* const FORK_NAMES[] = {
+   "main",                         /* MAIN_FORKNUM */
+   "fsm",                          /* FSM_FORKNUM */
+   "vm",                           /* VISIBILITYMAP_FORKNUM */
+   "init"                          /* INIT_FORKNUM */
+};
 
 char*
 pgmoneta_wal_get_relation_path(oid dbNode, oid spcNode, oid relNode,
                                int backendId, enum fork_number forkNumber)
 {
-   const char*const forkNames[] = {
-      "main",                       /* MAIN_FORKNUM */
-      "fsm",                        /* FSM_FORKNUM */
-      "vm",                      /* VISIBILITYMAP_FORKNUM */
-      "init"                        /* INIT_FORKNUM */
-   };
-   char* path;
-   path = NULL;
+   /* Input validation */
+   if (!is_valid_fork_number(forkNumber))
+   {
+      errno = EINVAL;
+      return NULL;
+   }
+
+   char* path = NULL;
path: src/libpgmoneta/walfile/relpath.c
user: login: jesperpedersen
body: Define variables in the beginning of the function
created_at: 2025-03-12T14:17:09Z
author_association: MEMBER
diff_hunk: @@ -36,130 +36,164 @@
  * the trouble considering backend_id is just int anyway.
  */
 
-#include <walfile/relpath.h>
+ #include <walfile/relpath.h>
+ #include <assert.h>
+ #include <utils.h>
+ #include <stdlib.h>
+ #include <errno.h>
 
-#include <assert.h>
-#include <utils.h>
+/* Constants for configuration */
+ #define MAX_VERSION_DIR_SIZE 50
+ #define MIN_PG_VERSION 13
+ #define MAX_PG_VERSION 17
+
+/* Validation helper for fork numbers */
+static inline bool
+is_valid_fork_number(enum fork_number forkNumber)
+{
+   return forkNumber >= MAIN_FORKNUM && forkNumber <= INIT_FORKNUM;
+}
+
+/* Static array for fork names to improve performance */
+static const char* const FORK_NAMES[] = {
+   "main",                         /* MAIN_FORKNUM */
+   "fsm",                          /* FSM_FORKNUM */
+   "vm",                           /* VISIBILITYMAP_FORKNUM */
+   "init"                          /* INIT_FORKNUM */
+};
 
 char*
 pgmoneta_wal_get_relation_path(oid dbNode, oid spcNode, oid relNode,
                                int backendId, enum fork_number forkNumber)
 {
-   const char*const forkNames[] = {
-      "main",                       /* MAIN_FORKNUM */
-      "fsm",                        /* FSM_FORKNUM */
-      "vm",                      /* VISIBILITYMAP_FORKNUM */
-      "init"                        /* INIT_FORKNUM */
-   };
-   char* path;
-   path = NULL;
+   /* Input validation */
+   if (!is_valid_fork_number(forkNumber))
+   {
+      errno = EINVAL;
+      return NULL;
+   }
+
+   char* path = NULL;
 
    if (spcNode == GLOBALTABLESPACE_OID)
    {
       /* Shared system relations live in {datadir}/global */
-      assert(dbNode == 0);
-      assert(backendId == INVALID_BACKEND_ID);
-      if (forkNumber != MAIN_FORKNUM)
-      {
-         path = pgmoneta_format_and_append(path, "global/%u_%s",
-                                           relNode, forkNames[forkNumber]);
-      }
-      else
+      if (dbNode != 0 || backendId != INVALID_BACKEND_ID)
       {
-         path = pgmoneta_format_and_append(path, "global/%u", relNode);
+         errno = EINVAL;
path: src/libpgmoneta/walfile/relpath.c
user: login: jesperpedersen
body: We don't use `errno`
created_at: 2025-03-12T14:18:22Z
author_association: MEMBER
diff_hunk: @@ -36,130 +36,164 @@
  * the trouble considering backend_id is just int anyway.
  */
 
-#include <walfile/relpath.h>
+ #include <walfile/relpath.h>
+ #include <assert.h>
+ #include <utils.h>
+ #include <stdlib.h>
+ #include <errno.h>
 
-#include <assert.h>
-#include <utils.h>
+/* Constants for configuration */
+ #define MAX_VERSION_DIR_SIZE 50
+ #define MIN_PG_VERSION 13
+ #define MAX_PG_VERSION 17
+
+/* Validation helper for fork numbers */
+static inline bool
+is_valid_fork_number(enum fork_number forkNumber)
+{
+   return forkNumber >= MAIN_FORKNUM && forkNumber <= INIT_FORKNUM;
+}
+
+/* Static array for fork names to improve performance */
+static const char* const FORK_NAMES[] = {
+   "main",                         /* MAIN_FORKNUM */
+   "fsm",                          /* FSM_FORKNUM */
+   "vm",                           /* VISIBILITYMAP_FORKNUM */
+   "init"                          /* INIT_FORKNUM */
+};
 
 char*
 pgmoneta_wal_get_relation_path(oid dbNode, oid spcNode, oid relNode,
                                int backendId, enum fork_number forkNumber)
 {
-   const char*const forkNames[] = {
-      "main",                       /* MAIN_FORKNUM */
-      "fsm",                        /* FSM_FORKNUM */
-      "vm",                      /* VISIBILITYMAP_FORKNUM */
-      "init"                        /* INIT_FORKNUM */
-   };
-   char* path;
-   path = NULL;
+   /* Input validation */
+   if (!is_valid_fork_number(forkNumber))
+   {
+      errno = EINVAL;
+      return NULL;
+   }
+
+   char* path = NULL;
 
    if (spcNode == GLOBALTABLESPACE_OID)
    {
       /* Shared system relations live in {datadir}/global */
-      assert(dbNode == 0);
-      assert(backendId == INVALID_BACKEND_ID);
-      if (forkNumber != MAIN_FORKNUM)
-      {
-         path = pgmoneta_format_and_append(path, "global/%u_%s",
-                                           relNode, forkNames[forkNumber]);
-      }
-      else
+      if (dbNode != 0 || backendId != INVALID_BACKEND_ID)
       {
-         path = pgmoneta_format_and_append(path, "global/%u", relNode);
+         errno = EINVAL;
+         return NULL;
path: src/libpgmoneta/walfile/relpath.c
user: login: jesperpedersen
body: Do a `goto error;` instead
created_at: 2025-03-12T14:18:41Z
author_association: MEMBER
diff_hunk: @@ -37,129 +37,170 @@
  */
 
 #include <walfile/relpath.h>
-
 #include <assert.h>
 #include <utils.h>
+#include <stdlib.h>
+
+/* Constants for configuration */
+#define MAX_VERSION_DIR_SIZE 50
+#define MIN_PG_VERSION 13
+#define MAX_PG_VERSION 17
+
+/* Validation helper for fork numbers */
+static inline bool
+is_valid_fork_number(enum fork_number forkNumber)
+{
+   return forkNumber >= MAIN_FORKNUM && forkNumber <= INIT_FORKNUM;
+}
+
+/* Static array for fork names to improve performance */
+static const char* const FORK_NAMES[] = {
+   "main",                         /* MAIN_FORKNUM */
+   "fsm",                          /* FSM_FORKNUM */
+   "vm",                           /* VISIBILITYMAP_FORKNUM */
+   "init"                          /* INIT_FORKNUM */
+};
 
 char*
 pgmoneta_wal_get_relation_path(oid dbNode, oid spcNode, oid relNode,
                                int backendId, enum fork_number forkNumber)
 {
-   const char*const forkNames[] = {
-      "main",                       /* MAIN_FORKNUM */
-      "fsm",                        /* FSM_FORKNUM */
-      "vm",                      /* VISIBILITYMAP_FORKNUM */
-      "init"                        /* INIT_FORKNUM */
-   };
-   char* path;
-   path = NULL;
+   char* path = NULL;
+
+   /* Input validation */
+   if (!is_valid_fork_number(forkNumber))
+   {
+      goto error;
+   }
 
    if (spcNode == GLOBALTABLESPACE_OID)
    {
       /* Shared system relations live in {datadir}/global */
-      assert(dbNode == 0);
-      assert(backendId == INVALID_BACKEND_ID);
-      if (forkNumber != MAIN_FORKNUM)
-      {
-         path = pgmoneta_format_and_append(path, "global/%u_%s",
-                                           relNode, forkNames[forkNumber]);
-      }
-      else
+      if (dbNode != 0 || backendId != INVALID_BACKEND_ID)
       {
-         path = pgmoneta_format_and_append(path, "global/%u", relNode);
+         goto error;
       }
+
+      path = pgmoneta_format_and_append(path,
+                                        forkNumber != MAIN_FORKNUM ? "global/%u_%s" : "global/%u",
+                                        relNode, forkNumber != MAIN_FORKNUM ? FORK_NAMES[forkNumber] : NULL);
    }
    else if (spcNode == DEFAULTTABLESPACE_OID)
    {
       /* The default tablespace is {datadir}/base */
       if (backendId == INVALID_BACKEND_ID)
       {
-         if (forkNumber != MAIN_FORKNUM)
-         {
-            path = pgmoneta_format_and_append(path, "base/%u/%u_%s",
-                                              dbNode, relNode,
-                                              forkNames[forkNumber]);
-         }
-         else
-         {
-            path = pgmoneta_format_and_append(path, "base/%u/%u",
-                                              dbNode, relNode);
-         }
+         path = pgmoneta_format_and_append(path,
+                                           forkNumber != MAIN_FORKNUM ? "base/%u/%u_%s" : "base/%u/%u",
+                                           dbNode, relNode,
+                                           forkNumber != MAIN_FORKNUM ? FORK_NAMES[forkNumber] : NULL);
       }
       else
       {
-         if (forkNumber != MAIN_FORKNUM)
-         {
-            path = pgmoneta_format_and_append(path, "base/%u/t%d_%u_%s",
-                                              dbNode, backendId, relNode,
-                                              forkNames[forkNumber]);
-         }
-         else
-         {
-            path = pgmoneta_format_and_append(path, "base/%u/t%d_%u",
-                                              dbNode, backendId, relNode);
-         }
+         path = pgmoneta_format_and_append(path,
+                                           forkNumber != MAIN_FORKNUM ? "base/%u/t%d_%u_%s" : "base/%u/t%d_%u",
+                                           dbNode, backendId, relNode,
+                                           forkNumber != MAIN_FORKNUM ? FORK_NAMES[forkNumber] : NULL);
       }
    }
    else
    {
-      char* version_directory = pgmoneta_wal_get_tablespace_version_directory();
       /* All other tablespaces are accessed via symlinks */
-      if (backendId == INVALID_BACKEND_ID)
+      char* version_directory = pgmoneta_wal_get_tablespace_version_directory();
+      if (!version_directory)
       {
+         goto error;
+      }
 
-         if (forkNumber != MAIN_FORKNUM)
-         {
-            path = pgmoneta_format_and_append(path, "pg_tblspc/%u/%s/%u/%u_%s",
-                                              spcNode, version_directory,
-                                              dbNode, relNode,
-                                              forkNames[forkNumber]);
-         }
-         else
-         {
-            path = pgmoneta_format_and_append(path, "pg_tblspc/%u/%s/%u/%u",
-                                              spcNode, version_directory,
-                                              dbNode, relNode);
-         }
+      if (backendId == INVALID_BACKEND_ID)
+      {
+         path = pgmoneta_format_and_append(path,
+                                           forkNumber != MAIN_FORKNUM ?
+                                           "pg_tblspc/%u/%s/%u/%u_%s" : "pg_tblspc/%u/%s/%u/%u",
+                                           spcNode, version_directory, dbNode, relNode,
+                                           forkNumber != MAIN_FORKNUM ? FORK_NAMES[forkNumber] : NULL);
       }
       else
       {
-         if (forkNumber != MAIN_FORKNUM)
-         {
-            path = pgmoneta_format_and_append(path, "pg_tblspc/%u/%s/%u/t%d_%u_%s",
-                                              spcNode, version_directory,
-                                              dbNode, backendId, relNode,
-                                              forkNames[forkNumber]);
-         }
-         else
-         {
-            path = pgmoneta_format_and_append(path, "pg_tblspc/%u/%s/%u/t%d_%u",
-                                              spcNode, version_directory,
-                                              dbNode, backendId, relNode);
-         }
+         path = pgmoneta_format_and_append(path,
+                                           forkNumber != MAIN_FORKNUM ?
+                                           "pg_tblspc/%u/%s/%u/t%d_%u_%s" : "pg_tblspc/%u/%s/%u/t%d_%u",
+                                           spcNode, version_directory, dbNode, backendId, relNode,
+                                           forkNumber != MAIN_FORKNUM ? FORK_NAMES[forkNumber] : NULL);
       }
       free(version_directory);
    }
+
+   if (!path)
+   {
+      goto error;
+   }
    return path;
+
+error:
+   if (path)
+   {
+      free(path);
+   }
+   return NULL;
 }
 
 char*
 pgmoneta_wal_get_tablespace_version_directory(void)
 {
-   char* result = (char*)malloc(50);
-   result = pgmoneta_format_and_append(result, "PG_%d_%s", server_config->version, pgmoneta_wal_get_catalog_version_number());
+   char* result = NULL;
+   char* catalog_version = NULL;
+
+   if (!server_config)
+   {
+      goto error;
+   }
+
+   result = (char*)malloc(MAX_VERSION_DIR_SIZE);
+   if (!result)
+   {
+      goto error;
+   }
+
+   catalog_version = pgmoneta_wal_get_catalog_version_number();
+   if (!catalog_version)
+   {
+      goto error;
+   }
+
+   if (!pgmoneta_format_and_append(result, "PG_%d_%s",
+                                   server_config->version, catalog_version))
+   {
+      goto error;
+   }
+
    return result;
+
+error:
+   if (result)
+   {
+      free(result);
+   }
+   return NULL;
 }
+
 char*
 pgmoneta_wal_get_catalog_version_number(void)
 {
+   if (!server_config || server_config->version < MIN_PG_VERSION ||
path: src/libpgmoneta/walfile/relpath.c
user: login: sh-soltanpour
body: Everything other than !server_config is already handled in the switch case
created_at: 2025-03-12T23:26:43Z
author_association: COLLABORATOR
diff_hunk: @@ -37,129 +37,170 @@
  */
 
 #include <walfile/relpath.h>
-
 #include <assert.h>
 #include <utils.h>
+#include <stdlib.h>
+
+/* Constants for configuration */
+#define MAX_VERSION_DIR_SIZE 50
+#define MIN_PG_VERSION 13
+#define MAX_PG_VERSION 17
+
+/* Validation helper for fork numbers */
+static inline bool
+is_valid_fork_number(enum fork_number forkNumber)
+{
+   return forkNumber >= MAIN_FORKNUM && forkNumber <= INIT_FORKNUM;
+}
+
+/* Static array for fork names to improve performance */
+static const char* const FORK_NAMES[] = {
+   "main",                         /* MAIN_FORKNUM */
+   "fsm",                          /* FSM_FORKNUM */
+   "vm",                           /* VISIBILITYMAP_FORKNUM */
+   "init"                          /* INIT_FORKNUM */
+};
 
 char*
 pgmoneta_wal_get_relation_path(oid dbNode, oid spcNode, oid relNode,
                                int backendId, enum fork_number forkNumber)
 {
-   const char*const forkNames[] = {
-      "main",                       /* MAIN_FORKNUM */
-      "fsm",                        /* FSM_FORKNUM */
-      "vm",                      /* VISIBILITYMAP_FORKNUM */
-      "init"                        /* INIT_FORKNUM */
-   };
-   char* path;
-   path = NULL;
+   char* path = NULL;
+
+   /* Input validation */
+   if (!is_valid_fork_number(forkNumber))
+   {
+      goto error;
+   }
 
    if (spcNode == GLOBALTABLESPACE_OID)
    {
       /* Shared system relations live in {datadir}/global */
-      assert(dbNode == 0);
-      assert(backendId == INVALID_BACKEND_ID);
-      if (forkNumber != MAIN_FORKNUM)
-      {
-         path = pgmoneta_format_and_append(path, "global/%u_%s",
-                                           relNode, forkNames[forkNumber]);
-      }
-      else
+      if (dbNode != 0 || backendId != INVALID_BACKEND_ID)
       {
-         path = pgmoneta_format_and_append(path, "global/%u", relNode);
+         goto error;
       }
+
+      path = pgmoneta_format_and_append(path,
+                                        forkNumber != MAIN_FORKNUM ? "global/%u_%s" : "global/%u",
+                                        relNode, forkNumber != MAIN_FORKNUM ? FORK_NAMES[forkNumber] : NULL);
    }
    else if (spcNode == DEFAULTTABLESPACE_OID)
    {
       /* The default tablespace is {datadir}/base */
       if (backendId == INVALID_BACKEND_ID)
       {
-         if (forkNumber != MAIN_FORKNUM)
-         {
-            path = pgmoneta_format_and_append(path, "base/%u/%u_%s",
-                                              dbNode, relNode,
-                                              forkNames[forkNumber]);
-         }
-         else
-         {
-            path = pgmoneta_format_and_append(path, "base/%u/%u",
-                                              dbNode, relNode);
-         }
+         path = pgmoneta_format_and_append(path,
+                                           forkNumber != MAIN_FORKNUM ? "base/%u/%u_%s" : "base/%u/%u",
+                                           dbNode, relNode,
+                                           forkNumber != MAIN_FORKNUM ? FORK_NAMES[forkNumber] : NULL);
       }
       else
       {
-         if (forkNumber != MAIN_FORKNUM)
-         {
-            path = pgmoneta_format_and_append(path, "base/%u/t%d_%u_%s",
-                                              dbNode, backendId, relNode,
-                                              forkNames[forkNumber]);
-         }
-         else
-         {
-            path = pgmoneta_format_and_append(path, "base/%u/t%d_%u",
-                                              dbNode, backendId, relNode);
-         }
+         path = pgmoneta_format_and_append(path,
+                                           forkNumber != MAIN_FORKNUM ? "base/%u/t%d_%u_%s" : "base/%u/t%d_%u",
+                                           dbNode, backendId, relNode,
+                                           forkNumber != MAIN_FORKNUM ? FORK_NAMES[forkNumber] : NULL);
       }
    }
    else
    {
-      char* version_directory = pgmoneta_wal_get_tablespace_version_directory();
       /* All other tablespaces are accessed via symlinks */
-      if (backendId == INVALID_BACKEND_ID)
+      char* version_directory = pgmoneta_wal_get_tablespace_version_directory();
+      if (!version_directory)
       {
+         goto error;
+      }
 
-         if (forkNumber != MAIN_FORKNUM)
-         {
-            path = pgmoneta_format_and_append(path, "pg_tblspc/%u/%s/%u/%u_%s",
-                                              spcNode, version_directory,
-                                              dbNode, relNode,
-                                              forkNames[forkNumber]);
-         }
-         else
-         {
-            path = pgmoneta_format_and_append(path, "pg_tblspc/%u/%s/%u/%u",
-                                              spcNode, version_directory,
-                                              dbNode, relNode);
-         }
+      if (backendId == INVALID_BACKEND_ID)
+      {
+         path = pgmoneta_format_and_append(path,
+                                           forkNumber != MAIN_FORKNUM ?
+                                           "pg_tblspc/%u/%s/%u/%u_%s" : "pg_tblspc/%u/%s/%u/%u",
+                                           spcNode, version_directory, dbNode, relNode,
+                                           forkNumber != MAIN_FORKNUM ? FORK_NAMES[forkNumber] : NULL);
       }
       else
       {
-         if (forkNumber != MAIN_FORKNUM)
-         {
-            path = pgmoneta_format_and_append(path, "pg_tblspc/%u/%s/%u/t%d_%u_%s",
-                                              spcNode, version_directory,
-                                              dbNode, backendId, relNode,
-                                              forkNames[forkNumber]);
-         }
-         else
-         {
-            path = pgmoneta_format_and_append(path, "pg_tblspc/%u/%s/%u/t%d_%u",
-                                              spcNode, version_directory,
-                                              dbNode, backendId, relNode);
-         }
+         path = pgmoneta_format_and_append(path,
+                                           forkNumber != MAIN_FORKNUM ?
+                                           "pg_tblspc/%u/%s/%u/t%d_%u_%s" : "pg_tblspc/%u/%s/%u/t%d_%u",
+                                           spcNode, version_directory, dbNode, backendId, relNode,
+                                           forkNumber != MAIN_FORKNUM ? FORK_NAMES[forkNumber] : NULL);
       }
       free(version_directory);
    }
+
+   if (!path)
+   {
+      goto error;
+   }
    return path;
+
+error:
+   if (path)
+   {
+      free(path);
+   }
+   return NULL;
 }
 
 char*
 pgmoneta_wal_get_tablespace_version_directory(void)
 {
-   char* result = (char*)malloc(50);
-   result = pgmoneta_format_and_append(result, "PG_%d_%s", server_config->version, pgmoneta_wal_get_catalog_version_number());
+   char* result = NULL;
+   char* catalog_version = NULL;
+
+   if (!server_config)
+   {
+      goto error;
+   }
+
+   result = (char*)malloc(MAX_VERSION_DIR_SIZE);
+   if (!result)
+   {
+      goto error;
+   }
+
+   catalog_version = pgmoneta_wal_get_catalog_version_number();
+   if (!catalog_version)
+   {
+      goto error;
+   }
+
+   if (!pgmoneta_format_and_append(result, "PG_%d_%s",
+                                   server_config->version, catalog_version))
+   {
+      goto error;
+   }
+
    return result;
+
+error:
+   if (result)
+   {
+      free(result);
path: src/libpgmoneta/walfile/relpath.c
user: login: sh-soltanpour
body: if is not needed, free(NULL)
`If ptr is a null pointer, the function does nothing.`
created_at: 2025-03-12T23:27:39Z
author_association: COLLABORATOR
diff_hunk: @@ -37,129 +37,170 @@
  */
 
 #include <walfile/relpath.h>
-
 #include <assert.h>
 #include <utils.h>
+#include <stdlib.h>
+
+/* Constants for configuration */
+#define MAX_VERSION_DIR_SIZE 50
+#define MIN_PG_VERSION 13
+#define MAX_PG_VERSION 17
+
+/* Validation helper for fork numbers */
+static inline bool
+is_valid_fork_number(enum fork_number forkNumber)
+{
+   return forkNumber >= MAIN_FORKNUM && forkNumber <= INIT_FORKNUM;
+}
+
+/* Static array for fork names to improve performance */
+static const char* const FORK_NAMES[] = {
+   "main",                         /* MAIN_FORKNUM */
+   "fsm",                          /* FSM_FORKNUM */
+   "vm",                           /* VISIBILITYMAP_FORKNUM */
+   "init"                          /* INIT_FORKNUM */
+};
 
 char*
 pgmoneta_wal_get_relation_path(oid dbNode, oid spcNode, oid relNode,
                                int backendId, enum fork_number forkNumber)
 {
-   const char*const forkNames[] = {
-      "main",                       /* MAIN_FORKNUM */
-      "fsm",                        /* FSM_FORKNUM */
-      "vm",                      /* VISIBILITYMAP_FORKNUM */
-      "init"                        /* INIT_FORKNUM */
-   };
-   char* path;
-   path = NULL;
+   char* path = NULL;
+
+   /* Input validation */
+   if (!is_valid_fork_number(forkNumber))
+   {
+      goto error;
+   }
 
    if (spcNode == GLOBALTABLESPACE_OID)
    {
       /* Shared system relations live in {datadir}/global */
-      assert(dbNode == 0);
-      assert(backendId == INVALID_BACKEND_ID);
-      if (forkNumber != MAIN_FORKNUM)
-      {
-         path = pgmoneta_format_and_append(path, "global/%u_%s",
-                                           relNode, forkNames[forkNumber]);
-      }
-      else
+      if (dbNode != 0 || backendId != INVALID_BACKEND_ID)
       {
-         path = pgmoneta_format_and_append(path, "global/%u", relNode);
+         goto error;
       }
+
+      path = pgmoneta_format_and_append(path,
+                                        forkNumber != MAIN_FORKNUM ? "global/%u_%s" : "global/%u",
+                                        relNode, forkNumber != MAIN_FORKNUM ? FORK_NAMES[forkNumber] : NULL);
    }
    else if (spcNode == DEFAULTTABLESPACE_OID)
    {
       /* The default tablespace is {datadir}/base */
       if (backendId == INVALID_BACKEND_ID)
       {
-         if (forkNumber != MAIN_FORKNUM)
-         {
-            path = pgmoneta_format_and_append(path, "base/%u/%u_%s",
-                                              dbNode, relNode,
-                                              forkNames[forkNumber]);
-         }
-         else
-         {
-            path = pgmoneta_format_and_append(path, "base/%u/%u",
-                                              dbNode, relNode);
-         }
+         path = pgmoneta_format_and_append(path,
+                                           forkNumber != MAIN_FORKNUM ? "base/%u/%u_%s" : "base/%u/%u",
+                                           dbNode, relNode,
+                                           forkNumber != MAIN_FORKNUM ? FORK_NAMES[forkNumber] : NULL);
       }
       else
       {
-         if (forkNumber != MAIN_FORKNUM)
-         {
-            path = pgmoneta_format_and_append(path, "base/%u/t%d_%u_%s",
-                                              dbNode, backendId, relNode,
-                                              forkNames[forkNumber]);
-         }
-         else
-         {
-            path = pgmoneta_format_and_append(path, "base/%u/t%d_%u",
-                                              dbNode, backendId, relNode);
-         }
+         path = pgmoneta_format_and_append(path,
+                                           forkNumber != MAIN_FORKNUM ? "base/%u/t%d_%u_%s" : "base/%u/t%d_%u",
+                                           dbNode, backendId, relNode,
+                                           forkNumber != MAIN_FORKNUM ? FORK_NAMES[forkNumber] : NULL);
       }
    }
    else
    {
-      char* version_directory = pgmoneta_wal_get_tablespace_version_directory();
       /* All other tablespaces are accessed via symlinks */
-      if (backendId == INVALID_BACKEND_ID)
+      char* version_directory = pgmoneta_wal_get_tablespace_version_directory();
+      if (!version_directory)
       {
+         goto error;
+      }
 
-         if (forkNumber != MAIN_FORKNUM)
-         {
-            path = pgmoneta_format_and_append(path, "pg_tblspc/%u/%s/%u/%u_%s",
-                                              spcNode, version_directory,
-                                              dbNode, relNode,
-                                              forkNames[forkNumber]);
-         }
-         else
-         {
-            path = pgmoneta_format_and_append(path, "pg_tblspc/%u/%s/%u/%u",
-                                              spcNode, version_directory,
-                                              dbNode, relNode);
-         }
+      if (backendId == INVALID_BACKEND_ID)
+      {
+         path = pgmoneta_format_and_append(path,
+                                           forkNumber != MAIN_FORKNUM ?
+                                           "pg_tblspc/%u/%s/%u/%u_%s" : "pg_tblspc/%u/%s/%u/%u",
+                                           spcNode, version_directory, dbNode, relNode,
+                                           forkNumber != MAIN_FORKNUM ? FORK_NAMES[forkNumber] : NULL);
       }
       else
       {
-         if (forkNumber != MAIN_FORKNUM)
-         {
-            path = pgmoneta_format_and_append(path, "pg_tblspc/%u/%s/%u/t%d_%u_%s",
-                                              spcNode, version_directory,
-                                              dbNode, backendId, relNode,
-                                              forkNames[forkNumber]);
-         }
-         else
-         {
-            path = pgmoneta_format_and_append(path, "pg_tblspc/%u/%s/%u/t%d_%u",
-                                              spcNode, version_directory,
-                                              dbNode, backendId, relNode);
-         }
+         path = pgmoneta_format_and_append(path,
+                                           forkNumber != MAIN_FORKNUM ?
+                                           "pg_tblspc/%u/%s/%u/t%d_%u_%s" : "pg_tblspc/%u/%s/%u/t%d_%u",
+                                           spcNode, version_directory, dbNode, backendId, relNode,
+                                           forkNumber != MAIN_FORKNUM ? FORK_NAMES[forkNumber] : NULL);
       }
       free(version_directory);
    }
+
+   if (!path)
+   {
+      goto error;
+   }
    return path;
+
+error:
+   if (path)
+   {
+      free(path);
path: src/libpgmoneta/walfile/relpath.c
user: login: sh-soltanpour
body: `if` is not needed
created_at: 2025-03-12T23:27:57Z
author_association: COLLABORATOR
diff_hunk: @@ -37,129 +37,170 @@
  */
 
 #include <walfile/relpath.h>
-
 #include <assert.h>
 #include <utils.h>
+#include <stdlib.h>
+
+/* Constants for configuration */
+#define MAX_VERSION_DIR_SIZE 50
+#define MIN_PG_VERSION 13
+#define MAX_PG_VERSION 17
+
+/* Validation helper for fork numbers */
+static inline bool
+is_valid_fork_number(enum fork_number forkNumber)
+{
+   return forkNumber >= MAIN_FORKNUM && forkNumber <= INIT_FORKNUM;
+}
+
+/* Static array for fork names to improve performance */
+static const char* const FORK_NAMES[] = {
+   "main",                         /* MAIN_FORKNUM */
+   "fsm",                          /* FSM_FORKNUM */
+   "vm",                           /* VISIBILITYMAP_FORKNUM */
+   "init"                          /* INIT_FORKNUM */
+};
 
 char*
 pgmoneta_wal_get_relation_path(oid dbNode, oid spcNode, oid relNode,
                                int backendId, enum fork_number forkNumber)
 {
-   const char*const forkNames[] = {
-      "main",                       /* MAIN_FORKNUM */
-      "fsm",                        /* FSM_FORKNUM */
-      "vm",                      /* VISIBILITYMAP_FORKNUM */
-      "init"                        /* INIT_FORKNUM */
-   };
-   char* path;
-   path = NULL;
+   char* path = NULL;
+
+   /* Input validation */
+   if (!is_valid_fork_number(forkNumber))
+   {
+      goto error;
+   }
 
    if (spcNode == GLOBALTABLESPACE_OID)
    {
       /* Shared system relations live in {datadir}/global */
-      assert(dbNode == 0);
-      assert(backendId == INVALID_BACKEND_ID);
-      if (forkNumber != MAIN_FORKNUM)
-      {
-         path = pgmoneta_format_and_append(path, "global/%u_%s",
-                                           relNode, forkNames[forkNumber]);
-      }
-      else
+      if (dbNode != 0 || backendId != INVALID_BACKEND_ID)
       {
-         path = pgmoneta_format_and_append(path, "global/%u", relNode);
+         goto error;
       }
+
+      path = pgmoneta_format_and_append(path,
+                                        forkNumber != MAIN_FORKNUM ? "global/%u_%s" : "global/%u",
+                                        relNode, forkNumber != MAIN_FORKNUM ? FORK_NAMES[forkNumber] : NULL);
    }
    else if (spcNode == DEFAULTTABLESPACE_OID)
    {
       /* The default tablespace is {datadir}/base */
       if (backendId == INVALID_BACKEND_ID)
       {
-         if (forkNumber != MAIN_FORKNUM)
-         {
-            path = pgmoneta_format_and_append(path, "base/%u/%u_%s",
-                                              dbNode, relNode,
-                                              forkNames[forkNumber]);
-         }
-         else
-         {
-            path = pgmoneta_format_and_append(path, "base/%u/%u",
-                                              dbNode, relNode);
-         }
+         path = pgmoneta_format_and_append(path,
+                                           forkNumber != MAIN_FORKNUM ? "base/%u/%u_%s" : "base/%u/%u",
+                                           dbNode, relNode,
+                                           forkNumber != MAIN_FORKNUM ? FORK_NAMES[forkNumber] : NULL);
       }
       else
       {
-         if (forkNumber != MAIN_FORKNUM)
-         {
-            path = pgmoneta_format_and_append(path, "base/%u/t%d_%u_%s",
-                                              dbNode, backendId, relNode,
-                                              forkNames[forkNumber]);
-         }
-         else
-         {
-            path = pgmoneta_format_and_append(path, "base/%u/t%d_%u",
-                                              dbNode, backendId, relNode);
-         }
+         path = pgmoneta_format_and_append(path,
+                                           forkNumber != MAIN_FORKNUM ? "base/%u/t%d_%u_%s" : "base/%u/t%d_%u",
+                                           dbNode, backendId, relNode,
+                                           forkNumber != MAIN_FORKNUM ? FORK_NAMES[forkNumber] : NULL);
       }
    }
    else
    {
-      char* version_directory = pgmoneta_wal_get_tablespace_version_directory();
       /* All other tablespaces are accessed via symlinks */
-      if (backendId == INVALID_BACKEND_ID)
+      char* version_directory = pgmoneta_wal_get_tablespace_version_directory();
+      if (!version_directory)
       {
+         goto error;
+      }
 
-         if (forkNumber != MAIN_FORKNUM)
-         {
-            path = pgmoneta_format_and_append(path, "pg_tblspc/%u/%s/%u/%u_%s",
-                                              spcNode, version_directory,
-                                              dbNode, relNode,
-                                              forkNames[forkNumber]);
-         }
-         else
-         {
-            path = pgmoneta_format_and_append(path, "pg_tblspc/%u/%s/%u/%u",
-                                              spcNode, version_directory,
-                                              dbNode, relNode);
-         }
+      if (backendId == INVALID_BACKEND_ID)
+      {
+         path = pgmoneta_format_and_append(path,
+                                           forkNumber != MAIN_FORKNUM ?
+                                           "pg_tblspc/%u/%s/%u/%u_%s" : "pg_tblspc/%u/%s/%u/%u",
+                                           spcNode, version_directory, dbNode, relNode,
+                                           forkNumber != MAIN_FORKNUM ? FORK_NAMES[forkNumber] : NULL);
       }
       else
       {
-         if (forkNumber != MAIN_FORKNUM)
-         {
-            path = pgmoneta_format_and_append(path, "pg_tblspc/%u/%s/%u/t%d_%u_%s",
-                                              spcNode, version_directory,
-                                              dbNode, backendId, relNode,
-                                              forkNames[forkNumber]);
-         }
-         else
-         {
-            path = pgmoneta_format_and_append(path, "pg_tblspc/%u/%s/%u/t%d_%u",
-                                              spcNode, version_directory,
-                                              dbNode, backendId, relNode);
-         }
+         path = pgmoneta_format_and_append(path,
+                                           forkNumber != MAIN_FORKNUM ?
+                                           "pg_tblspc/%u/%s/%u/t%d_%u_%s" : "pg_tblspc/%u/%s/%u/t%d_%u",
+                                           spcNode, version_directory, dbNode, backendId, relNode,
+                                           forkNumber != MAIN_FORKNUM ? FORK_NAMES[forkNumber] : NULL);
       }
       free(version_directory);
    }
+
+   if (!path)
path: src/libpgmoneta/walfile/relpath.c
user: login: sh-soltanpour
body: why do we need the `if` here?
created_at: 2025-03-12T23:28:42Z
author_association: COLLABORATOR
diff_hunk: @@ -37,129 +37,170 @@
  */
 
 #include <walfile/relpath.h>
-
 #include <assert.h>
 #include <utils.h>
+#include <stdlib.h>
+
+/* Constants for configuration */
+#define MAX_VERSION_DIR_SIZE 50
+#define MIN_PG_VERSION 13
+#define MAX_PG_VERSION 17
+
+/* Validation helper for fork numbers */
+static inline bool
+is_valid_fork_number(enum fork_number forkNumber)
+{
+   return forkNumber >= MAIN_FORKNUM && forkNumber <= INIT_FORKNUM;
+}
+
+/* Static array for fork names to improve performance */
+static const char* const FORK_NAMES[] = {
+   "main",                         /* MAIN_FORKNUM */
+   "fsm",                          /* FSM_FORKNUM */
+   "vm",                           /* VISIBILITYMAP_FORKNUM */
+   "init"                          /* INIT_FORKNUM */
+};
 
 char*
 pgmoneta_wal_get_relation_path(oid dbNode, oid spcNode, oid relNode,
                                int backendId, enum fork_number forkNumber)
 {
-   const char*const forkNames[] = {
-      "main",                       /* MAIN_FORKNUM */
-      "fsm",                        /* FSM_FORKNUM */
-      "vm",                      /* VISIBILITYMAP_FORKNUM */
-      "init"                        /* INIT_FORKNUM */
-   };
-   char* path;
-   path = NULL;
+   char* path = NULL;
+
+   /* Input validation */
+   if (!is_valid_fork_number(forkNumber))
+   {
+      goto error;
+   }
 
    if (spcNode == GLOBALTABLESPACE_OID)
    {
       /* Shared system relations live in {datadir}/global */
-      assert(dbNode == 0);
-      assert(backendId == INVALID_BACKEND_ID);
-      if (forkNumber != MAIN_FORKNUM)
-      {
-         path = pgmoneta_format_and_append(path, "global/%u_%s",
-                                           relNode, forkNames[forkNumber]);
-      }
-      else
+      if (dbNode != 0 || backendId != INVALID_BACKEND_ID)
       {
-         path = pgmoneta_format_and_append(path, "global/%u", relNode);
+         goto error;
       }
+
+      path = pgmoneta_format_and_append(path,
+                                        forkNumber != MAIN_FORKNUM ? "global/%u_%s" : "global/%u",
+                                        relNode, forkNumber != MAIN_FORKNUM ? FORK_NAMES[forkNumber] : NULL);
    }
    else if (spcNode == DEFAULTTABLESPACE_OID)
    {
       /* The default tablespace is {datadir}/base */
       if (backendId == INVALID_BACKEND_ID)
       {
-         if (forkNumber != MAIN_FORKNUM)
-         {
-            path = pgmoneta_format_and_append(path, "base/%u/%u_%s",
-                                              dbNode, relNode,
-                                              forkNames[forkNumber]);
-         }
-         else
-         {
-            path = pgmoneta_format_and_append(path, "base/%u/%u",
-                                              dbNode, relNode);
-         }
+         path = pgmoneta_format_and_append(path,
+                                           forkNumber != MAIN_FORKNUM ? "base/%u/%u_%s" : "base/%u/%u",
+                                           dbNode, relNode,
+                                           forkNumber != MAIN_FORKNUM ? FORK_NAMES[forkNumber] : NULL);
       }
       else
       {
-         if (forkNumber != MAIN_FORKNUM)
-         {
-            path = pgmoneta_format_and_append(path, "base/%u/t%d_%u_%s",
-                                              dbNode, backendId, relNode,
-                                              forkNames[forkNumber]);
-         }
-         else
-         {
-            path = pgmoneta_format_and_append(path, "base/%u/t%d_%u",
-                                              dbNode, backendId, relNode);
-         }
+         path = pgmoneta_format_and_append(path,
+                                           forkNumber != MAIN_FORKNUM ? "base/%u/t%d_%u_%s" : "base/%u/t%d_%u",
+                                           dbNode, backendId, relNode,
+                                           forkNumber != MAIN_FORKNUM ? FORK_NAMES[forkNumber] : NULL);
       }
    }
    else
    {
-      char* version_directory = pgmoneta_wal_get_tablespace_version_directory();
       /* All other tablespaces are accessed via symlinks */
-      if (backendId == INVALID_BACKEND_ID)
+      char* version_directory = pgmoneta_wal_get_tablespace_version_directory();
+      if (!version_directory)
       {
+         goto error;
+      }
 
-         if (forkNumber != MAIN_FORKNUM)
-         {
-            path = pgmoneta_format_and_append(path, "pg_tblspc/%u/%s/%u/%u_%s",
-                                              spcNode, version_directory,
-                                              dbNode, relNode,
-                                              forkNames[forkNumber]);
-         }
-         else
-         {
-            path = pgmoneta_format_and_append(path, "pg_tblspc/%u/%s/%u/%u",
-                                              spcNode, version_directory,
-                                              dbNode, relNode);
-         }
+      if (backendId == INVALID_BACKEND_ID)
+      {
+         path = pgmoneta_format_and_append(path,
+                                           forkNumber != MAIN_FORKNUM ?
+                                           "pg_tblspc/%u/%s/%u/%u_%s" : "pg_tblspc/%u/%s/%u/%u",
+                                           spcNode, version_directory, dbNode, relNode,
+                                           forkNumber != MAIN_FORKNUM ? FORK_NAMES[forkNumber] : NULL);
       }
       else
       {
-         if (forkNumber != MAIN_FORKNUM)
-         {
-            path = pgmoneta_format_and_append(path, "pg_tblspc/%u/%s/%u/t%d_%u_%s",
-                                              spcNode, version_directory,
-                                              dbNode, backendId, relNode,
-                                              forkNames[forkNumber]);
-         }
-         else
-         {
-            path = pgmoneta_format_and_append(path, "pg_tblspc/%u/%s/%u/t%d_%u",
-                                              spcNode, version_directory,
-                                              dbNode, backendId, relNode);
-         }
+         path = pgmoneta_format_and_append(path,
+                                           forkNumber != MAIN_FORKNUM ?
+                                           "pg_tblspc/%u/%s/%u/t%d_%u_%s" : "pg_tblspc/%u/%s/%u/t%d_%u",
+                                           spcNode, version_directory, dbNode, backendId, relNode,
+                                           forkNumber != MAIN_FORKNUM ? FORK_NAMES[forkNumber] : NULL);
       }
       free(version_directory);
    }
+
+   if (!path)
+   {
+      goto error;
+   }
    return path;
+
+error:
+   if (path)
+   {
+      free(path);
+   }
+   return NULL;
 }
 
 char*
 pgmoneta_wal_get_tablespace_version_directory(void)
 {
-   char* result = (char*)malloc(50);
-   result = pgmoneta_format_and_append(result, "PG_%d_%s", server_config->version, pgmoneta_wal_get_catalog_version_number());
+   char* result = NULL;
+   char* catalog_version = NULL;
+
+   if (!server_config)
+   {
+      goto error;
+   }
+
+   result = (char*)malloc(MAX_VERSION_DIR_SIZE);
+   if (!result)
+   {
+      goto error;
+   }
+
+   catalog_version = pgmoneta_wal_get_catalog_version_number();
path: src/libpgmoneta/walfile/relpath.c
user: login: sh-soltanpour
body: you're already handling this in `pgmoneta_wal_get_catalog_version_number`
created_at: 2025-03-12T23:29:13Z
author_association: COLLABORATOR
diff_hunk: @@ -37,129 +37,153 @@
  */
 
 #include <walfile/relpath.h>
-
 #include <assert.h>
 #include <utils.h>
+#include <stdlib.h>
+
+/* Constants for configuration */
+#define MAX_VERSION_DIR_SIZE 50
+
+/* Validation helper for fork numbers */
+static inline bool
+is_valid_fork_number(enum fork_number forkNumber)
+{
+   return forkNumber >= MAIN_FORKNUM && forkNumber <= INIT_FORKNUM;
+}
+
+/* Static array for fork names to improve performance */
+static const char* const FORK_NAMES[] = {
+   "main",                         /* MAIN_FORKNUM */
+   "fsm",                          /* FSM_FORKNUM */
+   "vm",                           /* VISIBILITYMAP_FORKNUM */
+   "init"                          /* INIT_FORKNUM */
+};
 
 char*
 pgmoneta_wal_get_relation_path(oid dbNode, oid spcNode, oid relNode,
                                int backendId, enum fork_number forkNumber)
 {
-   const char*const forkNames[] = {
-      "main",                       /* MAIN_FORKNUM */
-      "fsm",                        /* FSM_FORKNUM */
-      "vm",                      /* VISIBILITYMAP_FORKNUM */
-      "init"                        /* INIT_FORKNUM */
-   };
-   char* path;
-   path = NULL;
+   char* path = NULL;
+
+   /* Input validation */
+   if (!is_valid_fork_number(forkNumber))
+   {
+      goto error;
+   }
 
    if (spcNode == GLOBALTABLESPACE_OID)
    {
       /* Shared system relations live in {datadir}/global */
-      assert(dbNode == 0);
-      assert(backendId == INVALID_BACKEND_ID);
-      if (forkNumber != MAIN_FORKNUM)
+      if (dbNode != 0 || backendId != INVALID_BACKEND_ID)
       {
-         path = pgmoneta_format_and_append(path, "global/%u_%s",
-                                           relNode, forkNames[forkNumber]);
-      }
-      else
-      {
-         path = pgmoneta_format_and_append(path, "global/%u", relNode);
+         goto error;
       }
+
+      path = pgmoneta_format_and_append(path,
+                                        forkNumber != MAIN_FORKNUM ? "global/%u_%s" : "global/%u",
+                                        relNode, forkNumber != MAIN_FORKNUM ? FORK_NAMES[forkNumber] : NULL);
    }
    else if (spcNode == DEFAULTTABLESPACE_OID)
    {
       /* The default tablespace is {datadir}/base */
       if (backendId == INVALID_BACKEND_ID)
       {
-         if (forkNumber != MAIN_FORKNUM)
-         {
-            path = pgmoneta_format_and_append(path, "base/%u/%u_%s",
-                                              dbNode, relNode,
-                                              forkNames[forkNumber]);
-         }
-         else
-         {
-            path = pgmoneta_format_and_append(path, "base/%u/%u",
-                                              dbNode, relNode);
-         }
+         path = pgmoneta_format_and_append(path,
+                                           forkNumber != MAIN_FORKNUM ? "base/%u/%u_%s" : "base/%u/%u",
+                                           dbNode, relNode,
+                                           forkNumber != MAIN_FORKNUM ? FORK_NAMES[forkNumber] : NULL);
       }
       else
       {
-         if (forkNumber != MAIN_FORKNUM)
-         {
-            path = pgmoneta_format_and_append(path, "base/%u/t%d_%u_%s",
-                                              dbNode, backendId, relNode,
-                                              forkNames[forkNumber]);
-         }
-         else
-         {
-            path = pgmoneta_format_and_append(path, "base/%u/t%d_%u",
-                                              dbNode, backendId, relNode);
-         }
+         path = pgmoneta_format_and_append(path,
+                                           forkNumber != MAIN_FORKNUM ? "base/%u/t%d_%u_%s" : "base/%u/t%d_%u",
+                                           dbNode, backendId, relNode,
+                                           forkNumber != MAIN_FORKNUM ? FORK_NAMES[forkNumber] : NULL);
       }
    }
    else
    {
-      char* version_directory = pgmoneta_wal_get_tablespace_version_directory();
       /* All other tablespaces are accessed via symlinks */
-      if (backendId == INVALID_BACKEND_ID)
+      char* version_directory = pgmoneta_wal_get_tablespace_version_directory();
+      if (!version_directory)
       {
+         goto error;
+      }
 
-         if (forkNumber != MAIN_FORKNUM)
-         {
-            path = pgmoneta_format_and_append(path, "pg_tblspc/%u/%s/%u/%u_%s",
-                                              spcNode, version_directory,
-                                              dbNode, relNode,
-                                              forkNames[forkNumber]);
-         }
-         else
-         {
-            path = pgmoneta_format_and_append(path, "pg_tblspc/%u/%s/%u/%u",
-                                              spcNode, version_directory,
-                                              dbNode, relNode);
-         }
+      if (backendId == INVALID_BACKEND_ID)
+      {
+         path = pgmoneta_format_and_append(path,
+                                           forkNumber != MAIN_FORKNUM ?
+                                           "pg_tblspc/%u/%s/%u/%u_%s" : "pg_tblspc/%u/%s/%u/%u",
+                                           spcNode, version_directory, dbNode, relNode,
+                                           forkNumber != MAIN_FORKNUM ? FORK_NAMES[forkNumber] : NULL);
       }
       else
       {
-         if (forkNumber != MAIN_FORKNUM)
-         {
-            path = pgmoneta_format_and_append(path, "pg_tblspc/%u/%s/%u/t%d_%u_%s",
-                                              spcNode, version_directory,
-                                              dbNode, backendId, relNode,
-                                              forkNames[forkNumber]);
-         }
-         else
-         {
-            path = pgmoneta_format_and_append(path, "pg_tblspc/%u/%s/%u/t%d_%u",
-                                              spcNode, version_directory,
-                                              dbNode, backendId, relNode);
-         }
+         path = pgmoneta_format_and_append(path,
path: src/libpgmoneta/walfile/relpath.c
user: login: sh-soltanpour
body: IMO the `if` and `else` approach is easier to read. WDYT? @jesperpedersen 
created_at: 2025-03-19T23:18:59Z
author_association: COLLABORATOR
diff_hunk: @@ -37,129 +37,153 @@
  */
 
 #include <walfile/relpath.h>
-
 #include <assert.h>
 #include <utils.h>
+#include <stdlib.h>
+
+/* Constants for configuration */
+#define MAX_VERSION_DIR_SIZE 50
path: src/libpgmoneta/walfile/relpath.c
user: login: sh-soltanpour
body: Move this to `relpath.h`
created_at: 2025-03-19T23:19:40Z
author_association: COLLABORATOR
diff_hunk: @@ -37,129 +37,153 @@
  */
 
 #include <walfile/relpath.h>
-
 #include <assert.h>
 #include <utils.h>
+#include <stdlib.h>
+
+/* Constants for configuration */
+#define MAX_VERSION_DIR_SIZE 50
+
+/* Validation helper for fork numbers */
path: src/libpgmoneta/walfile/relpath.c
user: login: sh-soltanpour
body: remove unncessary comments from here and other places
created_at: 2025-03-19T23:20:04Z
author_association: COLLABORATOR
diff_hunk: @@ -37,129 +37,153 @@
  */
 
 #include <walfile/relpath.h>
-
 #include <assert.h>
 #include <utils.h>
+#include <stdlib.h>
+
+/* Constants for configuration */
+#define MAX_VERSION_DIR_SIZE 50
+
+/* Validation helper for fork numbers */
+static inline bool
+is_valid_fork_number(enum fork_number forkNumber)
+{
+   return forkNumber >= MAIN_FORKNUM && forkNumber <= INIT_FORKNUM;
+}
+
+/* Static array for fork names to improve performance */
+static const char* const FORK_NAMES[] = {
+   "main",                         /* MAIN_FORKNUM */
+   "fsm",                          /* FSM_FORKNUM */
+   "vm",                           /* VISIBILITYMAP_FORKNUM */
+   "init"                          /* INIT_FORKNUM */
+};
 
 char*
 pgmoneta_wal_get_relation_path(oid dbNode, oid spcNode, oid relNode,
                                int backendId, enum fork_number forkNumber)
 {
-   const char*const forkNames[] = {
-      "main",                       /* MAIN_FORKNUM */
-      "fsm",                        /* FSM_FORKNUM */
-      "vm",                      /* VISIBILITYMAP_FORKNUM */
-      "init"                        /* INIT_FORKNUM */
-   };
-   char* path;
-   path = NULL;
+   char* path = NULL;
+
+   /* Input validation */
+   if (!is_valid_fork_number(forkNumber))
+   {
+      goto error;
+   }
 
    if (spcNode == GLOBALTABLESPACE_OID)
    {
       /* Shared system relations live in {datadir}/global */
-      assert(dbNode == 0);
-      assert(backendId == INVALID_BACKEND_ID);
-      if (forkNumber != MAIN_FORKNUM)
+      if (dbNode != 0 || backendId != INVALID_BACKEND_ID)
       {
-         path = pgmoneta_format_and_append(path, "global/%u_%s",
-                                           relNode, forkNames[forkNumber]);
-      }
-      else
-      {
-         path = pgmoneta_format_and_append(path, "global/%u", relNode);
+         goto error;
       }
+
+      path = pgmoneta_format_and_append(path,
+                                        forkNumber != MAIN_FORKNUM ? "global/%u_%s" : "global/%u",
+                                        relNode, forkNumber != MAIN_FORKNUM ? FORK_NAMES[forkNumber] : NULL);
    }
    else if (spcNode == DEFAULTTABLESPACE_OID)
    {
       /* The default tablespace is {datadir}/base */
       if (backendId == INVALID_BACKEND_ID)
       {
-         if (forkNumber != MAIN_FORKNUM)
-         {
-            path = pgmoneta_format_and_append(path, "base/%u/%u_%s",
-                                              dbNode, relNode,
-                                              forkNames[forkNumber]);
-         }
-         else
-         {
-            path = pgmoneta_format_and_append(path, "base/%u/%u",
-                                              dbNode, relNode);
-         }
+         path = pgmoneta_format_and_append(path,
+                                           forkNumber != MAIN_FORKNUM ? "base/%u/%u_%s" : "base/%u/%u",
+                                           dbNode, relNode,
+                                           forkNumber != MAIN_FORKNUM ? FORK_NAMES[forkNumber] : NULL);
       }
       else
       {
-         if (forkNumber != MAIN_FORKNUM)
-         {
-            path = pgmoneta_format_and_append(path, "base/%u/t%d_%u_%s",
-                                              dbNode, backendId, relNode,
-                                              forkNames[forkNumber]);
-         }
-         else
-         {
-            path = pgmoneta_format_and_append(path, "base/%u/t%d_%u",
-                                              dbNode, backendId, relNode);
-         }
+         path = pgmoneta_format_and_append(path,
+                                           forkNumber != MAIN_FORKNUM ? "base/%u/t%d_%u_%s" : "base/%u/t%d_%u",
+                                           dbNode, backendId, relNode,
+                                           forkNumber != MAIN_FORKNUM ? FORK_NAMES[forkNumber] : NULL);
       }
    }
    else
    {
-      char* version_directory = pgmoneta_wal_get_tablespace_version_directory();
       /* All other tablespaces are accessed via symlinks */
-      if (backendId == INVALID_BACKEND_ID)
+      char* version_directory = pgmoneta_wal_get_tablespace_version_directory();
+      if (!version_directory)
       {
+         goto error;
+      }
 
-         if (forkNumber != MAIN_FORKNUM)
-         {
-            path = pgmoneta_format_and_append(path, "pg_tblspc/%u/%s/%u/%u_%s",
-                                              spcNode, version_directory,
-                                              dbNode, relNode,
-                                              forkNames[forkNumber]);
-         }
-         else
-         {
-            path = pgmoneta_format_and_append(path, "pg_tblspc/%u/%s/%u/%u",
-                                              spcNode, version_directory,
-                                              dbNode, relNode);
-         }
+      if (backendId == INVALID_BACKEND_ID)
+      {
+         path = pgmoneta_format_and_append(path,
+                                           forkNumber != MAIN_FORKNUM ?
+                                           "pg_tblspc/%u/%s/%u/%u_%s" : "pg_tblspc/%u/%s/%u/%u",
+                                           spcNode, version_directory, dbNode, relNode,
+                                           forkNumber != MAIN_FORKNUM ? FORK_NAMES[forkNumber] : NULL);
       }
       else
       {
-         if (forkNumber != MAIN_FORKNUM)
-         {
-            path = pgmoneta_format_and_append(path, "pg_tblspc/%u/%s/%u/t%d_%u_%s",
-                                              spcNode, version_directory,
-                                              dbNode, backendId, relNode,
-                                              forkNames[forkNumber]);
-         }
-         else
-         {
-            path = pgmoneta_format_and_append(path, "pg_tblspc/%u/%s/%u/t%d_%u",
-                                              spcNode, version_directory,
-                                              dbNode, backendId, relNode);
-         }
+         path = pgmoneta_format_and_append(path,
path: src/libpgmoneta/walfile/relpath.c
user: login: jesperpedersen
body: Agreed
created_at: 2025-03-19T23:21:15Z
author_association: MEMBER
diff_hunk: @@ -37,129 +37,153 @@
  */
 
 #include <walfile/relpath.h>
-
 #include <assert.h>
 #include <utils.h>
+#include <stdlib.h>
+
+/* Constants for configuration */
+#define MAX_VERSION_DIR_SIZE 50
+
+/* Validation helper for fork numbers */
+static inline bool
+is_valid_fork_number(enum fork_number forkNumber)
+{
+   return forkNumber >= MAIN_FORKNUM && forkNumber <= INIT_FORKNUM;
+}
+
+/* Static array for fork names to improve performance */
+static const char* const FORK_NAMES[] = {
+   "main",                         /* MAIN_FORKNUM */
+   "fsm",                          /* FSM_FORKNUM */
+   "vm",                           /* VISIBILITYMAP_FORKNUM */
+   "init"                          /* INIT_FORKNUM */
+};
 
 char*
 pgmoneta_wal_get_relation_path(oid dbNode, oid spcNode, oid relNode,
                                int backendId, enum fork_number forkNumber)
 {
-   const char*const forkNames[] = {
-      "main",                       /* MAIN_FORKNUM */
-      "fsm",                        /* FSM_FORKNUM */
-      "vm",                      /* VISIBILITYMAP_FORKNUM */
-      "init"                        /* INIT_FORKNUM */
-   };
-   char* path;
-   path = NULL;
+   char* path = NULL;
+
+   /* Input validation */
+   if (!is_valid_fork_number(forkNumber))
+   {
+      goto error;
+   }
 
    if (spcNode == GLOBALTABLESPACE_OID)
    {
       /* Shared system relations live in {datadir}/global */
-      assert(dbNode == 0);
-      assert(backendId == INVALID_BACKEND_ID);
-      if (forkNumber != MAIN_FORKNUM)
+      if (dbNode != 0 || backendId != INVALID_BACKEND_ID)
       {
-         path = pgmoneta_format_and_append(path, "global/%u_%s",
-                                           relNode, forkNames[forkNumber]);
-      }
-      else
-      {
-         path = pgmoneta_format_and_append(path, "global/%u", relNode);
+         goto error;
       }
+
+      path = pgmoneta_format_and_append(path,
+                                        forkNumber != MAIN_FORKNUM ? "global/%u_%s" : "global/%u",
+                                        relNode, forkNumber != MAIN_FORKNUM ? FORK_NAMES[forkNumber] : NULL);
    }
    else if (spcNode == DEFAULTTABLESPACE_OID)
    {
       /* The default tablespace is {datadir}/base */
       if (backendId == INVALID_BACKEND_ID)
       {
-         if (forkNumber != MAIN_FORKNUM)
-         {
-            path = pgmoneta_format_and_append(path, "base/%u/%u_%s",
-                                              dbNode, relNode,
-                                              forkNames[forkNumber]);
-         }
-         else
-         {
-            path = pgmoneta_format_and_append(path, "base/%u/%u",
-                                              dbNode, relNode);
-         }
+         path = pgmoneta_format_and_append(path,
+                                           forkNumber != MAIN_FORKNUM ? "base/%u/%u_%s" : "base/%u/%u",
+                                           dbNode, relNode,
+                                           forkNumber != MAIN_FORKNUM ? FORK_NAMES[forkNumber] : NULL);
       }
       else
       {
-         if (forkNumber != MAIN_FORKNUM)
-         {
-            path = pgmoneta_format_and_append(path, "base/%u/t%d_%u_%s",
-                                              dbNode, backendId, relNode,
-                                              forkNames[forkNumber]);
-         }
-         else
-         {
-            path = pgmoneta_format_and_append(path, "base/%u/t%d_%u",
-                                              dbNode, backendId, relNode);
-         }
+         path = pgmoneta_format_and_append(path,
+                                           forkNumber != MAIN_FORKNUM ? "base/%u/t%d_%u_%s" : "base/%u/t%d_%u",
+                                           dbNode, backendId, relNode,
+                                           forkNumber != MAIN_FORKNUM ? FORK_NAMES[forkNumber] : NULL);
       }
    }
    else
    {
-      char* version_directory = pgmoneta_wal_get_tablespace_version_directory();
       /* All other tablespaces are accessed via symlinks */
-      if (backendId == INVALID_BACKEND_ID)
+      char* version_directory = pgmoneta_wal_get_tablespace_version_directory();
+      if (!version_directory)
       {
+         goto error;
+      }
 
-         if (forkNumber != MAIN_FORKNUM)
-         {
-            path = pgmoneta_format_and_append(path, "pg_tblspc/%u/%s/%u/%u_%s",
-                                              spcNode, version_directory,
-                                              dbNode, relNode,
-                                              forkNames[forkNumber]);
-         }
-         else
-         {
-            path = pgmoneta_format_and_append(path, "pg_tblspc/%u/%s/%u/%u",
-                                              spcNode, version_directory,
-                                              dbNode, relNode);
-         }
+      if (backendId == INVALID_BACKEND_ID)
+      {
+         path = pgmoneta_format_and_append(path,
+                                           forkNumber != MAIN_FORKNUM ?
+                                           "pg_tblspc/%u/%s/%u/%u_%s" : "pg_tblspc/%u/%s/%u/%u",
+                                           spcNode, version_directory, dbNode, relNode,
+                                           forkNumber != MAIN_FORKNUM ? FORK_NAMES[forkNumber] : NULL);
       }
       else
       {
-         if (forkNumber != MAIN_FORKNUM)
-         {
-            path = pgmoneta_format_and_append(path, "pg_tblspc/%u/%s/%u/t%d_%u_%s",
-                                              spcNode, version_directory,
-                                              dbNode, backendId, relNode,
-                                              forkNames[forkNumber]);
-         }
-         else
-         {
-            path = pgmoneta_format_and_append(path, "pg_tblspc/%u/%s/%u/t%d_%u",
-                                              spcNode, version_directory,
-                                              dbNode, backendId, relNode);
-         }
+         path = pgmoneta_format_and_append(path,
path: src/libpgmoneta/walfile/relpath.c
user: login: 7amo10
body: You both agreed that the `if` and `else` approach is easier to read. Just to confirm, are you suggesting restructuring the function to improve readability while keeping the same logic? Or do you mean that we should avoid refactoring it further since it's already structured with `if-else`? Let me know how you'd like me to proceed!
created_at: 2025-03-30T12:08:38Z
author_association: CONTRIBUTOR
diff_hunk: @@ -37,129 +37,153 @@
  */
 
 #include <walfile/relpath.h>
-
 #include <assert.h>
 #include <utils.h>
+#include <stdlib.h>
+
+/* Constants for configuration */
+#define MAX_VERSION_DIR_SIZE 50
+
+/* Validation helper for fork numbers */
+static inline bool
+is_valid_fork_number(enum fork_number forkNumber)
+{
+   return forkNumber >= MAIN_FORKNUM && forkNumber <= INIT_FORKNUM;
+}
+
+/* Static array for fork names to improve performance */
+static const char* const FORK_NAMES[] = {
+   "main",                         /* MAIN_FORKNUM */
+   "fsm",                          /* FSM_FORKNUM */
+   "vm",                           /* VISIBILITYMAP_FORKNUM */
+   "init"                          /* INIT_FORKNUM */
+};
 
 char*
 pgmoneta_wal_get_relation_path(oid dbNode, oid spcNode, oid relNode,
                                int backendId, enum fork_number forkNumber)
 {
-   const char*const forkNames[] = {
-      "main",                       /* MAIN_FORKNUM */
-      "fsm",                        /* FSM_FORKNUM */
-      "vm",                      /* VISIBILITYMAP_FORKNUM */
-      "init"                        /* INIT_FORKNUM */
-   };
-   char* path;
-   path = NULL;
+   char* path = NULL;
+
+   /* Input validation */
+   if (!is_valid_fork_number(forkNumber))
+   {
+      goto error;
+   }
 
    if (spcNode == GLOBALTABLESPACE_OID)
    {
       /* Shared system relations live in {datadir}/global */
-      assert(dbNode == 0);
-      assert(backendId == INVALID_BACKEND_ID);
-      if (forkNumber != MAIN_FORKNUM)
+      if (dbNode != 0 || backendId != INVALID_BACKEND_ID)
       {
-         path = pgmoneta_format_and_append(path, "global/%u_%s",
-                                           relNode, forkNames[forkNumber]);
-      }
-      else
-      {
-         path = pgmoneta_format_and_append(path, "global/%u", relNode);
+         goto error;
       }
+
+      path = pgmoneta_format_and_append(path,
+                                        forkNumber != MAIN_FORKNUM ? "global/%u_%s" : "global/%u",
+                                        relNode, forkNumber != MAIN_FORKNUM ? FORK_NAMES[forkNumber] : NULL);
    }
    else if (spcNode == DEFAULTTABLESPACE_OID)
    {
       /* The default tablespace is {datadir}/base */
       if (backendId == INVALID_BACKEND_ID)
       {
-         if (forkNumber != MAIN_FORKNUM)
-         {
-            path = pgmoneta_format_and_append(path, "base/%u/%u_%s",
-                                              dbNode, relNode,
-                                              forkNames[forkNumber]);
-         }
-         else
-         {
-            path = pgmoneta_format_and_append(path, "base/%u/%u",
-                                              dbNode, relNode);
-         }
+         path = pgmoneta_format_and_append(path,
+                                           forkNumber != MAIN_FORKNUM ? "base/%u/%u_%s" : "base/%u/%u",
+                                           dbNode, relNode,
+                                           forkNumber != MAIN_FORKNUM ? FORK_NAMES[forkNumber] : NULL);
       }
       else
       {
-         if (forkNumber != MAIN_FORKNUM)
-         {
-            path = pgmoneta_format_and_append(path, "base/%u/t%d_%u_%s",
-                                              dbNode, backendId, relNode,
-                                              forkNames[forkNumber]);
-         }
-         else
-         {
-            path = pgmoneta_format_and_append(path, "base/%u/t%d_%u",
-                                              dbNode, backendId, relNode);
-         }
+         path = pgmoneta_format_and_append(path,
+                                           forkNumber != MAIN_FORKNUM ? "base/%u/t%d_%u_%s" : "base/%u/t%d_%u",
+                                           dbNode, backendId, relNode,
+                                           forkNumber != MAIN_FORKNUM ? FORK_NAMES[forkNumber] : NULL);
       }
    }
    else
    {
-      char* version_directory = pgmoneta_wal_get_tablespace_version_directory();
       /* All other tablespaces are accessed via symlinks */
-      if (backendId == INVALID_BACKEND_ID)
+      char* version_directory = pgmoneta_wal_get_tablespace_version_directory();
+      if (!version_directory)
       {
+         goto error;
+      }
 
-         if (forkNumber != MAIN_FORKNUM)
-         {
-            path = pgmoneta_format_and_append(path, "pg_tblspc/%u/%s/%u/%u_%s",
-                                              spcNode, version_directory,
-                                              dbNode, relNode,
-                                              forkNames[forkNumber]);
-         }
-         else
-         {
-            path = pgmoneta_format_and_append(path, "pg_tblspc/%u/%s/%u/%u",
-                                              spcNode, version_directory,
-                                              dbNode, relNode);
-         }
+      if (backendId == INVALID_BACKEND_ID)
+      {
+         path = pgmoneta_format_and_append(path,
+                                           forkNumber != MAIN_FORKNUM ?
+                                           "pg_tblspc/%u/%s/%u/%u_%s" : "pg_tblspc/%u/%s/%u/%u",
+                                           spcNode, version_directory, dbNode, relNode,
+                                           forkNumber != MAIN_FORKNUM ? FORK_NAMES[forkNumber] : NULL);
       }
       else
       {
-         if (forkNumber != MAIN_FORKNUM)
-         {
-            path = pgmoneta_format_and_append(path, "pg_tblspc/%u/%s/%u/t%d_%u_%s",
-                                              spcNode, version_directory,
-                                              dbNode, backendId, relNode,
-                                              forkNames[forkNumber]);
-         }
-         else
-         {
-            path = pgmoneta_format_and_append(path, "pg_tblspc/%u/%s/%u/t%d_%u",
-                                              spcNode, version_directory,
-                                              dbNode, backendId, relNode);
-         }
+         path = pgmoneta_format_and_append(path,
path: src/libpgmoneta/walfile/relpath.c
user: login: jesperpedersen
body: Yes, keep the `if-else`
created_at: 2025-03-30T12:11:25Z
author_association: MEMBER
diff_hunk: @@ -37,129 +37,143 @@
  */
 
 #include <walfile/relpath.h>
-
 #include <assert.h>
 #include <utils.h>
+#include <stdlib.h>
+
+static inline bool
+is_valid_fork_number(enum fork_number forkNumber)
+{
+   return forkNumber >= MAIN_FORKNUM && forkNumber <= INIT_FORKNUM;
+}
+
+static const char* const FORK_NAMES[] = {
path: src/libpgmoneta/walfile/relpath.c
user: login: jesperpedersen
body: Move `{` to newline
created_at: 2025-03-30T12:28:04Z
author_association: MEMBER
diff_hunk: @@ -37,129 +37,143 @@
  */
 
 #include <walfile/relpath.h>
-
 #include <assert.h>
 #include <utils.h>
+#include <stdlib.h>
+
+static inline bool
+is_valid_fork_number(enum fork_number forkNumber)
+{
+   return forkNumber >= MAIN_FORKNUM && forkNumber <= INIT_FORKNUM;
+}
+
+static const char* const FORK_NAMES[] = {
+   "main",
+   "fsm",
+   "vm",
+   "init"
+};
 
 char*
 pgmoneta_wal_get_relation_path(oid dbNode, oid spcNode, oid relNode,
                                int backendId, enum fork_number forkNumber)
 {
-   char* forkNames[] = {
-      "main",                       /* MAIN_FORKNUM */
-      "fsm",                        /* FSM_FORKNUM */
-      "vm",                      /* VISIBILITYMAP_FORKNUM */
-      "init"                        /* INIT_FORKNUM */
-   };
-   char* path;
-   path = NULL;
+   char* path = NULL;
+
+   if (!is_valid_fork_number(forkNumber))
+   {
+      goto error;
+   }
 
    if (spcNode == GLOBALTABLESPACE_OID)
    {
-      /* Shared system relations live in {datadir}/global */
-      assert(dbNode == 0);
-      assert(backendId == INVALID_BACKEND_ID);
-      if (forkNumber != MAIN_FORKNUM)
-      {
-         path = pgmoneta_format_and_append(path, "global/%u_%s",
-                                           relNode, forkNames[forkNumber]);
-      }
-      else
+      if (dbNode != 0 || backendId != INVALID_BACKEND_ID)
       {
-         path = pgmoneta_format_and_append(path, "global/%u", relNode);
+         goto error;
       }
+
+      path = pgmoneta_format_and_append(path,
path: src/libpgmoneta/walfile/relpath.c
user: login: jesperpedersen
body: Do an `if`
created_at: 2025-03-30T12:28:40Z
author_association: MEMBER
diff_hunk: @@ -37,129 +37,143 @@
  */
 
 #include <walfile/relpath.h>
-
 #include <assert.h>
 #include <utils.h>
+#include <stdlib.h>
+
+static inline bool
+is_valid_fork_number(enum fork_number forkNumber)
+{
+   return forkNumber >= MAIN_FORKNUM && forkNumber <= INIT_FORKNUM;
+}
+
+static const char* const FORK_NAMES[] = {
+   "main",
+   "fsm",
+   "vm",
+   "init"
+};
 
 char*
 pgmoneta_wal_get_relation_path(oid dbNode, oid spcNode, oid relNode,
                                int backendId, enum fork_number forkNumber)
 {
-   char* forkNames[] = {
-      "main",                       /* MAIN_FORKNUM */
-      "fsm",                        /* FSM_FORKNUM */
-      "vm",                      /* VISIBILITYMAP_FORKNUM */
-      "init"                        /* INIT_FORKNUM */
-   };
-   char* path;
-   path = NULL;
+   char* path = NULL;
+
+   if (!is_valid_fork_number(forkNumber))
+   {
+      goto error;
+   }
 
    if (spcNode == GLOBALTABLESPACE_OID)
    {
-      /* Shared system relations live in {datadir}/global */
-      assert(dbNode == 0);
-      assert(backendId == INVALID_BACKEND_ID);
-      if (forkNumber != MAIN_FORKNUM)
-      {
-         path = pgmoneta_format_and_append(path, "global/%u_%s",
-                                           relNode, forkNames[forkNumber]);
-      }
-      else
+      if (dbNode != 0 || backendId != INVALID_BACKEND_ID)
       {
-         path = pgmoneta_format_and_append(path, "global/%u", relNode);
+         goto error;
       }
+
+      path = pgmoneta_format_and_append(path,
+                                        forkNumber != MAIN_FORKNUM ? "global/%u_%s" : "global/%u",
+                                        relNode, forkNumber != MAIN_FORKNUM ? FORK_NAMES[forkNumber] : NULL);
    }
    else if (spcNode == DEFAULTTABLESPACE_OID)
    {
-      /* The default tablespace is {datadir}/base */
       if (backendId == INVALID_BACKEND_ID)
       {
-         if (forkNumber != MAIN_FORKNUM)
-         {
-            path = pgmoneta_format_and_append(path, "base/%u/%u_%s",
-                                              dbNode, relNode,
-                                              forkNames[forkNumber]);
-         }
-         else
-         {
-            path = pgmoneta_format_and_append(path, "base/%u/%u",
-                                              dbNode, relNode);
-         }
+         path = pgmoneta_format_and_append(path,
path: src/libpgmoneta/walfile/relpath.c
user: login: jesperpedersen
body: Keep the `if`
created_at: 2025-03-30T12:28:53Z
author_association: MEMBER
diff_hunk: @@ -37,129 +37,143 @@
  */
 
 #include <walfile/relpath.h>
-
 #include <assert.h>
 #include <utils.h>
+#include <stdlib.h>
+
+static inline bool
+is_valid_fork_number(enum fork_number forkNumber)
+{
+   return forkNumber >= MAIN_FORKNUM && forkNumber <= INIT_FORKNUM;
+}
+
+static const char* const FORK_NAMES[] = {
+   "main",
+   "fsm",
+   "vm",
+   "init"
+};
 
 char*
 pgmoneta_wal_get_relation_path(oid dbNode, oid spcNode, oid relNode,
                                int backendId, enum fork_number forkNumber)
 {
-   char* forkNames[] = {
-      "main",                       /* MAIN_FORKNUM */
-      "fsm",                        /* FSM_FORKNUM */
-      "vm",                      /* VISIBILITYMAP_FORKNUM */
-      "init"                        /* INIT_FORKNUM */
-   };
-   char* path;
-   path = NULL;
+   char* path = NULL;
+
+   if (!is_valid_fork_number(forkNumber))
+   {
+      goto error;
+   }
 
    if (spcNode == GLOBALTABLESPACE_OID)
    {
-      /* Shared system relations live in {datadir}/global */
-      assert(dbNode == 0);
-      assert(backendId == INVALID_BACKEND_ID);
-      if (forkNumber != MAIN_FORKNUM)
-      {
-         path = pgmoneta_format_and_append(path, "global/%u_%s",
-                                           relNode, forkNames[forkNumber]);
-      }
-      else
+      if (dbNode != 0 || backendId != INVALID_BACKEND_ID)
       {
-         path = pgmoneta_format_and_append(path, "global/%u", relNode);
+         goto error;
       }
+
+      path = pgmoneta_format_and_append(path,
+                                        forkNumber != MAIN_FORKNUM ? "global/%u_%s" : "global/%u",
+                                        relNode, forkNumber != MAIN_FORKNUM ? FORK_NAMES[forkNumber] : NULL);
    }
    else if (spcNode == DEFAULTTABLESPACE_OID)
    {
-      /* The default tablespace is {datadir}/base */
       if (backendId == INVALID_BACKEND_ID)
       {
-         if (forkNumber != MAIN_FORKNUM)
-         {
-            path = pgmoneta_format_and_append(path, "base/%u/%u_%s",
-                                              dbNode, relNode,
-                                              forkNames[forkNumber]);
-         }
-         else
-         {
-            path = pgmoneta_format_and_append(path, "base/%u/%u",
-                                              dbNode, relNode);
-         }
+         path = pgmoneta_format_and_append(path,
+                                           forkNumber != MAIN_FORKNUM ? "base/%u/%u_%s" : "base/%u/%u",
+                                           dbNode, relNode,
+                                           forkNumber != MAIN_FORKNUM ? FORK_NAMES[forkNumber] : NULL);
       }
       else
       {
-         if (forkNumber != MAIN_FORKNUM)
-         {
-            path = pgmoneta_format_and_append(path, "base/%u/t%d_%u_%s",
-                                              dbNode, backendId, relNode,
-                                              forkNames[forkNumber]);
-         }
-         else
-         {
-            path = pgmoneta_format_and_append(path, "base/%u/t%d_%u",
-                                              dbNode, backendId, relNode);
-         }
+         path = pgmoneta_format_and_append(path,
path: src/libpgmoneta/walfile/relpath.c
user: login: jesperpedersen
body: Keep the `if`
created_at: 2025-03-30T12:29:04Z
author_association: MEMBER
diff_hunk: @@ -37,129 +37,153 @@
  */
 
 #include <walfile/relpath.h>
-
 #include <assert.h>
 #include <utils.h>
+#include <stdlib.h>
+
+/* Constants for configuration */
+#define MAX_VERSION_DIR_SIZE 50
+
+/* Validation helper for fork numbers */
+static inline bool
+is_valid_fork_number(enum fork_number forkNumber)
+{
+   return forkNumber >= MAIN_FORKNUM && forkNumber <= INIT_FORKNUM;
+}
+
+/* Static array for fork names to improve performance */
+static const char* const FORK_NAMES[] = {
+   "main",                         /* MAIN_FORKNUM */
+   "fsm",                          /* FSM_FORKNUM */
+   "vm",                           /* VISIBILITYMAP_FORKNUM */
+   "init"                          /* INIT_FORKNUM */
+};
 
 char*
 pgmoneta_wal_get_relation_path(oid dbNode, oid spcNode, oid relNode,
                                int backendId, enum fork_number forkNumber)
 {
-   const char*const forkNames[] = {
-      "main",                       /* MAIN_FORKNUM */
-      "fsm",                        /* FSM_FORKNUM */
-      "vm",                      /* VISIBILITYMAP_FORKNUM */
-      "init"                        /* INIT_FORKNUM */
-   };
-   char* path;
-   path = NULL;
+   char* path = NULL;
+
+   /* Input validation */
+   if (!is_valid_fork_number(forkNumber))
+   {
+      goto error;
+   }
 
    if (spcNode == GLOBALTABLESPACE_OID)
    {
       /* Shared system relations live in {datadir}/global */
-      assert(dbNode == 0);
-      assert(backendId == INVALID_BACKEND_ID);
-      if (forkNumber != MAIN_FORKNUM)
+      if (dbNode != 0 || backendId != INVALID_BACKEND_ID)
       {
-         path = pgmoneta_format_and_append(path, "global/%u_%s",
-                                           relNode, forkNames[forkNumber]);
-      }
-      else
-      {
-         path = pgmoneta_format_and_append(path, "global/%u", relNode);
+         goto error;
       }
+
+      path = pgmoneta_format_and_append(path,
+                                        forkNumber != MAIN_FORKNUM ? "global/%u_%s" : "global/%u",
+                                        relNode, forkNumber != MAIN_FORKNUM ? FORK_NAMES[forkNumber] : NULL);
    }
    else if (spcNode == DEFAULTTABLESPACE_OID)
    {
       /* The default tablespace is {datadir}/base */
       if (backendId == INVALID_BACKEND_ID)
       {
-         if (forkNumber != MAIN_FORKNUM)
-         {
-            path = pgmoneta_format_and_append(path, "base/%u/%u_%s",
-                                              dbNode, relNode,
-                                              forkNames[forkNumber]);
-         }
-         else
-         {
-            path = pgmoneta_format_and_append(path, "base/%u/%u",
-                                              dbNode, relNode);
-         }
+         path = pgmoneta_format_and_append(path,
+                                           forkNumber != MAIN_FORKNUM ? "base/%u/%u_%s" : "base/%u/%u",
+                                           dbNode, relNode,
+                                           forkNumber != MAIN_FORKNUM ? FORK_NAMES[forkNumber] : NULL);
       }
       else
       {
-         if (forkNumber != MAIN_FORKNUM)
-         {
-            path = pgmoneta_format_and_append(path, "base/%u/t%d_%u_%s",
-                                              dbNode, backendId, relNode,
-                                              forkNames[forkNumber]);
-         }
-         else
-         {
-            path = pgmoneta_format_and_append(path, "base/%u/t%d_%u",
-                                              dbNode, backendId, relNode);
-         }
+         path = pgmoneta_format_and_append(path,
+                                           forkNumber != MAIN_FORKNUM ? "base/%u/t%d_%u_%s" : "base/%u/t%d_%u",
+                                           dbNode, backendId, relNode,
+                                           forkNumber != MAIN_FORKNUM ? FORK_NAMES[forkNumber] : NULL);
       }
    }
    else
    {
-      char* version_directory = pgmoneta_wal_get_tablespace_version_directory();
       /* All other tablespaces are accessed via symlinks */
-      if (backendId == INVALID_BACKEND_ID)
+      char* version_directory = pgmoneta_wal_get_tablespace_version_directory();
+      if (!version_directory)
       {
+         goto error;
+      }
 
-         if (forkNumber != MAIN_FORKNUM)
-         {
-            path = pgmoneta_format_and_append(path, "pg_tblspc/%u/%s/%u/%u_%s",
-                                              spcNode, version_directory,
-                                              dbNode, relNode,
-                                              forkNames[forkNumber]);
-         }
-         else
-         {
-            path = pgmoneta_format_and_append(path, "pg_tblspc/%u/%s/%u/%u",
-                                              spcNode, version_directory,
-                                              dbNode, relNode);
-         }
+      if (backendId == INVALID_BACKEND_ID)
+      {
+         path = pgmoneta_format_and_append(path,
+                                           forkNumber != MAIN_FORKNUM ?
+                                           "pg_tblspc/%u/%s/%u/%u_%s" : "pg_tblspc/%u/%s/%u/%u",
+                                           spcNode, version_directory, dbNode, relNode,
+                                           forkNumber != MAIN_FORKNUM ? FORK_NAMES[forkNumber] : NULL);
       }
       else
       {
-         if (forkNumber != MAIN_FORKNUM)
-         {
-            path = pgmoneta_format_and_append(path, "pg_tblspc/%u/%s/%u/t%d_%u_%s",
-                                              spcNode, version_directory,
-                                              dbNode, backendId, relNode,
-                                              forkNames[forkNumber]);
-         }
-         else
-         {
-            path = pgmoneta_format_and_append(path, "pg_tblspc/%u/%s/%u/t%d_%u",
-                                              spcNode, version_directory,
-                                              dbNode, backendId, relNode);
-         }
+         path = pgmoneta_format_and_append(path,
path: src/libpgmoneta/walfile/relpath.c
user: login: jesperpedersen
body: Keep the `if`
created_at: 2025-03-30T12:29:25Z
author_association: MEMBER
diff_hunk: @@ -37,129 +37,143 @@
  */
 
 #include <walfile/relpath.h>
-
 #include <assert.h>
 #include <utils.h>
+#include <stdlib.h>
+
+static inline bool
+is_valid_fork_number(enum fork_number forkNumber)
+{
+   return forkNumber >= MAIN_FORKNUM && forkNumber <= INIT_FORKNUM;
+}
+
+static const char* const FORK_NAMES[] = {
+   "main",
+   "fsm",
+   "vm",
+   "init"
+};
 
 char*
 pgmoneta_wal_get_relation_path(oid dbNode, oid spcNode, oid relNode,
                                int backendId, enum fork_number forkNumber)
 {
-   char* forkNames[] = {
-      "main",                       /* MAIN_FORKNUM */
-      "fsm",                        /* FSM_FORKNUM */
-      "vm",                      /* VISIBILITYMAP_FORKNUM */
-      "init"                        /* INIT_FORKNUM */
-   };
-   char* path;
-   path = NULL;
+   char* path = NULL;
+
+   if (!is_valid_fork_number(forkNumber))
+   {
+      goto error;
+   }
 
    if (spcNode == GLOBALTABLESPACE_OID)
    {
-      /* Shared system relations live in {datadir}/global */
-      assert(dbNode == 0);
-      assert(backendId == INVALID_BACKEND_ID);
-      if (forkNumber != MAIN_FORKNUM)
-      {
-         path = pgmoneta_format_and_append(path, "global/%u_%s",
-                                           relNode, forkNames[forkNumber]);
-      }
-      else
+      if (dbNode != 0 || backendId != INVALID_BACKEND_ID)
       {
-         path = pgmoneta_format_and_append(path, "global/%u", relNode);
+         goto error;
       }
+
+      path = pgmoneta_format_and_append(path,
+                                        forkNumber != MAIN_FORKNUM ? "global/%u_%s" : "global/%u",
+                                        relNode, forkNumber != MAIN_FORKNUM ? FORK_NAMES[forkNumber] : NULL);
    }
    else if (spcNode == DEFAULTTABLESPACE_OID)
    {
-      /* The default tablespace is {datadir}/base */
       if (backendId == INVALID_BACKEND_ID)
       {
-         if (forkNumber != MAIN_FORKNUM)
-         {
-            path = pgmoneta_format_and_append(path, "base/%u/%u_%s",
-                                              dbNode, relNode,
-                                              forkNames[forkNumber]);
-         }
-         else
-         {
-            path = pgmoneta_format_and_append(path, "base/%u/%u",
-                                              dbNode, relNode);
-         }
+         path = pgmoneta_format_and_append(path,
path: src/libpgmoneta/walfile/relpath.c
user: login: jesperpedersen
body: ?
created_at: 2025-03-30T14:13:13Z
author_association: MEMBER
diff_hunk: @@ -37,129 +37,143 @@
  */
 
 #include <walfile/relpath.h>
-
 #include <assert.h>
 #include <utils.h>
+#include <stdlib.h>
+
+static inline bool
+is_valid_fork_number(enum fork_number forkNumber)
+{
+   return forkNumber >= MAIN_FORKNUM && forkNumber <= INIT_FORKNUM;
+}
+
+static const char* const FORK_NAMES[] = {
+   "main",
+   "fsm",
+   "vm",
+   "init"
+};
 
 char*
 pgmoneta_wal_get_relation_path(oid dbNode, oid spcNode, oid relNode,
                                int backendId, enum fork_number forkNumber)
 {
-   char* forkNames[] = {
-      "main",                       /* MAIN_FORKNUM */
-      "fsm",                        /* FSM_FORKNUM */
-      "vm",                      /* VISIBILITYMAP_FORKNUM */
-      "init"                        /* INIT_FORKNUM */
-   };
-   char* path;
-   path = NULL;
+   char* path = NULL;
+
+   if (!is_valid_fork_number(forkNumber))
+   {
+      goto error;
+   }
 
    if (spcNode == GLOBALTABLESPACE_OID)
    {
-      /* Shared system relations live in {datadir}/global */
-      assert(dbNode == 0);
-      assert(backendId == INVALID_BACKEND_ID);
-      if (forkNumber != MAIN_FORKNUM)
-      {
-         path = pgmoneta_format_and_append(path, "global/%u_%s",
-                                           relNode, forkNames[forkNumber]);
-      }
-      else
+      if (dbNode != 0 || backendId != INVALID_BACKEND_ID)
       {
-         path = pgmoneta_format_and_append(path, "global/%u", relNode);
+         goto error;
       }
+
+      path = pgmoneta_format_and_append(path,
+                                        forkNumber != MAIN_FORKNUM ? "global/%u_%s" : "global/%u",
+                                        relNode, forkNumber != MAIN_FORKNUM ? FORK_NAMES[forkNumber] : NULL);
    }
    else if (spcNode == DEFAULTTABLESPACE_OID)
    {
-      /* The default tablespace is {datadir}/base */
       if (backendId == INVALID_BACKEND_ID)
       {
-         if (forkNumber != MAIN_FORKNUM)
-         {
-            path = pgmoneta_format_and_append(path, "base/%u/%u_%s",
-                                              dbNode, relNode,
-                                              forkNames[forkNumber]);
-         }
-         else
-         {
-            path = pgmoneta_format_and_append(path, "base/%u/%u",
-                                              dbNode, relNode);
-         }
+         path = pgmoneta_format_and_append(path,
+                                           forkNumber != MAIN_FORKNUM ? "base/%u/%u_%s" : "base/%u/%u",
+                                           dbNode, relNode,
+                                           forkNumber != MAIN_FORKNUM ? FORK_NAMES[forkNumber] : NULL);
       }
       else
       {
-         if (forkNumber != MAIN_FORKNUM)
-         {
-            path = pgmoneta_format_and_append(path, "base/%u/t%d_%u_%s",
-                                              dbNode, backendId, relNode,
-                                              forkNames[forkNumber]);
-         }
-         else
-         {
-            path = pgmoneta_format_and_append(path, "base/%u/t%d_%u",
-                                              dbNode, backendId, relNode);
-         }
+         path = pgmoneta_format_and_append(path,
path: src/libpgmoneta/walfile/relpath.c
user: login: jesperpedersen
body: ?

created_at: 2025-03-30T14:13:28Z
author_association: MEMBER
diff_hunk: @@ -37,129 +37,153 @@
  */
 
 #include <walfile/relpath.h>
-
 #include <assert.h>
 #include <utils.h>
+#include <stdlib.h>
+
+/* Constants for configuration */
+#define MAX_VERSION_DIR_SIZE 50
+
+/* Validation helper for fork numbers */
+static inline bool
+is_valid_fork_number(enum fork_number forkNumber)
+{
+   return forkNumber >= MAIN_FORKNUM && forkNumber <= INIT_FORKNUM;
+}
+
+/* Static array for fork names to improve performance */
+static const char* const FORK_NAMES[] = {
+   "main",                         /* MAIN_FORKNUM */
+   "fsm",                          /* FSM_FORKNUM */
+   "vm",                           /* VISIBILITYMAP_FORKNUM */
+   "init"                          /* INIT_FORKNUM */
+};
 
 char*
 pgmoneta_wal_get_relation_path(oid dbNode, oid spcNode, oid relNode,
                                int backendId, enum fork_number forkNumber)
 {
-   const char*const forkNames[] = {
-      "main",                       /* MAIN_FORKNUM */
-      "fsm",                        /* FSM_FORKNUM */
-      "vm",                      /* VISIBILITYMAP_FORKNUM */
-      "init"                        /* INIT_FORKNUM */
-   };
-   char* path;
-   path = NULL;
+   char* path = NULL;
+
+   /* Input validation */
+   if (!is_valid_fork_number(forkNumber))
+   {
+      goto error;
+   }
 
    if (spcNode == GLOBALTABLESPACE_OID)
    {
       /* Shared system relations live in {datadir}/global */
-      assert(dbNode == 0);
-      assert(backendId == INVALID_BACKEND_ID);
-      if (forkNumber != MAIN_FORKNUM)
+      if (dbNode != 0 || backendId != INVALID_BACKEND_ID)
       {
-         path = pgmoneta_format_and_append(path, "global/%u_%s",
-                                           relNode, forkNames[forkNumber]);
-      }
-      else
-      {
-         path = pgmoneta_format_and_append(path, "global/%u", relNode);
+         goto error;
       }
+
+      path = pgmoneta_format_and_append(path,
+                                        forkNumber != MAIN_FORKNUM ? "global/%u_%s" : "global/%u",
+                                        relNode, forkNumber != MAIN_FORKNUM ? FORK_NAMES[forkNumber] : NULL);
    }
    else if (spcNode == DEFAULTTABLESPACE_OID)
    {
       /* The default tablespace is {datadir}/base */
       if (backendId == INVALID_BACKEND_ID)
       {
-         if (forkNumber != MAIN_FORKNUM)
-         {
-            path = pgmoneta_format_and_append(path, "base/%u/%u_%s",
-                                              dbNode, relNode,
-                                              forkNames[forkNumber]);
-         }
-         else
-         {
-            path = pgmoneta_format_and_append(path, "base/%u/%u",
-                                              dbNode, relNode);
-         }
+         path = pgmoneta_format_and_append(path,
+                                           forkNumber != MAIN_FORKNUM ? "base/%u/%u_%s" : "base/%u/%u",
+                                           dbNode, relNode,
+                                           forkNumber != MAIN_FORKNUM ? FORK_NAMES[forkNumber] : NULL);
       }
       else
       {
-         if (forkNumber != MAIN_FORKNUM)
-         {
-            path = pgmoneta_format_and_append(path, "base/%u/t%d_%u_%s",
-                                              dbNode, backendId, relNode,
-                                              forkNames[forkNumber]);
-         }
-         else
-         {
-            path = pgmoneta_format_and_append(path, "base/%u/t%d_%u",
-                                              dbNode, backendId, relNode);
-         }
+         path = pgmoneta_format_and_append(path,
+                                           forkNumber != MAIN_FORKNUM ? "base/%u/t%d_%u_%s" : "base/%u/t%d_%u",
+                                           dbNode, backendId, relNode,
+                                           forkNumber != MAIN_FORKNUM ? FORK_NAMES[forkNumber] : NULL);
       }
    }
    else
    {
-      char* version_directory = pgmoneta_wal_get_tablespace_version_directory();
       /* All other tablespaces are accessed via symlinks */
-      if (backendId == INVALID_BACKEND_ID)
+      char* version_directory = pgmoneta_wal_get_tablespace_version_directory();
+      if (!version_directory)
       {
+         goto error;
+      }
 
-         if (forkNumber != MAIN_FORKNUM)
-         {
-            path = pgmoneta_format_and_append(path, "pg_tblspc/%u/%s/%u/%u_%s",
-                                              spcNode, version_directory,
-                                              dbNode, relNode,
-                                              forkNames[forkNumber]);
-         }
-         else
-         {
-            path = pgmoneta_format_and_append(path, "pg_tblspc/%u/%s/%u/%u",
-                                              spcNode, version_directory,
-                                              dbNode, relNode);
-         }
+      if (backendId == INVALID_BACKEND_ID)
+      {
+         path = pgmoneta_format_and_append(path,
+                                           forkNumber != MAIN_FORKNUM ?
+                                           "pg_tblspc/%u/%s/%u/%u_%s" : "pg_tblspc/%u/%s/%u/%u",
+                                           spcNode, version_directory, dbNode, relNode,
+                                           forkNumber != MAIN_FORKNUM ? FORK_NAMES[forkNumber] : NULL);
       }
       else
       {
-         if (forkNumber != MAIN_FORKNUM)
-         {
-            path = pgmoneta_format_and_append(path, "pg_tblspc/%u/%s/%u/t%d_%u_%s",
-                                              spcNode, version_directory,
-                                              dbNode, backendId, relNode,
-                                              forkNames[forkNumber]);
-         }
-         else
-         {
-            path = pgmoneta_format_and_append(path, "pg_tblspc/%u/%s/%u/t%d_%u",
-                                              spcNode, version_directory,
-                                              dbNode, backendId, relNode);
-         }
+         path = pgmoneta_format_and_append(path,
path: src/libpgmoneta/walfile/relpath.c
user: login: jesperpedersen
body: ?
created_at: 2025-03-30T14:14:25Z
author_association: MEMBER

PR comments:
user: login: jesperpedersen
created_at: 2025-03-08T02:59:24Z
body: @sh-soltanpour Can you take a look ?

@7amo10 You need `./uncrustify.sh` for sure
author_association: MEMBER
user: login: jesperpedersen
created_at: 2025-03-08T18:15:03Z
body: There is something up... leave out any changes that you didn't change; I mean files
author_association: MEMBER
user: login: sh-soltanpour
created_at: 2025-03-08T20:07:28Z
body: yeah @7amo10 please remove unwanted changes and squash your commits based on [this guide](https://github.com/pgmoneta/pgmoneta/blob/main/CONTRIBUTING.md#development) and I'll take a look
author_association: COLLABORATOR
user: login: 7amo10
created_at: 2025-03-10T14:57:17Z
body: @sh-soltanpour 
I had to force push to remove an unwanted changes and ensure the PR stays focused on `relpath.c`. The changes remain the same functionally. Let me know if anything needs revision!
author_association: CONTRIBUTOR
user: login: jesperpedersen
created_at: 2025-03-10T15:22:56Z
body: @7amo10 Take a look at your patch, and remove unneeded changes. `uncrustify.sh` is also your friend
author_association: MEMBER
user: login: 7amo10
created_at: 2025-03-12T12:46:15Z
body: @jesperpedersen 

I’ve updated the commit with the changes to `src/libpgmoneta/walfile/relpath.c`. I’ve kept the functional improvements (input validation, memory management, error codes, etc.) as intended, and followed it with a formatting commit using `./uncrustify.sh` per the guidelines. I hope this addresses your feedback about removing unwanted changes—if I misunderstood, please let me know.

Could you clarify if there’s anything else you’d like me to adjust or improve? 
author_association: CONTRIBUTOR
user: login: jesperpedersen
created_at: 2025-03-12T14:21:26Z
body: Apply those comments over the entire patch
author_association: MEMBER
user: login: 7amo10
created_at: 2025-03-12T19:53:56Z
body: @jesperpedersen 
I have applied the requested changes:

- Resolve Indentation problems and extra spaces
- Removed unnecessary modifications that were unrelated to error handling and validation.
- Ensured proper memory management by handling allocations correctly in `pgmoneta_wal_get_tablespace_version_directory()`.

Please review and let me know if any further adjustments are needed. Thanks for your guidance!
author_association: CONTRIBUTOR
user: login: jesperpedersen
created_at: 2025-03-30T11:10:59Z
body: @7amo10 Can you rebase, and fix the above ?
author_association: MEMBER
user: login: jesperpedersen
created_at: 2025-03-30T14:14:57Z
body: You need to fix the `if-else`s
author_association: MEMBER
user: login: jesperpedersen
created_at: 2025-03-30T14:36:17Z
body: You need to reference the issue number in the commit subject -- see the git log for examples
author_association: MEMBER
user: login: 7amo10
created_at: 2025-03-31T15:40:34Z
body: > You need to reference the issue number in the commit subject -- see the git log for examples

Please review the this and merge whenever you have time.
author_association: CONTRIBUTOR
user: login: jesperpedersen
created_at: 2025-03-31T16:31:07Z
body: Merged.

Thanks for your contribution !
author_association: MEMBER