From patchwork Thu Nov 2 05:33:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qu Wenruo X-Patchwork-Id: 13443412 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 F1599C4167B for ; Thu, 2 Nov 2023 05:34:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1348556AbjKBFeU (ORCPT ); Thu, 2 Nov 2023 01:34:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38254 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1348533AbjKBFeT (ORCPT ); Thu, 2 Nov 2023 01:34:19 -0400 Received: from smtp-out1.suse.de (smtp-out1.suse.de [IPv6:2001:67c:2178:6::1c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 709B812C for ; Wed, 1 Nov 2023 22:34:13 -0700 (PDT) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id BF5FA215EE for ; Thu, 2 Nov 2023 05:34:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1698903250; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=k9MXKkOVGBXEnhTBHXJFuZSDwCpHr5r3pxdIhgYGGlU=; b=IZpFFGVfM2o+Xn4/NLLqPRLMXhxdRKdtThq+EHl4Yc7fZa9eMUh6ljnxHyVTU89bNpmt2C yzvZ/4dL10beCQ7e8lILZZ+KvVRoWJGCYZGDHYz5yIHcD4xYjcgcKufm6NzBaIaJD9074U mB5ou0YqI7zvvrH+PaQYVlinGldPKsg= Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id EC23D13460 for ; Thu, 2 Nov 2023 05:34:09 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id IFxVKdE0Q2U/AwAAMHmgww (envelope-from ) for ; Thu, 02 Nov 2023 05:34:09 +0000 From: Qu Wenruo To: linux-btrfs@vger.kernel.org Subject: [PATCH 1/3] btrfs-progs: subvolume create: handle failure for strdup() Date: Thu, 2 Nov 2023 16:03:48 +1030 Message-ID: <8c3df11d55b5add76a6abfd7896762697520a136.1698903010.git.wqu@suse.com> X-Mailer: git-send-email 2.42.0 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org The function strdup() can return NULL if the system is out of memory, thus we need to hanle the rare but possible -ENOMEM case. Signed-off-by: Qu Wenruo --- cmds/subvolume.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/cmds/subvolume.c b/cmds/subvolume.c index 8504c380c9ee..bccc4968dad3 100644 --- a/cmds/subvolume.c +++ b/cmds/subvolume.c @@ -194,8 +194,17 @@ static int cmd_subvolume_create(const struct cmd_struct *cmd, int argc, char **a } dupname = strdup(dst); + if (!dupname) { + error("out of memory when duplicating %s", dst); + goto out; + } newname = basename(dupname); + dupdir = strdup(dst); + if (!dupdir) { + error("out of memory when duplicating %s", dst); + goto out; + } dstdir = dirname(dupdir); if (!test_issubvolname(newname)) {