fix memory leak in get_task_name()
Subsystems (3)
v2 cover fix memory leak in get_task_name()
This series fixes some memory leaks related to the usage of the get_task_name() function from lib/fs.c.
Patch 2/2 addresses a coverity warning related to this memory leak, making the code a bit more readable by humans and coverity.
Changelog:
----------
v1 -> v2
- on Stephen's suggestion, drop asprintf() and use a local var for path; additionally drop %m from fscanf to not allocate memory, and resort to a param to return task name to the caller.
- patch 2/3 of the original series is no more needed.
Andrea Claudi (2):
lib/fs: fix memory leak in get_task_name()
rdma: make RES_PID and RES_KERN_NAME alternative to each other
include/utils.h | 2 +- ip/iptuntap.c | 17 ++++++++++------- lib/fs.c | 20 ++++++++++---------- rdma/res-cmid.c | 18 +++++++++--------- rdma/res-cq.c | 17 +++++++++-------- rdma/res-ctx.c | 16 ++++++++-------- rdma/res-mr.c | 15 ++++++++------- rdma/res-pd.c | 17 +++++++++-------- rdma/res-qp.c | 16 ++++++++-------- rdma/res-srq.c | 17 +++++++++-------- rdma/stat.c | 14 +++++++++----- 11 files changed, 90 insertions(+), 79 deletions(-)
Signature · 1 line
2.35.1
PATCH v2 1/2 lib/fs: fix memory leak in get_task_name()
asprintf() allocates memory which is not freed on the error path of get_task_name(), thus potentially leading to memory leaks.
Rework get_task_name() and avoid asprintf() usage and memory allocation, returning the task string to the caller using an additional char* parameter.
--- include/utils.h | 2 +- ip/iptuntap.c | 17 ++++++++++------- lib/fs.c | 20 ++++++++++---------- rdma/res-cmid.c | 8 +++++--- rdma/res-cq.c | 8 +++++--- rdma/res-ctx.c | 7 ++++--- rdma/res-mr.c | 7 ++++--- rdma/res-pd.c | 8 +++++--- rdma/res-qp.c | 7 ++++--- rdma/res-srq.c | 7 ++++--- rdma/stat.c | 5 ++++- 11 files changed, 56 insertions(+), 40 deletions(-)
include/utils.h +1 -1
@@ -307,7 +307,7 @@ char *find_cgroup2_mount(bool do_mount);__u64 get_cgroup2_id (const char *path);char *get_cgroup2_path (__u64 id,bool full);int get_command_name (const char *pid,char *comm,size_t len); -char *get_task_name (pid_t pid); +int get_task_name (pid_t pid,char *name);int get_rtnl_link_stats_rta (struct rtnl_link_stats64 *stats64,struct rtattr *tb[]);
ip/iptuntap.c +10 -7
@@ -321,14 +321,17 @@ static void show_processes(const char *name) }else if (err ==2 && !strcmp ("iff" , key) && !strcmp (name, value)) { -char *pname =get_task_name (pid); - -print_string (PRINT_ANY ,"name" , -"%s" , pname ? :"<NULL>" ); +SPRINT_BUF (pname); + +if (get_task_name (pid, pname)) { +print_string (PRINT_ANY ,"name" , +"%s" ,"<NULL>" ); + }else { +print_string (PRINT_ANY ,"name" , +"%s" , pname); + } -print_uint (PRINT_ANY ,"pid" , -"(%d)" , pid); -free (pname); +print_uint (PRINT_ANY ,"pid" ,"(%d)" , pid); }free (key);
lib/fs.c +10 -10
@@ -342,25 +342,25 @@ int get_command_name(const char *pid, char *comm, size_t len)return 0 ; } -char *get_task_name (pid_t pid) +int get_task_name (pid_t pid,char *name) { -char *comm; +char path[PATH_MAX ];FILE *f;if (!pid) -return NULL ; +return -1 ; -if (asprintf (&comm,"/proc/%d/comm" , pid) <0 ) -return NULL ; +if (snprintf (path,sizeof (path),"/proc/%d/comm" , pid) >=sizeof (path)) +return -1 ; - f =fopen (comm,"r" ); + f =fopen (path,"r" );if (!f) -return NULL ; +return -1 ; -if (fscanf (f,"%ms\n" , &comm) !=1 ) - comm =NULL ; +if (fscanf (f,"%s\n" , name) !=1 ) +return -1 ;fclose (f); -return comm; +return 0 ; }
rdma/res-cmid.c +5 -3
@@ -159,8 +159,11 @@ static int res_cm_id_line(struct rd *rd, const char *name, int idx, goto out;if (nla_line[RDMA_NLDEV_ATTR_RES_PID ]) { +SPRINT_BUF (b); + pid =mnl_attr_get_u32 (nla_line[RDMA_NLDEV_ATTR_RES_PID ]); - comm =get_task_name (pid); +if (!get_task_name (pid, b)) + comm = b; } if (rd_is_filtered_attr(rd, "pid", pid,
@@ -199,8 +202,7 @@ static int res_cm_id_line(struct rd *rd, const char *name, int idx,print_driver_table (rd, nla_line[RDMA_NLDEV_ATTR_DRIVER ]);newline (rd); -out:if (nla_line[RDMA_NLDEV_ATTR_RES_PID ]) -free (comm); +out:return MNL_CB_OK ; }
rdma/res-cq.c +5 -3
@@ -84,8 +84,11 @@ static int res_cq_line(struct rd *rd, const char *name, int idx, goto out;if (nla_line[RDMA_NLDEV_ATTR_RES_PID ]) { +SPRINT_BUF (b); + pid =mnl_attr_get_u32 (nla_line[RDMA_NLDEV_ATTR_RES_PID ]); - comm =get_task_name (pid); +if (!get_task_name (pid, b)) + comm = b; } if (rd_is_filtered_attr(rd, "pid", pid,
@@ -123,8 +126,7 @@ static int res_cq_line(struct rd *rd, const char *name, int idx,print_driver_table (rd, nla_line[RDMA_NLDEV_ATTR_DRIVER ]);newline (rd); -out:if (nla_line[RDMA_NLDEV_ATTR_RES_PID ]) -free (comm); +out:return MNL_CB_OK ; }
rdma/res-ctx.c +4 -3
@@ -18,8 +18,11 @@ static int res_ctx_line(struct rd *rd, const char *name, int idx,return MNL_CB_ERROR ;if (nla_line[RDMA_NLDEV_ATTR_RES_PID ]) { +SPRINT_BUF (b); + pid =mnl_attr_get_u32 (nla_line[RDMA_NLDEV_ATTR_RES_PID ]); - comm =get_task_name (pid); +if (!get_task_name (pid, b)) + comm = b; } if (rd_is_filtered_attr(rd, "pid", pid,
@@ -48,8 +51,6 @@ static int res_ctx_line(struct rd *rd, const char *name, int idx,newline (rd); out: -if (nla_line[RDMA_NLDEV_ATTR_RES_PID ]) -free (comm);return MNL_CB_OK ; }
rdma/res-mr.c +4 -3
@@ -47,8 +47,11 @@ static int res_mr_line(struct rd *rd, const char *name, int idx, goto out;if (nla_line[RDMA_NLDEV_ATTR_RES_PID ]) { +SPRINT_BUF (b); + pid =mnl_attr_get_u32 (nla_line[RDMA_NLDEV_ATTR_RES_PID ]); - comm =get_task_name (pid); +if (!get_task_name (pid, b)) + comm = b; } if (rd_is_filtered_attr(rd, "pid", pid,
@@ -87,8 +90,6 @@ static int res_mr_line(struct rd *rd, const char *name, int idx,newline (rd); out: -if (nla_line[RDMA_NLDEV_ATTR_RES_PID ]) -free (comm);return MNL_CB_OK ; }
rdma/res-pd.c +5 -3
@@ -34,8 +34,11 @@ static int res_pd_line(struct rd *rd, const char *name, int idx, nla_line[RDMA_NLDEV_ATTR_RES_UNSAFE_GLOBAL_RKEY]);if (nla_line[RDMA_NLDEV_ATTR_RES_PID ]) { +SPRINT_BUF (b); + pid =mnl_attr_get_u32 (nla_line[RDMA_NLDEV_ATTR_RES_PID ]); - comm =get_task_name (pid); +if (!get_task_name (pid, b)) + comm = b; } if (rd_is_filtered_attr(rd, "pid", pid,
@@ -76,8 +79,7 @@ static int res_pd_line(struct rd *rd, const char *name, int idx,print_driver_table (rd, nla_line[RDMA_NLDEV_ATTR_DRIVER ]);newline (rd); -out:if (nla_line[RDMA_NLDEV_ATTR_RES_PID ]) -free (comm); +out:return MNL_CB_OK ; }
rdma/res-qp.c +4 -3
@@ -146,8 +146,11 @@ static int res_qp_line(struct rd *rd, const char *name, int idx, goto out;if (nla_line[RDMA_NLDEV_ATTR_RES_PID ]) { +SPRINT_BUF (b); + pid =mnl_attr_get_u32 (nla_line[RDMA_NLDEV_ATTR_RES_PID ]); - comm =get_task_name (pid); +if (!get_task_name (pid, b)) + comm = b; } if (rd_is_filtered_attr(rd, "pid", pid,
@@ -179,8 +182,6 @@ static int res_qp_line(struct rd *rd, const char *name, int idx,print_driver_table (rd, nla_line[RDMA_NLDEV_ATTR_DRIVER ]);newline (rd); out: -if (nla_line[RDMA_NLDEV_ATTR_RES_PID ]) -free (comm);return MNL_CB_OK ; }
rdma/res-srq.c +4 -3
@@ -174,8 +174,11 @@ static int res_srq_line(struct rd *rd, const char *name, int idx,return MNL_CB_ERROR ;if (nla_line[RDMA_NLDEV_ATTR_RES_PID ]) { +SPRINT_BUF (b); + pid =mnl_attr_get_u32 (nla_line[RDMA_NLDEV_ATTR_RES_PID ]); - comm =get_task_name (pid); +if (!get_task_name (pid, b)) + comm = b; }if (rd_is_filtered_attr (rd,"pid" , pid, nla_line[RDMA_NLDEV_ATTR_RES_PID ]))
@@ -228,8 +231,6 @@ static int res_srq_line(struct rd *rd, const char *name, int idx,newline (rd); out: -if (nla_line[RDMA_NLDEV_ATTR_RES_PID ]) -free (comm);return MNL_CB_OK ; }
rdma/stat.c +4 -1
@@ -248,8 +248,11 @@ static int res_counter_line(struct rd *rd, const char *name, int index,return MNL_CB_OK ;if (nla_line[RDMA_NLDEV_ATTR_RES_PID ]) { +SPRINT_BUF (b); + pid =mnl_attr_get_u32 (nla_line[RDMA_NLDEV_ATTR_RES_PID ]); - comm =get_task_name (pid); +if (!get_task_name (pid, b)) + comm = b; }if (rd_is_filtered_attr (rd,"pid" , pid, nla_line[RDMA_NLDEV_ATTR_RES_PID ]))
Signature · 1 line
2.35.1
PATCH v2 2/2 rdma: make RES_PID and RES_KERN_NAME alternative to each other
In reply to Andrea ClaudiRDMA_NLDEV_ATTR_RES_PID and RDMA_NLDEV_ATTR_RES_KERN_NAME cannot be set together, as evident for the fill_res_name_pid() function in the kernel infiniband driver. This commit makes this clear at first glance, using an else branch for the RDMA_NLDEV_ATTR_RES_KERN_NAME case.
This also helps coverity to better understand this code and avoid producing a bogus warning complaining about mnl_attr_get_str overwriting comme, and thus leaking the storage that comm points to.
--- rdma/res-cmid.c | 10 ++++------ rdma/res-cq.c | 9 ++++----- rdma/res-ctx.c | 9 ++++----- rdma/res-mr.c | 8 ++++---- rdma/res-pd.c | 9 ++++----- rdma/res-qp.c | 9 ++++----- rdma/res-srq.c | 10 +++++----- rdma/stat.c | 9 +++++---- 8 files changed, 34 insertions(+), 39 deletions(-)
rdma/res-cmid.c +4 -6
@@ -164,6 +164,10 @@ static int res_cm_id_line(struct rd *rd, const char *name, int idx, pid =mnl_attr_get_u32 (nla_line[RDMA_NLDEV_ATTR_RES_PID ]);if (!get_task_name (pid, b)) comm = b; + }else if (nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME ]) { +/* discard const from mnl_attr_get_str */ + comm = (char *)mnl_attr_get_str ( + nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME ]); } if (rd_is_filtered_attr(rd, "pid", pid,
@@ -176,12 +180,6 @@ static int res_cm_id_line(struct rd *rd, const char *name, int idx, nla_line[RDMA_NLDEV_ATTR_RES_CM_IDN])) goto out; -if (nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME ]) { -/* discard const from mnl_attr_get_str */ - comm = (char *)mnl_attr_get_str ( - nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME ]); - } -open_json_object (NULL );print_link (rd, idx, name, port, nla_line); res_print_uint(rd, "cm-idn", cm_idn,
rdma/res-cq.c +4 -5
@@ -89,6 +89,10 @@ static int res_cq_line(struct rd *rd, const char *name, int idx, pid =mnl_attr_get_u32 (nla_line[RDMA_NLDEV_ATTR_RES_PID ]);if (!get_task_name (pid, b)) comm = b; + }else if (nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME ]) { +/* discard const from mnl_attr_get_str */ + comm = (char *)mnl_attr_get_str ( + nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME ]); } if (rd_is_filtered_attr(rd, "pid", pid,
@@ -106,11 +110,6 @@ static int res_cq_line(struct rd *rd, const char *name, int idx, nla_line[RDMA_NLDEV_ATTR_RES_CTXN])) goto out; -if (nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME ]) -/* discard const from mnl_attr_get_str */ - comm = (char *)mnl_attr_get_str ( - nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME ]); -open_json_object (NULL );print_dev (rd, idx, name);res_print_uint (rd,"cqn" , cqn, nla_line[RDMA_NLDEV_ATTR_RES_CQN ]);
rdma/res-ctx.c +4 -5
@@ -23,6 +23,10 @@ static int res_ctx_line(struct rd *rd, const char *name, int idx, pid =mnl_attr_get_u32 (nla_line[RDMA_NLDEV_ATTR_RES_PID ]);if (!get_task_name (pid, b)) comm = b; + }else if (nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME ]) { +/* discard const from mnl_attr_get_str */ + comm = (char *)mnl_attr_get_str ( + nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME ]); } if (rd_is_filtered_attr(rd, "pid", pid,
@@ -36,11 +40,6 @@ static int res_ctx_line(struct rd *rd, const char *name, int idx, nla_line[RDMA_NLDEV_ATTR_RES_CTXN])) goto out; -if (nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME ]) -/* discard const from mnl_attr_get_str */ - comm = (char *)mnl_attr_get_str ( - nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME ]); -open_json_object (NULL );print_dev (rd, idx, name);res_print_uint (rd,"ctxn" , ctxn, nla_line[RDMA_NLDEV_ATTR_RES_CTXN ]);
rdma/res-mr.c +4 -4
@@ -52,6 +52,10 @@ static int res_mr_line(struct rd *rd, const char *name, int idx, pid =mnl_attr_get_u32 (nla_line[RDMA_NLDEV_ATTR_RES_PID ]);if (!get_task_name (pid, b)) comm = b; + }else if (nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME ]) { +/* discard const from mnl_attr_get_str */ + comm = (char *)mnl_attr_get_str ( + nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME ]); } if (rd_is_filtered_attr(rd, "pid", pid,
@@ -70,10 +74,6 @@ static int res_mr_line(struct rd *rd, const char *name, int idx, nla_line[RDMA_NLDEV_ATTR_RES_PDN])) goto out; -if (nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME ]) -/* discard const from mnl_attr_get_str */ - comm = (char *)mnl_attr_get_str ( - nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME ]);open_json_object (NULL );print_dev (rd, idx, name);res_print_uint (rd,"mrn" , mrn, nla_line[RDMA_NLDEV_ATTR_RES_MRN ]);
rdma/res-pd.c +4 -5
@@ -39,6 +39,10 @@ static int res_pd_line(struct rd *rd, const char *name, int idx, pid =mnl_attr_get_u32 (nla_line[RDMA_NLDEV_ATTR_RES_PID ]);if (!get_task_name (pid, b)) comm = b; + }else if (nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME ]) { +/* discard const from mnl_attr_get_str */ + comm = (char *)mnl_attr_get_str ( + nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME ]); } if (rd_is_filtered_attr(rd, "pid", pid,
@@ -58,11 +62,6 @@ static int res_pd_line(struct rd *rd, const char *name, int idx, nla_line[RDMA_NLDEV_ATTR_RES_PDN])) goto out; -if (nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME ]) -/* discard const from mnl_attr_get_str */ - comm = (char *)mnl_attr_get_str ( - nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME ]); -open_json_object (NULL );print_dev (rd, idx, name);res_print_uint (rd,"pdn" , pdn, nla_line[RDMA_NLDEV_ATTR_RES_PDN ]);
rdma/res-qp.c +4 -5
@@ -151,17 +151,16 @@ static int res_qp_line(struct rd *rd, const char *name, int idx, pid =mnl_attr_get_u32 (nla_line[RDMA_NLDEV_ATTR_RES_PID ]);if (!get_task_name (pid, b)) comm = b; + }else if (nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME ]) { +/* discard const from mnl_attr_get_str */ + comm = (char *)mnl_attr_get_str ( + nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME ]); }if (rd_is_filtered_attr (rd,"pid" , pid, nla_line[RDMA_NLDEV_ATTR_RES_PID ])) goto out; -if (nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME ]) -/* discard const from mnl_attr_get_str */ - comm = (char *)mnl_attr_get_str ( - nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME ]); -open_json_object (NULL );print_link (rd, idx, name, port, nla_line);res_print_uint (rd,"lqpn" , lqpn, nla_line[RDMA_NLDEV_ATTR_RES_LQPN ]);
rdma/res-srq.c +5 -5
@@ -179,7 +179,12 @@ static int res_srq_line(struct rd *rd, const char *name, int idx, pid =mnl_attr_get_u32 (nla_line[RDMA_NLDEV_ATTR_RES_PID ]);if (!get_task_name (pid, b)) comm = b; + }else if (nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME ]) { +/* discard const from mnl_attr_get_str */ + comm = (char *)mnl_attr_get_str ( + nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME ]); } +if (rd_is_filtered_attr (rd,"pid" , pid, nla_line[RDMA_NLDEV_ATTR_RES_PID ])) goto out;
@@ -212,11 +217,6 @@ static int res_srq_line(struct rd *rd, const char *name, int idx, MNL_CB_OK) goto out; -if (nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME ]) -/* discard const from mnl_attr_get_str */ - comm = (char *)mnl_attr_get_str ( - nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME ]); -open_json_object (NULL );print_dev (rd, idx, name);res_print_uint (rd,"srqn" , srqn, nla_line[RDMA_NLDEV_ATTR_RES_SRQN ]);
rdma/stat.c +5 -4
@@ -253,15 +253,16 @@ static int res_counter_line(struct rd *rd, const char *name, int index, pid =mnl_attr_get_u32 (nla_line[RDMA_NLDEV_ATTR_RES_PID ]);if (!get_task_name (pid, b)) comm = b; + }else if (nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME ]) { +/* discard const from mnl_attr_get_str */ + comm = (char *)mnl_attr_get_str ( + nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME ]); } +if (rd_is_filtered_attr (rd,"pid" , pid, nla_line[RDMA_NLDEV_ATTR_RES_PID ]))return MNL_CB_OK ; -if (nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME ]) - comm = (char *)mnl_attr_get_str ( - nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME ]); -mnl_attr_for_each_nested (nla_entry, qp_table) {struct nlattr *qp_line[RDMA_NLDEV_ATTR_MAX ] = {};
Signature · 1 line
2.35.1
Re: [PATCH iproute2 v2 1/2] lib/fs: fix memory leak in get_task_name()
In reply to Andrea ClaudiQuoted 2 lines · Andrea Claudi
asprintf()allocates memory which is not freed on the error path ofget_task_name(), thus potentially leading to memory leaks.
Not really, memory is released when the application exits, which is the case here.
Quoted 6 lines
Rework
get_task_name()and avoidasprintf()usage and memory allocation, returning the task string to the caller using an additional char* parameter.Fixes 81bfd01a4c9e ("lib: move get_task_name() from rdma")
As I was told before, in netdev Fixes means that it is a bug that affects users. This is not the case here.
Quoted 14 lines
Signed-off-by Andrea Claudi <[email protected]>--- include/utils.h | 2 +- ip/iptuntap.c | 17 ++++++++++------- lib/fs.c | 20 ++++++++++---------- rdma/res-cmid.c | 8 +++++--- rdma/res-cq.c | 8 +++++--- rdma/res-ctx.c | 7 ++++--- rdma/res-mr.c | 7 ++++--- rdma/res-pd.c | 8 +++++--- rdma/res-qp.c | 7 ++++--- rdma/res-srq.c | 7 ++++--- rdma/stat.c | 5 ++++- 11 files changed, 56 insertions(+), 40 deletions(-)
Honestly, I don't see any need in both patches.
Thanks
Quoted 288 lines
include/utils.h +1
-1@@ -307,7 +307,7 @@ char *find_cgroup2_mount(bool do_mount);__u64 get_cgroup2_id (const char *path);char *get_cgroup2_path (__u64 id,bool full);int get_command_name (const char *pid,char *comm,size_t len); -char *get_task_name (pid_t pid); +int get_task_name (pid_t pid,char *name);int get_rtnl_link_stats_rta (struct rtnl_link_stats64 *stats64,struct rtattr *tb[]);ip/iptuntap.c +10
-7@@ -321,14 +321,17 @@ static void show_processes(const char *name) }else if (err ==2 && !strcmp ("iff" , key) && !strcmp (name, value)) { -char *pname =get_task_name (pid); - -print_string (PRINT_ANY ,"name" , -"%s" , pname ? :"<NULL>" ); +SPRINT_BUF (pname); + +if (get_task_name (pid, pname)) { +print_string (PRINT_ANY ,"name" , +"%s" ,"<NULL>" ); + }else { +print_string (PRINT_ANY ,"name" , +"%s" , pname); + } -print_uint (PRINT_ANY ,"pid" , -"(%d)" , pid); -free (pname); +print_uint (PRINT_ANY ,"pid" ,"(%d)" , pid); }free (key);lib/fs.c +10
-10@@ -342,25 +342,25 @@ int get_command_name(const char *pid, char *comm, size_t len)return 0 ; } -char *get_task_name (pid_t pid) +int get_task_name (pid_t pid,char *name) { -char *comm; +char path[PATH_MAX ];FILE *f;if (!pid) -return NULL ; +return -1 ; -if (asprintf (&comm,"/proc/%d/comm" , pid) <0 ) -return NULL ; +if (snprintf (path,sizeof (path),"/proc/%d/comm" , pid) >=sizeof (path)) +return -1 ; - f =fopen (comm,"r" ); + f =fopen (path,"r" );if (!f) -return NULL ; +return -1 ; -if (fscanf (f,"%ms\n" , &comm) !=1 ) - comm =NULL ; +if (fscanf (f,"%s\n" , name) !=1 ) +return -1 ;fclose (f); -return comm; +return 0 ; }rdma/res-cmid.c +5
-3@@ -159,8 +159,11 @@ static int res_cm_id_line(struct rd *rd, const char *name, int idx, goto out;if (nla_line[RDMA_NLDEV_ATTR_RES_PID ]) { +SPRINT_BUF (b); + pid =mnl_attr_get_u32 (nla_line[RDMA_NLDEV_ATTR_RES_PID ]); - comm =get_task_name (pid); +if (!get_task_name (pid, b)) + comm = b; } if (rd_is_filtered_attr(rd, "pid", pid,@@ -199,8 +202,7 @@ static int res_cm_id_line(struct rd *rd, const char *name, int idx,print_driver_table (rd, nla_line[RDMA_NLDEV_ATTR_DRIVER ]);newline (rd); -out:if (nla_line[RDMA_NLDEV_ATTR_RES_PID ]) -free (comm); +out:return MNL_CB_OK ; }rdma/res-cq.c +5
-3@@ -84,8 +84,11 @@ static int res_cq_line(struct rd *rd, const char *name, int idx, goto out;if (nla_line[RDMA_NLDEV_ATTR_RES_PID ]) { +SPRINT_BUF (b); + pid =mnl_attr_get_u32 (nla_line[RDMA_NLDEV_ATTR_RES_PID ]); - comm =get_task_name (pid); +if (!get_task_name (pid, b)) + comm = b; } if (rd_is_filtered_attr(rd, "pid", pid,@@ -123,8 +126,7 @@ static int res_cq_line(struct rd *rd, const char *name, int idx,print_driver_table (rd, nla_line[RDMA_NLDEV_ATTR_DRIVER ]);newline (rd); -out:if (nla_line[RDMA_NLDEV_ATTR_RES_PID ]) -free (comm); +out:return MNL_CB_OK ; }rdma/res-ctx.c +4
-3@@ -18,8 +18,11 @@ static int res_ctx_line(struct rd *rd, const char *name, int idx,return MNL_CB_ERROR ;if (nla_line[RDMA_NLDEV_ATTR_RES_PID ]) { +SPRINT_BUF (b); + pid =mnl_attr_get_u32 (nla_line[RDMA_NLDEV_ATTR_RES_PID ]); - comm =get_task_name (pid); +if (!get_task_name (pid, b)) + comm = b; } if (rd_is_filtered_attr(rd, "pid", pid,@@ -48,8 +51,6 @@ static int res_ctx_line(struct rd *rd, const char *name, int idx,newline (rd); out: -if (nla_line[RDMA_NLDEV_ATTR_RES_PID ]) -free (comm);return MNL_CB_OK ; }rdma/res-mr.c +4
-3@@ -47,8 +47,11 @@ static int res_mr_line(struct rd *rd, const char *name, int idx, goto out;if (nla_line[RDMA_NLDEV_ATTR_RES_PID ]) { +SPRINT_BUF (b); + pid =mnl_attr_get_u32 (nla_line[RDMA_NLDEV_ATTR_RES_PID ]); - comm =get_task_name (pid); +if (!get_task_name (pid, b)) + comm = b; } if (rd_is_filtered_attr(rd, "pid", pid,@@ -87,8 +90,6 @@ static int res_mr_line(struct rd *rd, const char *name, int idx,newline (rd); out: -if (nla_line[RDMA_NLDEV_ATTR_RES_PID ]) -free (comm);return MNL_CB_OK ; }rdma/res-pd.c +5
-3@@ -34,8 +34,11 @@ static int res_pd_line(struct rd *rd, const char *name, int idx, nla_line[RDMA_NLDEV_ATTR_RES_UNSAFE_GLOBAL_RKEY]);if (nla_line[RDMA_NLDEV_ATTR_RES_PID ]) { +SPRINT_BUF (b); + pid =mnl_attr_get_u32 (nla_line[RDMA_NLDEV_ATTR_RES_PID ]); - comm =get_task_name (pid); +if (!get_task_name (pid, b)) + comm = b; } if (rd_is_filtered_attr(rd, "pid", pid,@@ -76,8 +79,7 @@ static int res_pd_line(struct rd *rd, const char *name, int idx,print_driver_table (rd, nla_line[RDMA_NLDEV_ATTR_DRIVER ]);newline (rd); -out:if (nla_line[RDMA_NLDEV_ATTR_RES_PID ]) -free (comm); +out:return MNL_CB_OK ; }rdma/res-qp.c +4
-3@@ -146,8 +146,11 @@ static int res_qp_line(struct rd *rd, const char *name, int idx, goto out;if (nla_line[RDMA_NLDEV_ATTR_RES_PID ]) { +SPRINT_BUF (b); + pid =mnl_attr_get_u32 (nla_line[RDMA_NLDEV_ATTR_RES_PID ]); - comm =get_task_name (pid); +if (!get_task_name (pid, b)) + comm = b; } if (rd_is_filtered_attr(rd, "pid", pid,@@ -179,8 +182,6 @@ static int res_qp_line(struct rd *rd, const char *name, int idx,print_driver_table (rd, nla_line[RDMA_NLDEV_ATTR_DRIVER ]);newline (rd); out: -if (nla_line[RDMA_NLDEV_ATTR_RES_PID ]) -free (comm);return MNL_CB_OK ; }rdma/res-srq.c +4
-3@@ -174,8 +174,11 @@ static int res_srq_line(struct rd *rd, const char *name, int idx,return MNL_CB_ERROR ;if (nla_line[RDMA_NLDEV_ATTR_RES_PID ]) { +SPRINT_BUF (b); + pid =mnl_attr_get_u32 (nla_line[RDMA_NLDEV_ATTR_RES_PID ]); - comm =get_task_name (pid); +if (!get_task_name (pid, b)) + comm = b; }if (rd_is_filtered_attr (rd,"pid" , pid, nla_line[RDMA_NLDEV_ATTR_RES_PID ]))@@ -228,8 +231,6 @@ static int res_srq_line(struct rd *rd, const char *name, int idx,newline (rd); out: -if (nla_line[RDMA_NLDEV_ATTR_RES_PID ]) -free (comm);return MNL_CB_OK ; }rdma/stat.c +4
-1@@ -248,8 +248,11 @@ static int res_counter_line(struct rd *rd, const char *name, int index,return MNL_CB_OK ;if (nla_line[RDMA_NLDEV_ATTR_RES_PID ]) { +SPRINT_BUF (b); + pid =mnl_attr_get_u32 (nla_line[RDMA_NLDEV_ATTR_RES_PID ]); - comm =get_task_name (pid); +if (!get_task_name (pid, b)) + comm = b; }if (rd_is_filtered_attr (rd,"pid" , pid, nla_line[RDMA_NLDEV_ATTR_RES_PID ]))Signature · 1 line
2.35.1
Re: [PATCH iproute2 v2 1/2] lib/fs: fix memory leak in get_task_name()
In reply to Andrea Claudiinclude/utils.h char *find_cgroup2_mount(bool do_mount); · line 311
Quoted 11 lines · Andrea Claudi
include/utils.h +1
-1@@ -307,7 +307,7 @@ char *find_cgroup2_mount(bool do_mount);__u64 get_cgroup2_id (const char *path);char *get_cgroup2_path (__u64 id,bool full);int get_command_name (const char *pid,char *comm,size_t len); -char *get_task_name (pid_t pid); +int get_task_name (pid_t pid,char *name);
changing to an API with an assumed length is not better than the current situation. Why not just fixup the callers as needed to free the allocation?
Re: [PATCH iproute2 v2 1/2] lib/fs: fix memory leak in get_task_name()
In reply to Leon RomanovskyHi Leon,
Thanks for your review.
Quoted 7 lines · Leon Romanovsky
Quoted 2 lines · Andrea Claudi
asprintf()allocates memory which is not freed on the error path ofget_task_name(), thus potentially leading to memory leaks.Not really, memory is released when the application exits, which is the case here.
That's certainly true. However this is still a leak in the time frame between get_task_name function call and the application exit. For example:
$ ip -d -b - << EOF tuntap show monitor EOF
calls get_task_name one or more time (once for each tun interface), and leaks memory indefinitely, if ip is not interrupted in some way.
Of course this is a corner case, and the leaks should anyway be small. However I cannot see this as a good reason not to fix it.
Quoted 9 lines
Quoted 6 lines
Rework
get_task_name()and avoidasprintf()usage and memory allocation, returning the task string to the caller using an additional char* parameter.Fixes 81bfd01a4c9e ("lib: move get_task_name() from rdma")As I was told before, in netdev Fixes means that it is a bug that affects users. This is not the case here.
Thanks for letting me know. I usually rely a lot on Fixes: as iproute2 package maintainer, but I'll change my habits if this is the common understanding. Stephen, David, WDYT?
Quoted 309 lines
Quoted 15 lines
Signed-off-by Andrea Claudi <[email protected]>--- include/utils.h | 2 +- ip/iptuntap.c | 17 ++++++++++------- lib/fs.c | 20 ++++++++++---------- rdma/res-cmid.c | 8 +++++--- rdma/res-cq.c | 8 +++++--- rdma/res-ctx.c | 7 ++++--- rdma/res-mr.c | 7 ++++--- rdma/res-pd.c | 8 +++++--- rdma/res-qp.c | 7 ++++--- rdma/res-srq.c | 7 ++++--- rdma/stat.c | 5 ++++- 11 files changed, 56 insertions(+), 40 deletions(-)Honestly, I don't see any need in both patches.
Thanks
Quoted 288 lines
include/utils.h +1
-1@@ -307,7 +307,7 @@ char *find_cgroup2_mount(bool do_mount);__u64 get_cgroup2_id (const char *path);char *get_cgroup2_path (__u64 id,bool full);int get_command_name (const char *pid,char *comm,size_t len); -char *get_task_name (pid_t pid); +int get_task_name (pid_t pid,char *name);int get_rtnl_link_stats_rta (struct rtnl_link_stats64 *stats64,struct rtattr *tb[]);ip/iptuntap.c +10
-7@@ -321,14 +321,17 @@ static void show_processes(const char *name) }else if (err ==2 && !strcmp ("iff" , key) && !strcmp (name, value)) { -char *pname =get_task_name (pid); - -print_string (PRINT_ANY ,"name" , -"%s" , pname ? :"<NULL>" ); +SPRINT_BUF (pname); + +if (get_task_name (pid, pname)) { +print_string (PRINT_ANY ,"name" , +"%s" ,"<NULL>" ); + }else { +print_string (PRINT_ANY ,"name" , +"%s" , pname); + } -print_uint (PRINT_ANY ,"pid" , -"(%d)" , pid); -free (pname); +print_uint (PRINT_ANY ,"pid" ,"(%d)" , pid); }free (key);lib/fs.c +10
-10@@ -342,25 +342,25 @@ int get_command_name(const char *pid, char *comm, size_t len)return 0 ; } -char *get_task_name (pid_t pid) +int get_task_name (pid_t pid,char *name) { -char *comm; +char path[PATH_MAX ];FILE *f;if (!pid) -return NULL ; +return -1 ; -if (asprintf (&comm,"/proc/%d/comm" , pid) <0 ) -return NULL ; +if (snprintf (path,sizeof (path),"/proc/%d/comm" , pid) >=sizeof (path)) +return -1 ; - f =fopen (comm,"r" ); + f =fopen (path,"r" );if (!f) -return NULL ; +return -1 ; -if (fscanf (f,"%ms\n" , &comm) !=1 ) - comm =NULL ; +if (fscanf (f,"%s\n" , name) !=1 ) +return -1 ;fclose (f); -return comm; +return 0 ; }rdma/res-cmid.c +5
-3@@ -159,8 +159,11 @@ static int res_cm_id_line(struct rd *rd, const char *name, int idx, goto out;if (nla_line[RDMA_NLDEV_ATTR_RES_PID ]) { +SPRINT_BUF (b); + pid =mnl_attr_get_u32 (nla_line[RDMA_NLDEV_ATTR_RES_PID ]); - comm =get_task_name (pid); +if (!get_task_name (pid, b)) + comm = b; } if (rd_is_filtered_attr(rd, "pid", pid,@@ -199,8 +202,7 @@ static int res_cm_id_line(struct rd *rd, const char *name, int idx,print_driver_table (rd, nla_line[RDMA_NLDEV_ATTR_DRIVER ]);newline (rd); -out:if (nla_line[RDMA_NLDEV_ATTR_RES_PID ]) -free (comm); +out:return MNL_CB_OK ; }rdma/res-cq.c +5
-3@@ -84,8 +84,11 @@ static int res_cq_line(struct rd *rd, const char *name, int idx, goto out;if (nla_line[RDMA_NLDEV_ATTR_RES_PID ]) { +SPRINT_BUF (b); + pid =mnl_attr_get_u32 (nla_line[RDMA_NLDEV_ATTR_RES_PID ]); - comm =get_task_name (pid); +if (!get_task_name (pid, b)) + comm = b; } if (rd_is_filtered_attr(rd, "pid", pid,@@ -123,8 +126,7 @@ static int res_cq_line(struct rd *rd, const char *name, int idx,print_driver_table (rd, nla_line[RDMA_NLDEV_ATTR_DRIVER ]);newline (rd); -out:if (nla_line[RDMA_NLDEV_ATTR_RES_PID ]) -free (comm); +out:return MNL_CB_OK ; }rdma/res-ctx.c +4
-3@@ -18,8 +18,11 @@ static int res_ctx_line(struct rd *rd, const char *name, int idx,return MNL_CB_ERROR ;if (nla_line[RDMA_NLDEV_ATTR_RES_PID ]) { +SPRINT_BUF (b); + pid =mnl_attr_get_u32 (nla_line[RDMA_NLDEV_ATTR_RES_PID ]); - comm =get_task_name (pid); +if (!get_task_name (pid, b)) + comm = b; } if (rd_is_filtered_attr(rd, "pid", pid,@@ -48,8 +51,6 @@ static int res_ctx_line(struct rd *rd, const char *name, int idx,newline (rd); out: -if (nla_line[RDMA_NLDEV_ATTR_RES_PID ]) -free (comm);return MNL_CB_OK ; }rdma/res-mr.c +4
-3@@ -47,8 +47,11 @@ static int res_mr_line(struct rd *rd, const char *name, int idx, goto out;if (nla_line[RDMA_NLDEV_ATTR_RES_PID ]) { +SPRINT_BUF (b); + pid =mnl_attr_get_u32 (nla_line[RDMA_NLDEV_ATTR_RES_PID ]); - comm =get_task_name (pid); +if (!get_task_name (pid, b)) + comm = b; } if (rd_is_filtered_attr(rd, "pid", pid,@@ -87,8 +90,6 @@ static int res_mr_line(struct rd *rd, const char *name, int idx,newline (rd); out: -if (nla_line[RDMA_NLDEV_ATTR_RES_PID ]) -free (comm);return MNL_CB_OK ; }rdma/res-pd.c +5
-3@@ -34,8 +34,11 @@ static int res_pd_line(struct rd *rd, const char *name, int idx, nla_line[RDMA_NLDEV_ATTR_RES_UNSAFE_GLOBAL_RKEY]);if (nla_line[RDMA_NLDEV_ATTR_RES_PID ]) { +SPRINT_BUF (b); + pid =mnl_attr_get_u32 (nla_line[RDMA_NLDEV_ATTR_RES_PID ]); - comm =get_task_name (pid); +if (!get_task_name (pid, b)) + comm = b; } if (rd_is_filtered_attr(rd, "pid", pid,@@ -76,8 +79,7 @@ static int res_pd_line(struct rd *rd, const char *name, int idx,print_driver_table (rd, nla_line[RDMA_NLDEV_ATTR_DRIVER ]);newline (rd); -out:if (nla_line[RDMA_NLDEV_ATTR_RES_PID ]) -free (comm); +out:return MNL_CB_OK ; }rdma/res-qp.c +4
-3@@ -146,8 +146,11 @@ static int res_qp_line(struct rd *rd, const char *name, int idx, goto out;if (nla_line[RDMA_NLDEV_ATTR_RES_PID ]) { +SPRINT_BUF (b); + pid =mnl_attr_get_u32 (nla_line[RDMA_NLDEV_ATTR_RES_PID ]); - comm =get_task_name (pid); +if (!get_task_name (pid, b)) + comm = b; } if (rd_is_filtered_attr(rd, "pid", pid,@@ -179,8 +182,6 @@ static int res_qp_line(struct rd *rd, const char *name, int idx,print_driver_table (rd, nla_line[RDMA_NLDEV_ATTR_DRIVER ]);newline (rd); out: -if (nla_line[RDMA_NLDEV_ATTR_RES_PID ]) -free (comm);return MNL_CB_OK ; }rdma/res-srq.c +4
-3@@ -174,8 +174,11 @@ static int res_srq_line(struct rd *rd, const char *name, int idx,return MNL_CB_ERROR ;if (nla_line[RDMA_NLDEV_ATTR_RES_PID ]) { +SPRINT_BUF (b); + pid =mnl_attr_get_u32 (nla_line[RDMA_NLDEV_ATTR_RES_PID ]); - comm =get_task_name (pid); +if (!get_task_name (pid, b)) + comm = b; }if (rd_is_filtered_attr (rd,"pid" , pid, nla_line[RDMA_NLDEV_ATTR_RES_PID ]))@@ -228,8 +231,6 @@ static int res_srq_line(struct rd *rd, const char *name, int idx,newline (rd); out: -if (nla_line[RDMA_NLDEV_ATTR_RES_PID ]) -free (comm);return MNL_CB_OK ; }rdma/stat.c +4
-1@@ -248,8 +248,11 @@ static int res_counter_line(struct rd *rd, const char *name, int index,return MNL_CB_OK ;if (nla_line[RDMA_NLDEV_ATTR_RES_PID ]) { +SPRINT_BUF (b); + pid =mnl_attr_get_u32 (nla_line[RDMA_NLDEV_ATTR_RES_PID ]); - comm =get_task_name (pid); +if (!get_task_name (pid, b)) + comm = b; }if (rd_is_filtered_attr (rd,"pid" , pid, nla_line[RDMA_NLDEV_ATTR_RES_PID ]))Signature · 1 line
2.35.1
Re: [PATCH iproute2 v2 1/2] lib/fs: fix memory leak in get_task_name()
Quoted 3 lines · Andrea Claudi
Thanks for letting me know. I usually rely a lot on Fixes: as iproute2 package maintainer, but I'll change my habits if this is the common understanding. Stephen, David, WDYT?
I think a Fixes tag is relevant here.
Re: [PATCH iproute2 v2 1/2] lib/fs: fix memory leak in get_task_name()
In reply to David AhernQuoted 15 lines · On Mon, 7 Mar 2022 10:58:37 -0700 David Ahern <[email protected]> wrote:
include/utils.h char *find_cgroup2_mount(bool do_mount); · line 311
Quoted 11 lines · Andrea Claudi
include/utils.h +1
-1@@ -307,7 +307,7 @@ char *find_cgroup2_mount(bool do_mount);__u64 get_cgroup2_id (const char *path);char *get_cgroup2_path (__u64 id,bool full);int get_command_name (const char *pid,char *comm,size_t len); -char *get_task_name (pid_t pid); +int get_task_name (pid_t pid,char *name);changing to an API with an assumed length is not better than the current situation. Why not just fixup the callers as needed to free the allocation?
I wonder if iproute2 should start using GCC attribute cleanup function (like systemd) or would this be too much of using a new thing?
Not sure if using the attribute (wrapped in macro) just hides the problem
Re: [PATCH iproute2 v2 1/2] lib/fs: fix memory leak in get_task_name()
In reply to David AhernQuoted 16 lines · David Ahern
include/utils.h char *find_cgroup2_mount(bool do_mount); · line 311
Quoted 11 lines · Andrea Claudi
include/utils.h +1
-1@@ -307,7 +307,7 @@ char *find_cgroup2_mount(bool do_mount);__u64 get_cgroup2_id (const char *path);char *get_cgroup2_path (__u64 id,bool full);int get_command_name (const char *pid,char *comm,size_t len); -char *get_task_name (pid_t pid); +int get_task_name (pid_t pid,char *name);changing to an API with an assumed length is not better than the current situation. Why not just fixup the callers as needed to free the allocation?
I actually did that on v1. After Stephen's comment about asprintf(), I got the idea to make get_task_name() similar to get_command_name() and a bit more "user-friendly", so that callers do not need a free().
If you think this is not ideal, I can post a v3 with the necessary fixes to the callers.
Thanks,
Andrea
Re: [PATCH iproute2 v2 1/2] lib/fs: fix memory leak in get_task_name()
Quoted 22 lines · On Mon, 7 Mar 2022 19:21:41 +0100 Andrea Claudi <[email protected]> wrote:
Quoted 16 lines · David Ahern
include/utils.h char *find_cgroup2_mount(bool do_mount); · line 311
Quoted 11 lines · Andrea Claudi
include/utils.h +1
-1@@ -307,7 +307,7 @@ char *find_cgroup2_mount(bool do_mount);__u64 get_cgroup2_id (const char *path);char *get_cgroup2_path (__u64 id,bool full);int get_command_name (const char *pid,char *comm,size_t len); -char *get_task_name (pid_t pid); +int get_task_name (pid_t pid,char *name);changing to an API with an assumed length is not better than the current situation. Why not just fixup the callers as needed to free the allocation?
I actually did that on v1. After Stephen's comment about
asprintf(), I got the idea to makeget_task_name()similar toget_command_name()and a bit more "user-friendly", so that callers do not need afree().
Quoted 6 lines
If you think this is not ideal, I can post a v3 with the necessary fixes to the callers.
Thanks,
Andrea
My comment was purely a suggestion not a requirement.
I have just had issues with complaints from compiler about code not checking return value from asprintf, so tend to avoid it.
Re: [PATCH iproute2 v2 1/2] lib/fs: fix memory leak in get_task_name()
In reply to Andrea ClaudiQuoted 18 lines · Andrea Claudi
Quoted 16 lines · David Ahern
include/utils.h char *find_cgroup2_mount(bool do_mount); · line 311
Quoted 11 lines · Andrea Claudi
include/utils.h +1
-1@@ -307,7 +307,7 @@ char *find_cgroup2_mount(bool do_mount);__u64 get_cgroup2_id (const char *path);char *get_cgroup2_path (__u64 id,bool full);int get_command_name (const char *pid,char *comm,size_t len); -char *get_task_name (pid_t pid); +int get_task_name (pid_t pid,char *name);changing to an API with an assumed length is not better than the current situation. Why not just fixup the callers as needed to free the allocation?
Quoted 4 lines
I actually did that on v1. After Stephen's comment about
asprintf(), I got the idea to makeget_task_name()similar toget_command_name()and a bit more "user-friendly", so that callers do not need afree().
get_command_name passes a buffer and length. That's a better API - no assumptions.
Re: [PATCH iproute2 v2 1/2] lib/fs: fix memory leak in get_task_name()
In reply to Andrea ClaudiQuoted 32 lines · Andrea Claudi
Hi Leon,
Thanks for your review.Quoted 7 lines · Leon Romanovsky
Quoted 2 lines · Andrea Claudi
asprintf()allocates memory which is not freed on the error path ofget_task_name(), thus potentially leading to memory leaks.Not really, memory is released when the application exits, which is the case here.
That's certainly true. However this is still a leak in the time frame between
get_task_namefunction call and the application exit. For example:$ ip -d -b - << EOF tuntap show monitor EOFcalls
get_task_nameone or more time (once for each tun interface), and leaks memory indefinitely, if ip is not interrupted in some way.Of course this is a corner case, and the leaks should anyway be small. However I cannot see this as a good reason not to fix it.
Quoted 5 lines
Quoted 5 lines
Rework
get_task_name()and avoidasprintf()usage and memory allocation, returning the task string to the caller using an additional char* parameter.
Quoted 14 lines
Quoted 4 lines
Quoted 1 line
Fixes 81bfd01a4c9e ("lib: move get_task_name() from rdma")As I was told before, in netdev Fixes means that it is a bug that affects users. This is not the case here.
Thanks for letting me know. I usually rely a lot on Fixes: as iproute2 package maintainer, but I'll change my habits if this is the common understanding. Stephen, David, WDYT?
Quoted 5 lines
Quoted 5 lines
Signed-off-by Andrea Claudi <[email protected]>--- include/utils.h | 2 +- ip/iptuntap.c | 17 ++++++++++-------
Ahh, sorry, I missed this user.
So yes, Fixes is needed here.
Thanks
Quoted 305 lines
Quoted 304 lines
Quoted 10 lines
lib/fs.c | 20 ++++++++++---------- rdma/res-cmid.c | 8 +++++--- rdma/res-cq.c | 8 +++++--- rdma/res-ctx.c | 7 ++++--- rdma/res-mr.c | 7 ++++--- rdma/res-pd.c | 8 +++++--- rdma/res-qp.c | 7 ++++--- rdma/res-srq.c | 7 ++++--- rdma/stat.c | 5 ++++- 11 files changed, 56 insertions(+), 40 deletions(-)Honestly, I don't see any need in both patches.
Thanks
Quoted 288 lines
include/utils.h +1
-1@@ -307,7 +307,7 @@ char *find_cgroup2_mount(bool do_mount);__u64 get_cgroup2_id (const char *path);char *get_cgroup2_path (__u64 id,bool full);int get_command_name (const char *pid,char *comm,size_t len); -char *get_task_name (pid_t pid); +int get_task_name (pid_t pid,char *name);int get_rtnl_link_stats_rta (struct rtnl_link_stats64 *stats64,struct rtattr *tb[]);ip/iptuntap.c +10
-7@@ -321,14 +321,17 @@ static void show_processes(const char *name) }else if (err ==2 && !strcmp ("iff" , key) && !strcmp (name, value)) { -char *pname =get_task_name (pid); - -print_string (PRINT_ANY ,"name" , -"%s" , pname ? :"<NULL>" ); +SPRINT_BUF (pname); + +if (get_task_name (pid, pname)) { +print_string (PRINT_ANY ,"name" , +"%s" ,"<NULL>" ); + }else { +print_string (PRINT_ANY ,"name" , +"%s" , pname); + } -print_uint (PRINT_ANY ,"pid" , -"(%d)" , pid); -free (pname); +print_uint (PRINT_ANY ,"pid" ,"(%d)" , pid); }free (key);lib/fs.c +10
-10@@ -342,25 +342,25 @@ int get_command_name(const char *pid, char *comm, size_t len)return 0 ; } -char *get_task_name (pid_t pid) +int get_task_name (pid_t pid,char *name) { -char *comm; +char path[PATH_MAX ];FILE *f;if (!pid) -return NULL ; +return -1 ; -if (asprintf (&comm,"/proc/%d/comm" , pid) <0 ) -return NULL ; +if (snprintf (path,sizeof (path),"/proc/%d/comm" , pid) >=sizeof (path)) +return -1 ; - f =fopen (comm,"r" ); + f =fopen (path,"r" );if (!f) -return NULL ; +return -1 ; -if (fscanf (f,"%ms\n" , &comm) !=1 ) - comm =NULL ; +if (fscanf (f,"%s\n" , name) !=1 ) +return -1 ;fclose (f); -return comm; +return 0 ; }rdma/res-cmid.c +5
-3@@ -159,8 +159,11 @@ static int res_cm_id_line(struct rd *rd, const char *name, int idx, goto out;if (nla_line[RDMA_NLDEV_ATTR_RES_PID ]) { +SPRINT_BUF (b); + pid =mnl_attr_get_u32 (nla_line[RDMA_NLDEV_ATTR_RES_PID ]); - comm =get_task_name (pid); +if (!get_task_name (pid, b)) + comm = b; } if (rd_is_filtered_attr(rd, "pid", pid,@@ -199,8 +202,7 @@ static int res_cm_id_line(struct rd *rd, const char *name, int idx,print_driver_table (rd, nla_line[RDMA_NLDEV_ATTR_DRIVER ]);newline (rd); -out:if (nla_line[RDMA_NLDEV_ATTR_RES_PID ]) -free (comm); +out:return MNL_CB_OK ; }rdma/res-cq.c +5
-3@@ -84,8 +84,11 @@ static int res_cq_line(struct rd *rd, const char *name, int idx, goto out;if (nla_line[RDMA_NLDEV_ATTR_RES_PID ]) { +SPRINT_BUF (b); + pid =mnl_attr_get_u32 (nla_line[RDMA_NLDEV_ATTR_RES_PID ]); - comm =get_task_name (pid); +if (!get_task_name (pid, b)) + comm = b; } if (rd_is_filtered_attr(rd, "pid", pid,@@ -123,8 +126,7 @@ static int res_cq_line(struct rd *rd, const char *name, int idx,print_driver_table (rd, nla_line[RDMA_NLDEV_ATTR_DRIVER ]);newline (rd); -out:if (nla_line[RDMA_NLDEV_ATTR_RES_PID ]) -free (comm); +out:return MNL_CB_OK ; }rdma/res-ctx.c +4
-3@@ -18,8 +18,11 @@ static int res_ctx_line(struct rd *rd, const char *name, int idx,return MNL_CB_ERROR ;if (nla_line[RDMA_NLDEV_ATTR_RES_PID ]) { +SPRINT_BUF (b); + pid =mnl_attr_get_u32 (nla_line[RDMA_NLDEV_ATTR_RES_PID ]); - comm =get_task_name (pid); +if (!get_task_name (pid, b)) + comm = b; } if (rd_is_filtered_attr(rd, "pid", pid,@@ -48,8 +51,6 @@ static int res_ctx_line(struct rd *rd, const char *name, int idx,newline (rd); out: -if (nla_line[RDMA_NLDEV_ATTR_RES_PID ]) -free (comm);return MNL_CB_OK ; }rdma/res-mr.c +4
-3@@ -47,8 +47,11 @@ static int res_mr_line(struct rd *rd, const char *name, int idx, goto out;if (nla_line[RDMA_NLDEV_ATTR_RES_PID ]) { +SPRINT_BUF (b); + pid =mnl_attr_get_u32 (nla_line[RDMA_NLDEV_ATTR_RES_PID ]); - comm =get_task_name (pid); +if (!get_task_name (pid, b)) + comm = b; } if (rd_is_filtered_attr(rd, "pid", pid,@@ -87,8 +90,6 @@ static int res_mr_line(struct rd *rd, const char *name, int idx,newline (rd); out: -if (nla_line[RDMA_NLDEV_ATTR_RES_PID ]) -free (comm);return MNL_CB_OK ; }rdma/res-pd.c +5
-3@@ -34,8 +34,11 @@ static int res_pd_line(struct rd *rd, const char *name, int idx, nla_line[RDMA_NLDEV_ATTR_RES_UNSAFE_GLOBAL_RKEY]);if (nla_line[RDMA_NLDEV_ATTR_RES_PID ]) { +SPRINT_BUF (b); + pid =mnl_attr_get_u32 (nla_line[RDMA_NLDEV_ATTR_RES_PID ]); - comm =get_task_name (pid); +if (!get_task_name (pid, b)) + comm = b; } if (rd_is_filtered_attr(rd, "pid", pid,@@ -76,8 +79,7 @@ static int res_pd_line(struct rd *rd, const char *name, int idx,print_driver_table (rd, nla_line[RDMA_NLDEV_ATTR_DRIVER ]);newline (rd); -out:if (nla_line[RDMA_NLDEV_ATTR_RES_PID ]) -free (comm); +out:return MNL_CB_OK ; }rdma/res-qp.c +4
-3@@ -146,8 +146,11 @@ static int res_qp_line(struct rd *rd, const char *name, int idx, goto out;if (nla_line[RDMA_NLDEV_ATTR_RES_PID ]) { +SPRINT_BUF (b); + pid =mnl_attr_get_u32 (nla_line[RDMA_NLDEV_ATTR_RES_PID ]); - comm =get_task_name (pid); +if (!get_task_name (pid, b)) + comm = b; } if (rd_is_filtered_attr(rd, "pid", pid,@@ -179,8 +182,6 @@ static int res_qp_line(struct rd *rd, const char *name, int idx,print_driver_table (rd, nla_line[RDMA_NLDEV_ATTR_DRIVER ]);newline (rd); out: -if (nla_line[RDMA_NLDEV_ATTR_RES_PID ]) -free (comm);return MNL_CB_OK ; }rdma/res-srq.c +4
-3@@ -174,8 +174,11 @@ static int res_srq_line(struct rd *rd, const char *name, int idx,return MNL_CB_ERROR ;if (nla_line[RDMA_NLDEV_ATTR_RES_PID ]) { +SPRINT_BUF (b); + pid =mnl_attr_get_u32 (nla_line[RDMA_NLDEV_ATTR_RES_PID ]); - comm =get_task_name (pid); +if (!get_task_name (pid, b)) + comm = b; }if (rd_is_filtered_attr (rd,"pid" , pid, nla_line[RDMA_NLDEV_ATTR_RES_PID ]))@@ -228,8 +231,6 @@ static int res_srq_line(struct rd *rd, const char *name, int idx,newline (rd); out: -if (nla_line[RDMA_NLDEV_ATTR_RES_PID ]) -free (comm);return MNL_CB_OK ; }rdma/stat.c +4
-1@@ -248,8 +248,11 @@ static int res_counter_line(struct rd *rd, const char *name, int index,return MNL_CB_OK ;if (nla_line[RDMA_NLDEV_ATTR_RES_PID ]) { +SPRINT_BUF (b); + pid =mnl_attr_get_u32 (nla_line[RDMA_NLDEV_ATTR_RES_PID ]); - comm =get_task_name (pid); +if (!get_task_name (pid, b)) + comm = b; }if (rd_is_filtered_attr (rd,"pid" , pid, nla_line[RDMA_NLDEV_ATTR_RES_PID ]))Signature · 1 line
2.35.1