Skip to content

feat: Enhance relpath error handling and validation - #505

Merged
jesperpedersen merged 1 commit into
pgmoneta:mainfrom
7amo10:Improve-relpath-validation
Mar 31, 2025
Merged

jesperpedersen merged 1 commit into
pgmoneta:mainfrom
7amo10:Improve-relpath-validation

Conversation

@7amo10

@7amo10 7amo10 commented Mar 7, 2025

Copy link
Copy Markdown
Contributor

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

@jesperpedersen

Copy link
Copy Markdown
Member

@sh-soltanpour Can you take a look ?

@7amo10 You need ./uncrustify.sh for sure

@jesperpedersen jesperpedersen added the enhancement Improvement to an existing feature label Mar 8, 2025
@jesperpedersen

Copy link
Copy Markdown
Member

There is something up... leave out any changes that you didn't change; I mean files

@sh-soltanpour

Copy link
Copy Markdown
Collaborator

yeah @7amo10 please remove unwanted changes and squash your commits based on this guide and I'll take a look

@7amo10
7amo10 force-pushed the Improve-relpath-validation branch 2 times, most recently from aaf22cf to cd0f579 Compare March 10, 2025 14:45
@7amo10

7amo10 commented Mar 10, 2025

Copy link
Copy Markdown
Contributor Author

@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!

@jesperpedersen

Copy link
Copy Markdown
Member

@7amo10 Take a look at your patch, and remove unneeded changes. uncrustify.sh is also your friend

@7amo10
7amo10 force-pushed the Improve-relpath-validation branch from cd0f579 to c437603 Compare March 11, 2025 14:19
@7amo10

7amo10 commented Mar 12, 2025

Copy link
Copy Markdown
Contributor Author

@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?

Comment thread src/libpgmoneta/walfile/relpath.c Resolved
Comment thread src/libpgmoneta/walfile/relpath.c Resolved
Comment thread src/libpgmoneta/walfile/relpath.c Resolved
Comment thread src/libpgmoneta/walfile/relpath.c Resolved
Comment thread src/libpgmoneta/walfile/relpath.c Resolved
@jesperpedersen

Copy link
Copy Markdown
Member

Apply those comments over the entire patch

@7amo10
7amo10 force-pushed the Improve-relpath-validation branch from c437603 to 1546660 Compare March 12, 2025 19:42
@7amo10

7amo10 commented Mar 12, 2025 •

Copy link
Copy Markdown
Contributor Author

@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!

Comment thread src/libpgmoneta/walfile/relpath.c Outdated
char*
pgmoneta_wal_get_catalog_version_number(void)
{
if (!server_config || server_config->version < MIN_PG_VERSION ||

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everything other than !server_config is already handled in the switch case

Comment thread src/libpgmoneta/walfile/relpath.c Outdated
error:
if (result)
{
free(result);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if is not needed, free(NULL)
If ptr is a null pointer, the function does nothing.

Comment thread src/libpgmoneta/walfile/relpath.c Outdated
error:
if (path)
{
free(path);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if is not needed

Comment thread src/libpgmoneta/walfile/relpath.c Outdated
free(version_directory);
}

if (!path)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we need the if here?

Comment thread src/libpgmoneta/walfile/relpath.c Outdated
goto error;
}

catalog_version = pgmoneta_wal_get_catalog_version_number();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you're already handling this in pgmoneta_wal_get_catalog_version_number

@7amo10
7amo10 force-pushed the Improve-relpath-validation branch from 1546660 to 37f7864 Compare March 19, 2025 15:01
Comment thread src/libpgmoneta/walfile/relpath.c Outdated
spcNode, version_directory,
dbNode, backendId, relNode);
}
path = pgmoneta_format_and_append(path,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO the if and else approach is easier to read. WDYT? @jesperpedersen

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, keep the if-else

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep the if

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?

Comment thread src/libpgmoneta/walfile/relpath.c Resolved
Comment thread src/libpgmoneta/walfile/relpath.c Resolved
@jesperpedersen

Copy link
Copy Markdown
Member

@7amo10 Can you rebase, and fix the above ?

@7amo10
7amo10 force-pushed the Improve-relpath-validation branch from 37f7864 to 6a10a22 Compare March 30, 2025 12:22
Comment thread src/libpgmoneta/walfile/relpath.c Outdated
return forkNumber >= MAIN_FORKNUM && forkNumber <= INIT_FORKNUM;
}

static const char* const FORK_NAMES[] = {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move { to newline

Comment thread src/libpgmoneta/walfile/relpath.c Outdated
goto error;
}

path = pgmoneta_format_and_append(path,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do an if

Comment thread src/libpgmoneta/walfile/relpath.c Outdated
path = pgmoneta_format_and_append(path, "base/%u/%u",
dbNode, relNode);
}
path = pgmoneta_format_and_append(path,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep the if

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?

Comment thread src/libpgmoneta/walfile/relpath.c Outdated
path = pgmoneta_format_and_append(path, "base/%u/t%d_%u",
dbNode, backendId, relNode);
}
path = pgmoneta_format_and_append(path,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep the if

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?

Comment thread src/libpgmoneta/walfile/relpath.c Outdated
spcNode, version_directory,
dbNode, backendId, relNode);
}
path = pgmoneta_format_and_append(path,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep the if

@7amo10
7amo10 force-pushed the Improve-relpath-validation branch 2 times, most recently from 27fcd01 to 7e89605 Compare March 30, 2025 14:07
Comment thread src/libpgmoneta/walfile/relpath.c Outdated
path = pgmoneta_format_and_append(path, "base/%u/%u",
dbNode, relNode);
}
path = pgmoneta_format_and_append(path,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?

Comment thread src/libpgmoneta/walfile/relpath.c Outdated
path = pgmoneta_format_and_append(path, "base/%u/t%d_%u",
dbNode, backendId, relNode);
}
path = pgmoneta_format_and_append(path,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?

Comment thread src/libpgmoneta/walfile/relpath.c Outdated
spcNode, version_directory,
dbNode, backendId, relNode);
}
path = pgmoneta_format_and_append(path,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?

@jesperpedersen

Copy link
Copy Markdown
Member

You need to fix the if-elses

@7amo10
7amo10 force-pushed the Improve-relpath-validation branch from 7e89605 to 2a24b7a Compare March 30, 2025 14:28
@jesperpedersen

Copy link
Copy Markdown
Member

You need to reference the issue number in the commit subject -- see the git log for examples

7amo10 added a commit to 7amo10/pgmoneta that referenced this pull request Mar 30, 2025
@7amo10
7amo10 force-pushed the Improve-relpath-validation branch from 2a24b7a to 0f40eb1 Compare March 30, 2025 14:52
@7amo10
7amo10 force-pushed the Improve-relpath-validation branch from 0f40eb1 to 1bd64e2 Compare March 30, 2025 15:02
@7amo10

7amo10 commented Mar 31, 2025

Copy link
Copy Markdown
Contributor Author

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.

@jesperpedersen
jesperpedersen merged commit 1bd64e2 into pgmoneta:main Mar 31, 2025
@jesperpedersen

Copy link
Copy Markdown
Member

Merged.

Thanks for your contribution !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement Improvement to an existing feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants