-
Notifications
You must be signed in to change notification settings - Fork 62
Fix issues discovered by static analysis #1127
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -790,10 +790,20 @@ gboolean bd_s390_zfcp_scsi_offline (const gchar *devno, const gchar *wwpn, const | |
| hba_path = g_strdup_printf ("%s/hba_id", fcpsysfs); | ||
| len = 0; /* should be zero, but re-set it just in case */ | ||
| fd = fopen (hba_path, "r"); | ||
| if (!fd) { | ||
| g_set_error (&l_error, BD_S390_ERROR, BD_S390_ERROR_DEVICE, | ||
| "Failed to open %s: %m", hba_path); | ||
| g_free (hba_path); | ||
| g_free (fcpsysfs); | ||
| g_free (scsidev); | ||
| bd_utils_report_finished (progress_id, l_error->message); | ||
| g_propagate_error (error, l_error); | ||
| return FALSE; | ||
| } | ||
|
Comment on lines
+793
to
+802
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This error handling logic is very similar in the three blocks you've added (here, and at lines 822-832, 853-864) and also resembles other error paths in this function. This leads to a lot of duplicated code, which is difficult to maintain. Since this function deals with many allocated resources inside a loop, consider refactoring to centralize cleanup. A common C idiom is to use |
||
| rc = getline (&fcphbasysfs, &len, fd); | ||
| if (rc == -1) { | ||
| g_set_error (&l_error, BD_S390_ERROR, BD_S390_ERROR_DEVICE, | ||
| "Failed to read value from %s", hba_path); | ||
| "Failed to read value from %s: %m", hba_path); | ||
| fclose (fd); | ||
| g_free (hba_path); | ||
| g_free (fcpsysfs); | ||
|
|
@@ -809,6 +819,17 @@ gboolean bd_s390_zfcp_scsi_offline (const gchar *devno, const gchar *wwpn, const | |
| wwpn_path = g_strdup_printf ("%s/wwpn", fcpsysfs); | ||
| len = 0; | ||
| fd = fopen (wwpn_path, "r"); | ||
| if (!fd) { | ||
| g_set_error (&l_error, BD_S390_ERROR, BD_S390_ERROR_DEVICE, | ||
| "Failed to open %s: %m", wwpn_path); | ||
| g_free (wwpn_path); | ||
| g_free (fcphbasysfs); | ||
| g_free (fcpsysfs); | ||
| g_free (scsidev); | ||
| bd_utils_report_finished (progress_id, l_error->message); | ||
| g_propagate_error (error, l_error); | ||
| return FALSE; | ||
| } | ||
| rc = getline (&fcpwwpnsysfs, &len, fd); | ||
| if (rc == -1) { | ||
| g_set_error (&l_error, BD_S390_ERROR, BD_S390_ERROR_DEVICE, | ||
|
|
@@ -829,6 +850,18 @@ gboolean bd_s390_zfcp_scsi_offline (const gchar *devno, const gchar *wwpn, const | |
| lun_path = g_strdup_printf ("%s/fcp_lun", fcpsysfs); | ||
| len = 0; | ||
| fd = fopen (lun_path, "r"); | ||
| if (!fd) { | ||
| g_set_error (&l_error, BD_S390_ERROR, BD_S390_ERROR_DEVICE, | ||
| "Failed to open %s: %m", lun_path); | ||
| g_free (lun_path); | ||
| g_free (fcpwwpnsysfs); | ||
| g_free (fcphbasysfs); | ||
| g_free (fcpsysfs); | ||
| g_free (scsidev); | ||
| bd_utils_report_finished (progress_id, l_error->message); | ||
| g_propagate_error (error, l_error); | ||
| return FALSE; | ||
| } | ||
| rc = getline (&fcplunsysfs, &len, fd); | ||
| if (rc == -1) { | ||
| g_set_error (&l_error, BD_S390_ERROR, BD_S390_ERROR_DEVICE, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While adding this null check is crucial for correctness, the error handling logic is now duplicated in multiple places within this function (e.g., lines 401-406, 412-417, 430-435, and also in existing code). This makes the code harder to maintain and prone to errors if a cleanup path is modified in one place but not others.
Consider refactoring to use a single cleanup point with
goto. This is a common and idiomatic pattern in C for handling resource cleanup and would centralize the logic, reduce duplication, and improve readability.