Git development
 help / color / mirror / Atom feed
From: Dominik Maier <dmnk@google.com>
To: git@vger.kernel.org
Cc: "René Scharfe" <l.s.r@web.de>,
	"Junio C Hamano" <gitster@pobox.com>,
	dmnk+artist@google.com
Subject: [PATCH v2 0/2] archive: defer --add-file stat() and fix zip extra-file prefix handling
Date: Tue, 24 Nov 2026 18:05:48 +0000	[thread overview]
Message-ID: <cover.1795543448.git.dmnk@google.com> (raw)
In-Reply-To: <20261119192311.2685319-1-dmnk@google.com>

Following René's suggestion on v1, patch 2/2 moves the --add-file
stat() check into parse_archive_args() after parse_options() so
--no-add-file can cancel an earlier --add-file and --add-virtual-file
works over --remote.

Testing --remote --add-virtual-file with --format=zip turned up an
out-of-bounds pointer advance in write_zip_entry() when --prefix is
longer than an extra or virtual file's path (path + args->baselen), so
patch 1/2 fixes that first.

Dominik Maier (2):
  archive-zip: fix prefix stripping for extra and virtual files
  archive: defer --add-file stat() until after option parsing

 archive-zip.c          |  5 +++--
 archive.c              | 28 +++++++++++++++++++++-------
 t/t5000-tar-tree.sh    | 24 ++++++++++++++++++++++++
 t/t5003-archive-zip.sh |  5 +++++
 4 files changed, 53 insertions(+), 9 deletions(-)

Range-diff against v1:
-:  ---------- > 1:  72c0d0c0a4 archive-zip: fix prefix stripping for extra and virtual files
1:  c51e62800c ! 2:  543109b748 archive: reject remote --add-file in callback
    @@ Metadata
     Author: Dominik Maier <dmnk@google.com>
     
      ## Commit message ##
    -    archive: reject remote --add-file in callback
    +    archive: defer --add-file stat() until after option parsing
     
    -    parse_archive_args() only checks "is_remote && args->extra_files.nr"
    -    after parse_options() returns. When git-upload-archive parses client
    -    arguments, add_file_cb() runs first and calls stat() on the requested
    -    path on the server filesystem, reporting "File not found" or "Not a
    -    regular file" before the "--remote" incompatibility check is reached
    -    (and succeeding altogether if "--no-add-file" follows).
    +    add_file_cb() calls stat() on each --add-file argument while
    +    parse_options() is still running, before parse_archive_args() checks
    +    is_remote. When git-upload-archive parses client arguments, it probes
    +    the requested path on the server filesystem and reports "File not found"
    +    or "Not a regular file" instead of rejecting --add-file with --remote.
     
    -    Pass is_remote into add_file_cb() and reject "--add-file" and
    -    "--add-virtual-file" immediately before touching the filesystem.
    +    Move the stat() and regular-file checks into parse_archive_args() after
    +    parse_options() finishes, refusing entries with a NULL content pointer
    +    when is_remote is set. This rejects remote --add-file before touching
    +    the filesystem, allows a later --no-add-file to cancel an earlier
    +    --add-file, and lets --add-virtual-file work with --remote since its
    +    content is supplied inline.
     
    +    Suggested-by: René Scharfe <l.s.r@web.de>
         Signed-off-by: Dominik Maier <dmnk@google.com>
     
      ## archive.c ##
    -@@ archive.c: static void extra_file_info_clear(void *util, const char *str UNUSED)
    - 	free(info);
    - }
    +@@ archive.c: static int add_file_cb(const struct option *opt, const char *arg, int unset)
      
    -+struct add_file_data {
    -+	struct archiver_args *args;
    -+	const char **basep;
    -+	int is_remote;
    -+};
    -+
    - static int add_file_cb(const struct option *opt, const char *arg, int unset)
    - {
    --	struct archiver_args *args = opt->value;
    --	const char **basep = (const char **)opt->defval;
    --	const char *base = *basep;
    -+	struct add_file_data *data = opt->value;
    -+	struct archiver_args *args = data->args;
    -+	const char *base = *data->basep;
    - 	char *path;
    - 	struct string_list_item *item;
    - 	struct extra_file_info *info;
    - 
    -+	if (data->is_remote)
    -+		die(_("options '%s' and '%s' cannot be used together"),
    -+		    !strcmp(opt->long_name, "add-virtual-file") ?
    -+		    "--add-virtual-file" : "--add-file",
    -+		    "--remote");
    -+
    - 	if (unset) {
    - 		string_list_clear_func(&args->extra_files,
    - 				       extra_file_info_clear);
    + 	if (!strcmp(opt->long_name, "add-file")) {
    + 		path = prefix_filename(args->prefix, arg);
    +-		if (stat(path, &info->stat))
    +-			die(_("File not found: %s"), path);
    +-		if (!S_ISREG(info->stat.st_mode))
    +-			die(_("Not a regular file: %s"), path);
    +-		info->content = NULL; /* read the file later */
    ++		memset(&info->stat, 0, sizeof(info->stat));
    ++		info->content = NULL; /* stat and read the file later */
    + 	} else if (!strcmp(opt->long_name, "add-virtual-file")) {
    + 		struct strbuf buf = STRBUF_INIT;
    + 		const char *p = arg;
     @@ archive.c: static int parse_archive_args(int argc, const char **argv,
    + 	const char *mtime_option = NULL;
    + 	int compression_level = -1;
    + 	int verbose = 0;
    ++	struct string_list_item *item;
      	int i;
      	int list = 0;
      	int worktree_attributes = 0;
    -+	struct add_file_data add_file_data = {
    -+		.args = args,
    -+		.basep = &base,
    -+		.is_remote = is_remote,
    -+	};
    - 	struct option opts[] = {
    - 		OPT_GROUP(""),
    - 		OPT_STRING(0, "format", &format, N_("fmt"), N_("archive format")),
    -@@ archive.c: static int parse_archive_args(int argc, const char **argv,
    - 		{
    - 			.type = OPTION_CALLBACK,
    - 			.long_name = "add-file",
    --			.value = args,
    -+			.value = &add_file_data,
    - 			.argh = N_("file"),
    - 			.help = N_("add untracked file to archive"),
    - 			.callback = add_file_cb,
    --			.defval = (intptr_t) &base,
    - 		},
    - 		{
    - 			.type = OPTION_CALLBACK,
    - 			.long_name = "add-virtual-file",
    --			.value = args,
    -+			.value = &add_file_data,
    - 			.argh = N_("path:content"),
    - 			.help = N_("add untracked file to archive"),
    - 			.callback = add_file_cb,
    --			.defval = (intptr_t) &base,
    - 		},
    - 		OPT_STRING('o', "output", &output, N_("file"),
    - 			N_("write the archive to this file")),
     @@ archive.c: static int parse_archive_args(int argc, const char **argv,
      		die(_("the option '%s' requires '%s'"), "--exec", "--remote");
      	if (output)
      		die(_("Unexpected option --output"));
     -	if (is_remote && args->extra_files.nr)
     -		die(_("options '%s' and '%s' cannot be used together"), "--add-file", "--remote");
    ++	for_each_string_list_item(item, &args->extra_files) {
    ++		const char *path = item->string;
    ++		struct extra_file_info *info = item->util;
    ++
    ++		if (info->content)
    ++			continue;
    ++		if (is_remote)
    ++			die(_("options '%s' and '%s' cannot be used together"),
    ++			    "--add-file", "--remote");
    ++		if (stat(path, &info->stat))
    ++			die(_("File not found: %s"), path);
    ++		if (!S_ISREG(info->stat.st_mode))
    ++			die(_("Not a regular file: %s"), path);
    ++	}
      
      	if (!base)
      		base = "";
    @@ t/t5000-tar-tree.sh: check_tar with_untracked2
      check_added with_untracked2 untracked one/untracked
      check_added with_untracked2 untracked two/untracked
      
    -+test_expect_success 'git archive --remote rejects --add-file before stat' '
    ++test_expect_success 'git archive --remote rejects --add-file without stat' '
     +	test_must_fail git archive --remote=. --add-file=no-such-file HEAD 2>err &&
     +	test_grep "cannot be used together" err &&
     +	test_grep ! "File not found" err &&
    -+	echo untracked >untracked &&
    -+	test_must_fail git archive --remote=. --add-file=untracked --no-add-file HEAD 2>err &&
    -+	test_grep "cannot be used together" err
    ++	test_must_fail git archive --remote=. --add-file=. HEAD 2>err &&
    ++	test_grep "cannot be used together" err &&
    ++	test_grep ! "Not a regular file" err
     +'
    ++
    ++test_expect_success 'git archive allows --no-add-file after missing --add-file' '
    ++	git archive --add-file=no-such-file --no-add-file HEAD >canceled.tar &&
    ++	test_cmp_bin b.tar canceled.tar &&
    ++	git archive --remote=. --add-file=no-such-file --no-add-file HEAD >remote-canceled.tar &&
    ++	test_cmp_bin b.tar remote-canceled.tar
    ++'
    ++
    ++test_expect_success 'git archive --remote with --add-virtual-file' '
    ++	printf virtual >expected-virtual &&
    ++	git archive --remote=. --add-virtual-file=virtual:virtual HEAD >remote_virtual.tar
    ++'
    ++
    ++check_tar remote_virtual
    ++check_added remote_virtual expected-virtual virtual
     +
      test_expect_success 'git archive on large files' '
      	test_config core.bigfilethreshold 1 &&
-- 
2.56.1.831.g720f3f5937-goog


  parent reply	other threads:[~2026-11-24 18:05 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-19 19:23 [PATCH] archive: reject remote --add-file in callback Dominik Maier
2026-11-21 15:15 ` René Scharfe
2026-11-24 18:05 ` Dominik Maier [this message]
2026-11-24 18:05   ` [PATCH v2 1/2] archive-zip: fix prefix stripping for extra and virtual files Dominik Maier
2026-11-25  0:09     ` Junio C Hamano
2026-11-25 11:39       ` René Scharfe
2026-11-25 17:42         ` Junio C Hamano
2026-11-24 18:05   ` [PATCH v2 2/2] archive: defer --add-file stat() until after option parsing Dominik Maier
2026-11-25  0:24     ` Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=cover.1795543448.git.dmnk@google.com \
    --to=dmnk@google.com \
    --cc=dmnk+artist@google.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=l.s.r@web.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox