get_comm() clears errno and checks it immediately, without any operation that can set errno in between. The check can never be true. Print the concrete get_comm() failure reason where it is detected, and let callers handle a NULL return without guessing the error cause. Signed-off-by: Yichong Chen --- tools/mm/page_owner_sort.c | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/tools/mm/page_owner_sort.c b/tools/mm/page_owner_sort.c index 35d3d254941c..30b3cfc7eeab 100644 --- a/tools/mm/page_owner_sort.c +++ b/tools/mm/page_owner_sort.c @@ -391,17 +391,14 @@ static char *get_comm(char *buf) { char *comm_str = malloc(TASK_COMM_LEN); - if (!comm_str) + if (!comm_str) { + fprintf(stderr, "Out of memory\n"); return NULL; + } memset(comm_str, 0, TASK_COMM_LEN); if (search_pattern(&comm_pattern, comm_str, TASK_COMM_LEN, buf) < 0) { - free(comm_str); - return NULL; - } - errno = 0; - if (errno != 0) { if (debug_on) fprintf(stderr, "wrong comm in follow buf:\n%s\n", buf); free(comm_str); @@ -514,19 +511,15 @@ static bool add_list(char *buf, int len, char *ext_buf) return false; } filter_result = filter_record(buf); - if (filter_result == FILTER_ERROR) { - fprintf(stderr, "Out of memory\n"); + if (filter_result == FILTER_ERROR) return false; - } if (filter_result == FILTER_SKIP) return true; list[list_size].pid = get_pid(buf); list[list_size].tgid = get_tgid(buf); list[list_size].comm = get_comm(buf); - if (!list[list_size].comm) { - fprintf(stderr, "Out of memory\n"); + if (!list[list_size].comm) return false; - } list[list_size].txt = malloc(len + 1); if (!list[list_size].txt) { fprintf(stderr, "Out of memory\n"); -- 2.51.0