From patchwork Wed Jun 7 09:30:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Weifeng Su X-Patchwork-Id: 13270297 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4416CC77B7A for ; Wed, 7 Jun 2023 09:31:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235807AbjFGJbG (ORCPT ); Wed, 7 Jun 2023 05:31:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49650 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240140AbjFGJaz (ORCPT ); Wed, 7 Jun 2023 05:30:55 -0400 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B223919BC for ; Wed, 7 Jun 2023 02:30:48 -0700 (PDT) Received: from canpemm500005.china.huawei.com (unknown [172.30.72.53]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4QbhsG0nt8zTkrY; Wed, 7 Jun 2023 17:30:26 +0800 (CST) Received: from Y00251687ALE274.china.huawei.com (10.174.178.198) by canpemm500005.china.huawei.com (7.192.104.229) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.23; Wed, 7 Jun 2023 17:30:46 +0800 From: Weifeng Su To: , , , CC: , , Weifeng Su Subject: [PATCH] libxcmd: add return value check for dynamic memory function Date: Wed, 7 Jun 2023 17:30:18 +0800 Message-ID: <20230607093018.61752-1-suweifeng1@huawei.com> X-Mailer: git-send-email 2.18.0.windows.1 MIME-Version: 1.0 X-Originating-IP: [10.174.178.198] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) To canpemm500005.china.huawei.com (7.192.104.229) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-xfs@vger.kernel.org The result check was missed and It cause the coredump like: 0x00005589f3e358dd in add_command (ci=0x5589f3e3f020 ) at command.c:37 0x00005589f3e337d8 in init_commands () at init.c:37 init (argc=, argv=0x7ffecfb0cd28) at init.c:102 0x00005589f3e33399 in main (argc=, argv=) at init.c:112 Add check for realloc function to ignore this coredump and exit with error output Signed-off-by: Weifeng Su Reviewed-by: Christoph Hellwig --- libxcmd/command.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/libxcmd/command.c b/libxcmd/command.c index a76d1515..47d050c3 100644 --- a/libxcmd/command.c +++ b/libxcmd/command.c @@ -34,6 +34,10 @@ add_command( const cmdinfo_t *ci) { cmdtab = realloc((void *)cmdtab, ++ncmds * sizeof(*cmdtab)); + if (!cmdtab) { + perror("realloc"); + exit(1); + } cmdtab[ncmds - 1] = *ci; qsort(cmdtab, ncmds, sizeof(*cmdtab), compare); }