Skip to contentSkip to conversation

mmc-utils: Close the device fd on the return paths

PATCH In review Shawn Lin 3 participants 3 messages Sep 4 – Sep 10 MMC, SD and SDIO

lore
Share & tools
lore

Shawn Lin

author
#0 lore raw

PATCH 1/1 mmc-utils: Close the device fd on the return paths

From: Shawn Lin <[email protected]>

do_read_extcsd(), do_write_extcsd(), do_writeprotect_user_set(), do_create_gp_partition(), do_enh_area_set() and do_write_reliability_set() open the device but never close it.

For a short lived command line tool the kernel reclaims the descriptors when the process exits, so this is only visible to fd tracking tools (strace open/close pairing, valgrind --track-fds=yes), but it is flagged as a leak and would matter if the code is reused in a long lived process.

Close the descriptor on the paths that return to the caller. The error paths ending in exit(1) terminate the process and rely on the kernel cleanup, consistent with the rest of the codebase.

---
 mmc_cmds.c | 6 ++++++
 1 file changed, 6 insertions(+)
mmc_cmds.c +6 -0
index 43dcdcc..0692505 100644
@@ -550,6 +550,7 @@ int do_writeprotect_user_set(int nargs, char **argv)
 			exit(1);
 		}
 	}
+	close(fd);
 	return ret;
 
 usage:
@@ -1442,6 +1443,7 @@ int do_create_gp_partition(int nargs, char **argv)
 	if (set_partitioning_setting_completed(dry_run, device, fd))
 		exit(1);
 
+	close(fd);
 	return 0;
 }
 
@@ -1589,6 +1591,7 @@ int do_enh_area_set(int nargs, char **argv)
 	if (set_partitioning_setting_completed(dry_run, device, fd))
 		exit(1);
 
+	close(fd);
 	return 0;
 }
 
@@ -1656,6 +1659,7 @@ int do_write_reliability_set(int nargs, char **argv)
 	if (set_partitioning_setting_completed(dry_run, device, fd))
 		exit(1);
 
+	close(fd);
 	return 0;
 }
 
@@ -2170,6 +2174,7 @@ int do_read_extcsd(int nargs, char **argv)
 		       "Please check sysfs node '/sys/devices/.../mmc_host/mmcX/mmcX:XXXX/cmdq_en'\n");
 	}
 out_free:
+	close(fd);
 	return ret;
 }
 
@@ -2202,6 +2207,7 @@ int do_write_extcsd(int nargs, char **argv)
 		exit(1);
 	}
 
+	close(fd);
 	return ret;
 }
 
Signature · 1 line
2.25.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 11 lines

From: Shawn Lin <[email protected]>

do_read_extcsd(), do_write_extcsd(), do_writeprotect_user_set(), do_create_gp_partition(), do_enh_area_set() and do_write_reliability_set() open the device but never close it.

For a short lived command line tool the kernel reclaims the descriptors when the process exits, so this is only visible to fd tracking tools (strace open/close pairing, valgrind --track-fds=yes), but it is flagged as a leak and would matter if the code is reused in a long lived process.

Quoted 5 lines

Close the descriptor on the paths that return to the caller. The error paths ending in exit(1) terminate the process and rely on the kernel cleanup, consistent with the rest of the codebase.

#2 lore raw
In reply to Shawn Lin
Quoted 13 lines · Shawn Lin

From: Shawn Lin <[email protected]>

do_read_extcsd(), do_write_extcsd(), do_writeprotect_user_set(), do_create_gp_partition(), do_enh_area_set() and do_write_reliability_set() open the device but never close it.

For a short lived command line tool the kernel reclaims the descriptors when the process exits, so this is only visible to fd tracking tools (strace open/close pairing, valgrind --track-fds=yes), but it is flagged as a leak and would matter if the code is reused in a long lived process.

Quoted 5 lines

Close the descriptor on the paths that return to the caller. The error paths ending in exit(1) terminate the process and rely on the kernel cleanup, consistent with the rest of the codebase.

Applied for mmc-utils master, thanks!

Kind regards
Uffe

Quoted 59 lines
---
 mmc_cmds.c | 6 ++++++
 1 file changed, 6 insertions(+)
mmc_cmds.c +6 -1
index 43dcdcc..0692505 100644
@@ -550,6 +550,7 @@ int do_writeprotect_user_set(int nargs, char **argv)
                        exit(1);
                }
        }
+       close(fd);
        return ret;
 
 usage:
@@ -1442,6 +1443,7 @@ int do_create_gp_partition(int nargs, char **argv)
        if (set_partitioning_setting_completed(dry_run, device, fd))
                exit(1);
 
+       close(fd);
        return 0;
 }
 
@@ -1589,6 +1591,7 @@ int do_enh_area_set(int nargs, char **argv)
        if (set_partitioning_setting_completed(dry_run, device, fd))
                exit(1);
 
+       close(fd);
        return 0;
 }
 
@@ -1656,6 +1659,7 @@ int do_write_reliability_set(int nargs, char **argv)
        if (set_partitioning_setting_completed(dry_run, device, fd))
                exit(1);
 
+       close(fd);
        return 0;
 }
 
@@ -2170,6 +2174,7 @@ int do_read_extcsd(int nargs, char **argv)
                       "Please check sysfs node '/sys/devices/.../mmc_host/mmcX/mmcX:XXXX/cmdq_en'\n");
        }
 out_free:
+       close(fd);
        return ret;
 }
 
@@ -2202,6 +2207,7 @@ int do_write_extcsd(int nargs, char **argv)
                exit(1);
        }
 
+       close(fd);
        return ret;
 }
 
--

2.25.1