Skip to content

Fix: strdup memory check #523

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 15 commits into
base: v2.4-stable
Choose a base branch
from

Conversation

XV02
Copy link

@XV02 XV02 commented May 26, 2025

Summary of changes

This pull request includes following changes or fixes.

  • Added checks for strdup
  • Changed logic of delim strategy to avoid repeated code

Description

This change adds checks for memory allocations done by strdup which previously could lead to unexpected Segmentation faults when the malloc failed and returned null.
Also updated the log of one of the uses of strdup to avoid repeated code and stepping up maintainability.

Type of change

Please delete items that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have confirmed my fix is effective or that my feature works

@XV02 XV02 self-assigned this May 26, 2025
@XV02 XV02 requested review from piste-jp and removed request for piste-jp May 26, 2025 20:27
Copy link
Member

@vandelvan vandelvan left a comment

Choose a reason for hiding this comment

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

Hey @XV02, thanks for your effort! Great work!

But I'm concerned by the amount of To-dos you added, have you properly tested each check before creating this PR? I am hoping you only forgot to delete those comments because otherwise we will not approve a PR that adds technical debt.

I left a comment in each of those To-dos to keep track of each of them and know which ones have been tested.
Thanks!

@@ -1912,7 +1912,12 @@ int ltfs_fsops_symlink_path(const char* to, const char* from, ltfs_file_id *id,

id->uid = d->uid;
id->ino = d->ino;
// TODO: Check if the return error is needed or can be skipped.
Copy link
Member

Choose a reason for hiding this comment

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

Please avoid adding technical debt hehe, did you just forget to erase this TODO? Or you did not manage to check it?

@@ -489,6 +489,11 @@ int ltfsmsg_internal(bool print_id, int level, char **msg_out, const char *_id,
vsprintf(msg_buf, output_buf, argp);
va_end(argp);
*msg_out = strdup(msg_buf);
// TODO: Check if the return -1 is needed, or the program can just continue
Copy link
Member

Choose a reason for hiding this comment

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

Same as before, please do not add more technical debt, I cannot approve a PR that has not been properly tested 😅

@@ -189,6 +189,10 @@ int camtape_ioctlrc2err(void *device, int fd, struct scsi_sense_data *sense_data

if (msg) {
*msg = strdup("No Sense Information");
if (! (*msg)) {
ltfsmsg(LTFS_ERR, 10001E, "camtape_ioctlrc2err: msg assign");
// TODO: Check if error return is needed
Copy link
Member

Choose a reason for hiding this comment

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

Same as before

@@ -217,6 +221,10 @@ int camtape_ioctlrc2err(void *device, int fd, struct scsi_sense_data *sense_data
ltfsmsg(LTFS_INFO, 31212I, rc_sense);
if (msg)
*msg = strdup("Cannot get sense information");
if (! (*msg)) {
ltfsmsg(LTFS_ERR, 10001E, "camtape_ioctlrc2err: msg assign");
// TODO: Check if error return is needed
Copy link
Member

Choose a reason for hiding this comment

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

Same as before

@@ -515,6 +515,10 @@ static inline int _sense2errcode(uint32_t sense, struct error_table *table, char
rc = table[i].err_code;
if (msg)
*msg = strdup(table[i].msg);
if (! (*msg)) {
ltfsmsg(LTFS_ERR, 10001E, "_sense2errcode: msg assign");
// TODO: Check if error return is needed
Copy link
Member

Choose a reason for hiding this comment

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

Same as before

@@ -3946,6 +4002,10 @@ int camtape_set_lbp(void *device, bool enable)
entry = mt_status_entry_find(&mtinfo, tmpname);
if (entry == NULL) {
msg = strdup("Cannot find sa(4) protection.protection_supported parameter");
if (! (*msg)) {
ltfsmsg(LTFS_ERR, 10001E, "camtape_set_lbp: msg assign");
// TODO: Check if error return is needed
Copy link
Member

Choose a reason for hiding this comment

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

Same as before

@@ -3965,6 +4025,10 @@ int camtape_set_lbp(void *device, bool enable)
prot_entry = mt_status_entry_find(&mtinfo, MT_PROTECTION_NAME);
if (prot_entry == NULL) {
msg = strdup("Cannot find sa(4) protection node!");
if (! (*msg)) {
ltfsmsg(LTFS_ERR, 10001E, "camtape_set_lbp: msg assign");
// TODO: Check if error return is needed
Copy link
Member

Choose a reason for hiding this comment

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

Same as before

@@ -3998,6 +4062,10 @@ int camtape_set_lbp(void *device, bool enable)
entry = mt_entry_find(prot_entry, __DECONST(char *, protect_list[i].name));
if (entry == NULL) {
msg = strdup("Cannot find all protection information entries");
if (! (*msg)) {
ltfsmsg(LTFS_ERR, 10001E, "camtape_set_lbp: msg assign");
// TODO: Check if error return is needed
Copy link
Member

Choose a reason for hiding this comment

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

Same as before

@@ -4020,6 +4088,10 @@ int camtape_set_lbp(void *device, bool enable)
snprintf(tmpstr, sizeof(tmpstr), "Error returned from MTIOCSETLIST ioctl to set "
"protection parameters: %s", strerror(errno));
msg = strdup(tmpstr);
if (! (*msg)) {
ltfsmsg(LTFS_ERR, 10001E, "camtape_set_lbp: msg assign");
// TODO: Check if error return is needed
Copy link
Member

Choose a reason for hiding this comment

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

Same as before

@@ -294,10 +294,15 @@ int main(int argc, char **argv)
while (true) {
int option_index = 0;
int c = getopt_long(argc, argv, short_options, long_options, &option_index);
// TODO: Check for optarg to not be null before using it
Copy link
Member

Choose a reason for hiding this comment

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

Same as before

Copy link
Author

Choose a reason for hiding this comment

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

Working on it for all of them, thanks.

@XV02
Copy link
Author

XV02 commented May 26, 2025

Hi @vandelvan thanks for the reminder, I need to remove the ones checked already, only a couple where left for review, I will update it and come back with the full code checked and ready.

Copy link
Member

@piste-jp piste-jp left a comment

Choose a reason for hiding this comment

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

Please consider how to handle memory allocation error correctly.

The important thing is not detecting the memory allocation error, how to handle it.

  • Memory allocation error might be one shot ? or permanent ?
  • LTFS can keep routing the tape after memory error ?
  • No data corruption after memory allocation error handling ?
  • No memory leak after memory allocation error ?
  • No cleaning resources after memory allocation error ?

Copy link
Member

Choose a reason for hiding this comment

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

Did you really test this code? This code is only compiled under FreeBSD.

Copy link
Author

Choose a reason for hiding this comment

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

Working on it.

Copy link
Member

Choose a reason for hiding this comment

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

Did you really test this code? This code is only compiled under FreeBSD.

Copy link
Author

Choose a reason for hiding this comment

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

Working on it.

Copy link
Member

Choose a reason for hiding this comment

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

Did you really test this code? This code is only compiled under FreeBSD.

Copy link
Author

Choose a reason for hiding this comment

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

Working on it.

Copy link
Member

@piste-jp piste-jp left a comment

Choose a reason for hiding this comment

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

It looks a few critical updates are included. I cannot accept this PR.

}
else if (*delim == '/') {
Copy link
Member

Choose a reason for hiding this comment

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

Please follow the convention. You must not break a line before else.

It looks there are multiple points that is same style. Please correct all of them.

Copy link
Author

Choose a reason for hiding this comment

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

Will check them.

@@ -1659,7 +1659,7 @@ int ltfs_mount(bool force_full, bool deep_recovery, bool recover_extra, bool rec
(unsigned long long)volume_change_ref);

/* Index of IP could be corrupted. So set skip flag to true */
ret = _ltfs_search_index_wp(recover_symlink, true, &seekpos, vol);
ret = _ltfs_search_index_wp(recover_symlink, false, &seekpos, vol);
Copy link
Member

Choose a reason for hiding this comment

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

Why this line is changed?

It looks you ruined the fix made before.

Copy link
Author

Choose a reason for hiding this comment

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

It looks a change from somewhere else got into the PR, fixing it, thanks for the catch.

/* Index of DP could be corrupted. So set skip flag to false */
ret = _ltfs_search_index_wp(recover_symlink, false, &seekpos, vol);
}
/* Index of IP could be corrupted. So set skip flag */
Copy link
Member

Choose a reason for hiding this comment

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

Why this part is changed?

It looks you ruined the fix made before.

Copy link
Author

Choose a reason for hiding this comment

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

It looks a change from somewhere else got into the PR, fixing it, thanks for the catch.
@piste-jp Can you also respond my other comments? Thanks, appreciate it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants