git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: "brian m. carlson" <sandals@crustytoothpaste.net>
To: "René Scharfe" <l.s.r@web.de>
Cc: Git Mailing List <git@vger.kernel.org>,
	Junio C Hamano <gitster@pobox.com>
Subject: Re: [PATCH 2/2] sha1-name: check for overflow of N in "foo^N" and "foo~N"
Date: Sun, 15 Sep 2019 15:15:13 +0000	[thread overview]
Message-ID: <20190915151513.GU11334@genre.crustytoothpaste.net> (raw)
In-Reply-To: <2be6e3ee-209e-9cd1-eb43-284f9a8462b3@web.de>

[-- Attachment #1: Type: text/plain, Size: 1911 bytes --]

On 2019-09-15 at 12:10:28, René Scharfe wrote:
> Reject values that don't fit into an int, as get_parent() and
> get_nth_ancestor() cannot handle them.  That's better than potentially
> returning a random object.
> 
> If this restriction turns out to be too tight then we can switch to a
> wider data type, but we'd still have to check for overflow.

Certainly we want Git to perform as well as possible on large
repositories, but I doubt if it will scale to more than 2 billion
revisions, even with significant effort.  I think this restriction
should be fine.

> diff --git a/sha1-name.c b/sha1-name.c
> index c665e3f96d..7a047e9e2b 100644
> --- a/sha1-name.c
> +++ b/sha1-name.c
> @@ -1160,13 +1160,22 @@ static enum get_oid_result get_oid_1(struct repository *r,
>  	}
> 
>  	if (has_suffix) {
> -		int num = 0;
> +		unsigned int num = 0;
>  		int len1 = cp - name;
>  		cp++;
> -		while (cp < name + len)
> -			num = num * 10 + *cp++ - '0';
> +		while (cp < name + len) {
> +			unsigned int digit = *cp++ - '0';
> +			if (unsigned_mult_overflows(num, 10))
> +				return MISSING_OBJECT;
> +			num *= 10;
> +			if (unsigned_add_overflows(num, digit))
> +				return MISSING_OBJECT;

I was worried whether these functions only handled size_t or if they
also handle unsigned int, but I checked and they seem to be fine for any
unsigned type.

> +			num += digit;
> +		}
>  		if (!num && len1 == len - 1)
>  			num = 1;
> +		else if (num > INT_MAX)
> +			return MISSING_OBJECT;
>  		if (has_suffix == '^')
>  			return get_parent(r, name, len1, oid, num);
>  		/* else if (has_suffix == '~') -- goes without saying */

This approach seems reasonable.  I must admit some curiosity as to how
you discovered this issue, though.  Did you have a cat assisting you in
typing revisions?
-- 
brian m. carlson: Houston, Texas, US
OpenPGP: https://keybase.io/bk2204

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 868 bytes --]

  reply	other threads:[~2019-09-15 15:15 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-09-15 12:03 [PATCH 1/2] rev-parse: demonstrate overflow of N for "foo^N" and "foo~N" René Scharfe
2019-09-15 12:10 ` [PATCH 2/2] sha1-name: check for overflow of N in " René Scharfe
2019-09-15 15:15   ` brian m. carlson [this message]
2019-09-15 16:12     ` René Scharfe

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

  List information: http://vger.kernel.org/majordomo-info.html

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

  git send-email \
    --in-reply-to=20190915151513.GU11334@genre.crustytoothpaste.net \
    --to=sandals@crustytoothpaste.net \
    --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.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).