From patchwork Thu May 23 12:26:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13671724 Received: from wfhigh5-smtp.messagingengine.com (wfhigh5-smtp.messagingengine.com [64.147.123.156]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A0D1E13C693 for ; Thu, 23 May 2024 12:26:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=64.147.123.156 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716467199; cv=none; b=i0QGm68Q5sYmuzDkq8SolwrRTCfS9lq0Idqufm77SKdANymuqliLl/afPONBzfv/Q0rsuRc/vDIk9fGYBqI4O1biNbt4xYnK1Y93gH7AaEo9Rjr/YssBAXbIYIhmL1o6Ws+d0pm3NeGpEZ/2ER+f0X1dlavQxKuoCckH6s4TYEI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716467199; c=relaxed/simple; bh=e+Vv/VpZSuLk96UoC0bsZ8dTDUsFe/S9qDhp1jJ4FoY=; h=Date:From:To:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=jyEbTF83g6u3zfULsM5DHkpRLomNnRAJi8aUv26XvRf5HomZ8srEqICqTI5m5Gb9u/0os6Eg6LvYv9Z9Gb4KnBwd2Nn+nKMAGEs9TgV24d8xyULUFZlfvmLsISvOXzHOb7sLvv8kNbodcmLUsmHobl8g+gMU7oWxbABIazVYYiw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im; spf=pass smtp.mailfrom=pks.im; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b=pHJMXDUc; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=Y1ge12bR; arc=none smtp.client-ip=64.147.123.156 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pks.im Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b="pHJMXDUc"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="Y1ge12bR" Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailfhigh.west.internal (Postfix) with ESMTP id E138A18000F0 for ; Thu, 23 May 2024 08:26:36 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute5.internal (MEProxy); Thu, 23 May 2024 08:26:37 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=cc :content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm1; t=1716467196; x=1716553596; bh=wavjjhVUVG +JOOEXyAzqv/Y3AfBBKWZZvSsRA2pIiQo=; b=pHJMXDUc582UUDA/RkZWFoJb6d adBx+S3fjQ+45a5fxvON3iM6BDlgvO1WfzHFnKbjATbMmmYN1TwYnY0Ej9Cow67E Fu+T8VmBi2ny/wsi1uF/hWBgU92/awfDjbuG/MLcEeiSdHIYXaijq4LnbpRiybjt M7du9g+Diu574ZhMNpo89+w579QGabBqTa3PjHoLMiLJ5YOxldgqSrJsfghtmdZv ZyZs6lzJHQUh83qQYrGdqZtAc4aAWyrjQh57XQcbn9LkKwu3ZlX5JWIn3RqvHjCN 8Si2rsLVpWtH5IL+S3FsqosxfxA9lXtMe7/Gr5L0iwL+9yX7SRKDOwWIqcXg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm1; t=1716467196; x=1716553596; bh=wavjjhVUVG+JOOEXyAzqv/Y3AfBB KWZZvSsRA2pIiQo=; b=Y1ge12bRxi4Y7BIu+NkqTmhPwv0QeaORex4i8jN7/lIs P9F3aXQ9AssG3LIEfibW6kKoxTJ9+OLJujm90a4d6O5ph4M7MZ5Rj2Bigh7VCcjo ko8uCcUwHrV5RFiMccLljNfFf5F689sY+zdw4cWD2PS+JKDiN//zaDdhUGL/pHZl LrusS/bvfRFoxmWgDwIOqfVld+I/w2KqfJmamISgilh9UlH6rKkKfWRcLwOIrKkd BH8qusZmWF57xbeDnD1ZPvzZ9bfA6no5mgDNCIuHq23ZMJ4TiUnYcLdoDusATqG2 f+FeXM7P07Jdp09vIs2W6OH0rS1VW8gNDcQHFsielw== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrvdeiiedgheduucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpeffhffvuffkfhggtggujgesghdtre ertddtvdenucfhrhhomheprfgrthhrihgtkhcuufhtvghinhhhrghrughtuceophhssehp khhsrdhimheqnecuggftrfgrthhtvghrnhepheeghfdtfeeuffehkefgffduleffjedthf dvjeektdfhhedvlefgtefgvdettdfhnecuvehluhhsthgvrhfuihiivgepvdenucfrrghr rghmpehmrghilhhfrhhomhepphhssehpkhhsrdhimh X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA for ; Thu, 23 May 2024 08:26:35 -0400 (EDT) Received: by localhost (OpenSMTPD) with ESMTPSA id 0432fe6a (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO) for ; Thu, 23 May 2024 12:26:29 +0000 (UTC) Date: Thu, 23 May 2024 14:26:33 +0200 From: Patrick Steinhardt To: git@vger.kernel.org Subject: [PATCH 17/20] builtin/mv: refactor `add_slash()` to always return allocated strings Message-ID: <97470398ad943776577f18ad2e19bb8707e6dc46.1716465556.git.ps@pks.im> References: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: The `add_slash()` function will only conditionally return an allocated string when the passed-in string did not yet have a trailing slash. This makes the memory ownership harder to track than really necessary. It's dubious whether this optimization really buys us all that much. The number of times we execute this function is bounded by the number of arguments to git-mv(1), so in the typical case we may end up saving an allocation or two. Simplify the code to unconditionally return allocated strings. Signed-off-by: Patrick Steinhardt --- builtin/mv.c | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/builtin/mv.c b/builtin/mv.c index 74aa9746aa..9f4c75df04 100644 --- a/builtin/mv.c +++ b/builtin/mv.c @@ -76,7 +76,7 @@ static const char **internal_prefix_pathspec(const char *prefix, return result; } -static const char *add_slash(const char *path) +static char *add_slash(const char *path) { size_t len = strlen(path); if (len && path[len - 1] != '/') { @@ -86,7 +86,7 @@ static const char *add_slash(const char *path) with_slash[len] = 0; return with_slash; } - return path; + return xstrdup(path); } #define SUBMODULE_WITH_GITDIR ((const char *)1) @@ -111,7 +111,7 @@ static void prepare_move_submodule(const char *src, int first, static int index_range_of_same_dir(const char *src, int length, int *first_p, int *last_p) { - const char *src_w_slash = add_slash(src); + char *src_w_slash = add_slash(src); int first, last, len_w_slash = length + 1; first = index_name_pos(the_repository->index, src_w_slash, len_w_slash); @@ -124,8 +124,8 @@ static int index_range_of_same_dir(const char *src, int length, if (strncmp(path, src_w_slash, len_w_slash)) break; } - if (src_w_slash != src) - free((char *)src_w_slash); + + free(src_w_slash); *first_p = first; *last_p = last; return last - first; @@ -141,7 +141,7 @@ static int index_range_of_same_dir(const char *src, int length, static int empty_dir_has_sparse_contents(const char *name) { int ret = 0; - const char *with_slash = add_slash(name); + char *with_slash = add_slash(name); int length = strlen(with_slash); int pos = index_name_pos(the_repository->index, with_slash, length); @@ -159,8 +159,7 @@ static int empty_dir_has_sparse_contents(const char *name) } free_return: - if (with_slash != name) - free((char *)with_slash); + free(with_slash); return ret; } @@ -178,7 +177,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix) OPT_END(), }; const char **source, **destination, **dest_path, **submodule_gitfile; - const char *dst_w_slash; + char *dst_w_slash = NULL; const char **src_dir = NULL; int src_dir_nr = 0, src_dir_alloc = 0; struct strbuf a_src_dir = STRBUF_INIT; @@ -243,10 +242,6 @@ int cmd_mv(int argc, const char **argv, const char *prefix) dst_mode = SPARSE; } } - if (dst_w_slash != dest_path[0]) { - free((char *)dst_w_slash); - dst_w_slash = NULL; - } /* Checking */ for (i = 0; i < argc; i++) { @@ -265,12 +260,14 @@ int cmd_mv(int argc, const char **argv, const char *prefix) pos = index_name_pos(the_repository->index, src, length); if (pos < 0) { - const char *src_w_slash = add_slash(src); + char *src_w_slash = add_slash(src); if (!path_in_sparse_checkout(src_w_slash, the_repository->index) && empty_dir_has_sparse_contents(src)) { + free(src_w_slash); modes[i] |= SKIP_WORKTREE_DIR; goto dir_check; } + free(src_w_slash); /* only error if existence is expected. */ if (!(modes[i] & SPARSE)) bad = _("bad source"); @@ -310,7 +307,9 @@ int cmd_mv(int argc, const char **argv, const char *prefix) dir_check: if (S_ISDIR(st.st_mode)) { - int j, dst_len, n; + char *dst_with_slash; + size_t dst_with_slash_len; + int j, n; int first = index_name_pos(the_repository->index, src, length), last; if (first >= 0) { @@ -335,19 +334,21 @@ int cmd_mv(int argc, const char **argv, const char *prefix) REALLOC_ARRAY(modes, n); REALLOC_ARRAY(submodule_gitfile, n); - dst = add_slash(dst); - dst_len = strlen(dst); + dst_with_slash = add_slash(dst); + dst_with_slash_len = strlen(dst_with_slash); for (j = 0; j < last - first; j++) { const struct cache_entry *ce = the_repository->index->cache[first + j]; const char *path = ce->name; source[argc + j] = path; destination[argc + j] = - prefix_path(dst, dst_len, path + length + 1); + prefix_path(dst_with_slash, dst_with_slash_len, path + length + 1); memset(modes + argc + j, 0, sizeof(enum update_mode)); modes[argc + j] |= ce_skip_worktree(ce) ? SPARSE : INDEX; submodule_gitfile[argc + j] = NULL; } + + free(dst_with_slash); argc += last - first; goto act_on_entry; } @@ -565,6 +566,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix) COMMIT_LOCK | SKIP_IF_UNCHANGED)) die(_("Unable to write new index file")); + free(dst_w_slash); string_list_clear(&src_for_dst, 0); string_list_clear(&dirty_paths, 0); UNLEAK(source);