# [PATCH] create_delta_index: simplify condition always evaluating to true

14 messages from 2013-08-15 to 2013-08-17. Participants: Stefan Beller, Eric Sunshine, Junio C Hamano, Philip Oakley, Nicolas Pitre, brian m. carlson.
Thread: https://gitlist.dev/t/34704

## Stefan Beller, 2013-08-15 19:37

Subject: [PATCH] create_delta_index: simplify condition always evaluating to true
Message-ID: <1376595460-6546-1-git-send-email-stefanbeller@googlemail.com>
URL: https://gitlist.dev/e/1376595460-6546-1-git-send-email-stefanbeller%40googlemail.com

```
When checking the previous lines in that function, we can deduct that
hsize must always be smaller than (1u<<31), since 506049c7df2c6
(fix >4GiB source delta assertion failure), because the entries is
capped at an upper bound of 0xfffffffeU, so hsize contains a maximum
value of 0x3fffffff, which is smaller than (1u<<31), so i will never
be larger than 31.

Signed-off-by: Stefan Beller <stefanbeller@googlemail.com>
---
 diff-delta.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/diff-delta.c b/diff-delta.c
index 93385e1..54da95b 100644
--- a/diff-delta.c
+++ b/diff-delta.c
@@ -154,8 +154,15 @@ struct delta_index * create_delta_index(const void *buf, unsigned long bufsize)
 		 */
 		entries = 0xfffffffeU / RABIN_WINDOW;
 	}
+
+	/*
+	 * Do not check i < 31 in the loop, because the assignement
+	 * previous to the loop makes sure, hsize is definitely
+	 * smaller than 1<<31, hence the loop will always stop
+	 * before i exceeds 31 resulting in an infinite loop.
+	 */
 	hsize = entries / 4;
-	for (i = 4; (1u << i) < hsize && i < 31; i++);
+	for (i = 4; (1u << i) < hsize; i++);
 	hsize = 1 << i;
 	hmask = hsize - 1;
 
-- 
1.8.4.rc3.498.g5af1768

```

## Eric Sunshine, 2013-08-15 21:21

Subject: Re: [PATCH] create_delta_index: simplify condition always evaluating to true
Message-ID: <CAPig+cQmdPo4mo69DsDmUURcw+HbxkAoNEqY08qiuJs8S+=bvQ@mail.gmail.com>
URL: https://gitlist.dev/e/CAPig%2BcQmdPo4mo69DsDmUURcw%2BHbxkAoNEqY08qiuJs8S%2B%3DbvQ%40mail.gmail.com
In-Reply-To: <1376595460-6546-1-git-send-email-stefanbeller@googlemail.com>

```
On Thu, Aug 15, 2013 at 3:37 PM, Stefan Beller
<stefanbeller@googlemail.com> wrote:
> When checking the previous lines in that function, we can deduct that

s/deduct/deduce/

> hsize must always be smaller than (1u<<31), since 506049c7df2c6
> (fix >4GiB source delta assertion failure), because the entries is

s/the entries/entries/ reads a bit better.

> capped at an upper bound of 0xfffffffeU, so hsize contains a maximum
> value of 0x3fffffff, which is smaller than (1u<<31), so i will never

s/i/it/

> be larger than 31.
>
> Signed-off-by: Stefan Beller <stefanbeller@googlemail.com>
> ---
>  diff-delta.c | 9 ++++++++-
>  1 file changed, 8 insertions(+), 1 deletion(-)
>
> diff --git a/diff-delta.c b/diff-delta.c
> index 93385e1..54da95b 100644
> --- a/diff-delta.c
> +++ b/diff-delta.c
> @@ -154,8 +154,15 @@ struct delta_index * create_delta_index(const void *buf, unsigned long bufsize)
>                  */
>                 entries = 0xfffffffeU / RABIN_WINDOW;
>         }
> +
> +       /*
> +        * Do not check i < 31 in the loop, because the assignement
> +        * previous to the loop makes sure, hsize is definitely
> +        * smaller than 1<<31, hence the loop will always stop
> +        * before i exceeds 31 resulting in an infinite loop.
> +        */

This comment echoes the commit message, and indeed the explanation
makes more sense in that context since someone can read the entire
patch to see and understand the actual change. It may have less value
as an in-code comment.

>         hsize = entries / 4;
> -       for (i = 4; (1u << i) < hsize && i < 31; i++);
> +       for (i = 4; (1u << i) < hsize; i++);
>         hsize = 1 << i;
>         hmask = hsize - 1;
>
> --

```

## Stefan Beller, 2013-08-15 21:34

Subject: [PATCH] create_delta_index: simplify condition always evaluating to true
Message-ID: <1376602462-32339-1-git-send-email-stefanbeller@googlemail.com>
URL: https://gitlist.dev/e/1376602462-32339-1-git-send-email-stefanbeller%40googlemail.com
In-Reply-To: <CAPig+cQmdPo4mo69DsDmUURcw+HbxkAoNEqY08qiuJs8S+=bvQ@mail.gmail.com>

```
When checking the previous lines in that function, we can deduce that
hsize must always be smaller than (1u<<31), since 506049c7df2c6
(fix >4GiB source delta assertion failure), because entries is
capped at an upper bound of 0xfffffffeU, so hsize contains a maximum
value of 0x3fffffff, which is smaller than (1u<<31), so the value of
'i' will never be larger than 31.

Signed-off-by: Stefan Beller <stefanbeller@googlemail.com>
---

Eric, thanks for reviewing my patch.

I applied the first 2 proposals (deduce, entries), but I disagree on
the third, so I reformulated the sentence, as I really meant the variable
i and not it as a pronoun.

Do I understand right, you're suggesting to remove the 
source code comment? I did this now, but I have a bad feeling with it.

The change of this patch surely removes dead code as of now and makes it
more readable. But also it could become alive again, once somebody
changes things nearby and forgets about the assumption, hsize not
exceeding a certain size. That's why I put a comment in there, so 
the future changes nearby may be more careful.

Thanks,
Stefan


 diff-delta.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/diff-delta.c b/diff-delta.c
index 93385e1..3797ce6 100644
--- a/diff-delta.c
+++ b/diff-delta.c
@@ -155,7 +155,7 @@ struct delta_index * create_delta_index(const void *buf, unsigned long bufsize)
 		entries = 0xfffffffeU / RABIN_WINDOW;
 	}
 	hsize = entries / 4;
-	for (i = 4; (1u << i) < hsize && i < 31; i++);
+	for (i = 4; (1u << i) < hsize; i++);
 	hsize = 1 << i;
 	hmask = hsize - 1;
 
-- 
1.8.4.rc3.1.gc1ebd90

```

## Junio C Hamano, 2013-08-15 21:43

Subject: Re: [PATCH] create_delta_index: simplify condition always evaluating to true
Message-ID: <7vzjsipr3s.fsf@alter.siamese.dyndns.org>
URL: https://gitlist.dev/e/7vzjsipr3s.fsf%40alter.siamese.dyndns.org
In-Reply-To: <1376595460-6546-1-git-send-email-stefanbeller@googlemail.com>

```
Forwarding to the area expert...

Stefan Beller <stefanbeller@googlemail.com> writes:

> When checking the previous lines in that function, we can deduct that
> hsize must always be smaller than (1u<<31), since 506049c7df2c6
> (fix >4GiB source delta assertion failure), because the entries is
> capped at an upper bound of 0xfffffffeU, so hsize contains a maximum
> value of 0x3fffffff, which is smaller than (1u<<31), so i will never
> be larger than 31.
>
> Signed-off-by: Stefan Beller <stefanbeller@googlemail.com>
> ---
>  diff-delta.c | 9 ++++++++-
>  1 file changed, 8 insertions(+), 1 deletion(-)
>
> diff --git a/diff-delta.c b/diff-delta.c
> index 93385e1..54da95b 100644
> --- a/diff-delta.c
> +++ b/diff-delta.c
> @@ -154,8 +154,15 @@ struct delta_index * create_delta_index(const void *buf, unsigned long bufsize)
>  		 */
>  		entries = 0xfffffffeU / RABIN_WINDOW;
>  	}
> +
> +	/*
> +	 * Do not check i < 31 in the loop, because the assignement
> +	 * previous to the loop makes sure, hsize is definitely
> +	 * smaller than 1<<31, hence the loop will always stop
> +	 * before i exceeds 31 resulting in an infinite loop.
> +	 */
>  	hsize = entries / 4;
> -	for (i = 4; (1u << i) < hsize && i < 31; i++);
> +	for (i = 4; (1u << i) < hsize; i++);
>  	hsize = 1 << i;
>  	hmask = hsize - 1;

```

## Eric Sunshine, 2013-08-15 21:46

Subject: Re: [PATCH] create_delta_index: simplify condition always evaluating to true
Message-ID: <CAPig+cQ5Y9irLk=9Bhz09c=5yzZEcyMKn2kbhcrO_zDpgmkhGw@mail.gmail.com>
URL: https://gitlist.dev/e/CAPig%2BcQ5Y9irLk%3D9Bhz09c%3D5yzZEcyMKn2kbhcrO_zDpgmkhGw%40mail.gmail.com
In-Reply-To: <1376602462-32339-1-git-send-email-stefanbeller@googlemail.com>

```
On Thu, Aug 15, 2013 at 5:34 PM, Stefan Beller
<stefanbeller@googlemail.com> wrote:
> When checking the previous lines in that function, we can deduce that
> hsize must always be smaller than (1u<<31), since 506049c7df2c6
> (fix >4GiB source delta assertion failure), because entries is
> capped at an upper bound of 0xfffffffeU, so hsize contains a maximum
> value of 0x3fffffff, which is smaller than (1u<<31), so the value of
> 'i' will never be larger than 31.
>
> Signed-off-by: Stefan Beller <stefanbeller@googlemail.com>
> ---
>
> Eric, thanks for reviewing my patch.
>
> I applied the first 2 proposals (deduce, entries), but I disagree on
> the third, so I reformulated the sentence, as I really meant the variable
> i and not it as a pronoun.

Thanks. Adding the quotes around 'i' makes your meaning clear. Without
the quotes, apparently it was ambiguous, and my brain read it as a
misspelling of 'it'.

> Do I understand right, you're suggesting to remove the
> source code comment? I did this now, but I have a bad feeling with it.
>
> The change of this patch surely removes dead code as of now and makes it
> more readable. But also it could become alive again, once somebody
> changes things nearby and forgets about the assumption, hsize not
> exceeding a certain size. That's why I put a comment in there, so
> the future changes nearby may be more careful.

Indeed, I feel uncomfortable with the patch in general for the very
reason that you state: it might become live again. Without the patch,
the code remains safe without any extra effort. With this patch, even
with the in-code comment, someone making changes needs to take special
care. Sometimes it makes sense to leave safeties in place, even if
they can't be triggered _today_; and safeties (such as i < 31) also
serve as documentation.

>
> Thanks,
> Stefan
>
>
>  diff-delta.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/diff-delta.c b/diff-delta.c
> index 93385e1..3797ce6 100644
> --- a/diff-delta.c
> +++ b/diff-delta.c
> @@ -155,7 +155,7 @@ struct delta_index * create_delta_index(const void *buf, unsigned long bufsize)
>                 entries = 0xfffffffeU / RABIN_WINDOW;
>         }
>         hsize = entries / 4;
> -       for (i = 4; (1u << i) < hsize && i < 31; i++);
> +       for (i = 4; (1u << i) < hsize; i++);
>         hsize = 1 << i;
>         hmask = hsize - 1;
>
> --
> 1.8.4.rc3.1.gc1ebd90
>

```

## Stefan Beller, 2013-08-15 22:04

Subject: Re: [PATCH] create_delta_index: simplify condition always evaluating to true
Message-ID: <520D5069.5090905@googlemail.com>
URL: https://gitlist.dev/e/520D5069.5090905%40googlemail.com
In-Reply-To: <7vzjsipr3s.fsf@alter.siamese.dyndns.org>

```
Nicolas,

I am sorry for not including you in the first mail.
Just before Junio included you, there were these 2 mails
http://www.mail-archive.com/git@vger.kernel.org/msg34101.html
http://www.mail-archive.com/git@vger.kernel.org/msg34103.html

Stefan

On 08/15/2013 11:43 PM, Junio C Hamano wrote:
> Forwarding to the area expert...
> 
> Stefan Beller <stefanbeller@googlemail.com> writes:
> 
>> When checking the previous lines in that function, we can deduct that
>> hsize must always be smaller than (1u<<31), since 506049c7df2c6
>> (fix >4GiB source delta assertion failure), because the entries is
>> capped at an upper bound of 0xfffffffeU, so hsize contains a maximum
>> value of 0x3fffffff, which is smaller than (1u<<31), so i will never
>> be larger than 31.
>>
>> Signed-off-by: Stefan Beller <stefanbeller@googlemail.com>
>> ---
>>  diff-delta.c | 9 ++++++++-
>>  1 file changed, 8 insertions(+), 1 deletion(-)
>>
>> diff --git a/diff-delta.c b/diff-delta.c
>> index 93385e1..54da95b 100644
>> --- a/diff-delta.c
>> +++ b/diff-delta.c
>> @@ -154,8 +154,15 @@ struct delta_index * create_delta_index(const void *buf, unsigned long bufsize)
>>  		 */
>>  		entries = 0xfffffffeU / RABIN_WINDOW;
>>  	}
>> +
>> +	/*
>> +	 * Do not check i < 31 in the loop, because the assignement
>> +	 * previous to the loop makes sure, hsize is definitely
>> +	 * smaller than 1<<31, hence the loop will always stop
>> +	 * before i exceeds 31 resulting in an infinite loop.
>> +	 */
>>  	hsize = entries / 4;
>> -	for (i = 4; (1u << i) < hsize && i < 31; i++);
>> +	for (i = 4; (1u << i) < hsize; i++);
>>  	hsize = 1 << i;
>>  	hmask = hsize - 1;



```

## Philip Oakley, 2013-08-15 22:14

Subject: Re: [PATCH] create_delta_index: simplify condition always evaluating to true
Message-ID: <0F8B752CC3F241A9874E8A268A25D162@PhilipOakley>
URL: https://gitlist.dev/e/0F8B752CC3F241A9874E8A268A25D162%40PhilipOakley
In-Reply-To: <1376602462-32339-1-git-send-email-stefanbeller@googlemail.com>

```
From: "Stefan Beller" <stefanbeller@googlemail.com>
> When checking the previous lines in that function, we can deduce that
> hsize must always be smaller than (1u<<31), since 506049c7df2c6
> (fix >4GiB source delta assertion failure), because entries is
> capped at an upper bound of 0xfffffffeU, so hsize contains a maximum
> value of 0x3fffffff, which is smaller than (1u<<31), so the value of
> 'i' will never be larger than 31.
>
> Signed-off-by: Stefan Beller <stefanbeller@googlemail.com>
> ---
>
> Eric, thanks for reviewing my patch.
>
> I applied the first 2 proposals (deduce, entries), but I disagree on
> the third, so I reformulated the sentence, as I really meant the 
> variable
> i and not it as a pronoun.
>
> Do I understand right, you're suggesting to remove the
> source code comment? I did this now, but I have a bad feeling with it.
>
> The change of this patch surely removes dead code as of now and makes 
> it
> more readable. But also it could become alive again, once somebody
> changes things nearby and forgets about the assumption, hsize not
> exceeding a certain size. That's why I put a comment in there, so
> the future changes nearby may be more careful.

Should the comment also include a note about potential undefined 
behaviour of a large shift, with the possible consequential bad compiler 
optimization, such as simply deleting the code. I understand the initial 
spot was part of the STACK tool check of such latent optimisation bugs.

http://css.csail.mit.edu/stack/

>
> Thanks,
> Stefan
>
>
> diff-delta.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/diff-delta.c b/diff-delta.c
> index 93385e1..3797ce6 100644
> --- a/diff-delta.c
> +++ b/diff-delta.c
> @@ -155,7 +155,7 @@ struct delta_index * create_delta_index(const void 
> *buf, unsigned long bufsize)
>  entries = 0xfffffffeU / RABIN_WINDOW;
>  }
>  hsize = entries / 4;
> - for (i = 4; (1u << i) < hsize && i < 31; i++);
> + for (i = 4; (1u << i) < hsize; i++);
>  hsize = 1 << i;
>  hmask = hsize - 1;
>
> -- 
> 1.8.4.rc3.1.gc1ebd90
>

```

## Nicolas Pitre, 2013-08-16 02:34

Subject: Re: [PATCH] create_delta_index: simplify condition always evaluating to true
Message-ID: <alpine.LFD.2.03.1308152231230.14472@syhkavp.arg>
URL: https://gitlist.dev/e/alpine.LFD.2.03.1308152231230.14472%40syhkavp.arg
In-Reply-To: <7vzjsipr3s.fsf@alter.siamese.dyndns.org>

```
On Thu, 15 Aug 2013, Junio C Hamano wrote:

> Forwarding to the area expert...
> 
> Stefan Beller <stefanbeller@googlemail.com> writes:
> 
> > When checking the previous lines in that function, we can deduct that
> > hsize must always be smaller than (1u<<31), since 506049c7df2c6
> > (fix >4GiB source delta assertion failure), because the entries is
> > capped at an upper bound of 0xfffffffeU, so hsize contains a maximum
> > value of 0x3fffffff, which is smaller than (1u<<31), so i will never
> > be larger than 31.
> >
> > Signed-off-by: Stefan Beller <stefanbeller@googlemail.com>

Acked-by: Nicolas Pitre <nico@fluxnic.net>

You probably could dispense with the comment.  The code is obvious 
enough and the commit log has the rationale already.

> > ---
> >  diff-delta.c | 9 ++++++++-
> >  1 file changed, 8 insertions(+), 1 deletion(-)
> >
> > diff --git a/diff-delta.c b/diff-delta.c
> > index 93385e1..54da95b 100644
> > --- a/diff-delta.c
> > +++ b/diff-delta.c
> > @@ -154,8 +154,15 @@ struct delta_index * create_delta_index(const void *buf, unsigned long bufsize)
> >  		 */
> >  		entries = 0xfffffffeU / RABIN_WINDOW;
> >  	}
> > +
> > +	/*
> > +	 * Do not check i < 31 in the loop, because the assignement
> > +	 * previous to the loop makes sure, hsize is definitely
> > +	 * smaller than 1<<31, hence the loop will always stop
> > +	 * before i exceeds 31 resulting in an infinite loop.
> > +	 */
> >  	hsize = entries / 4;
> > -	for (i = 4; (1u << i) < hsize && i < 31; i++);
> > +	for (i = 4; (1u << i) < hsize; i++);
> >  	hsize = 1 << i;
> >  	hmask = hsize - 1;
> --
> To unsubscribe from this list: send the line "unsubscribe git" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
> 

```

## Nicolas Pitre, 2013-08-16 02:38

Subject: Re: [PATCH] create_delta_index: simplify condition always evaluating to true
Message-ID: <alpine.LFD.2.03.1308152236250.14472@syhkavp.arg>
URL: https://gitlist.dev/e/alpine.LFD.2.03.1308152236250.14472%40syhkavp.arg
In-Reply-To: <CAPig+cQ5Y9irLk=9Bhz09c=5yzZEcyMKn2kbhcrO_zDpgmkhGw@mail.gmail.com>

```
On Thu, 15 Aug 2013, Eric Sunshine wrote:

> On Thu, Aug 15, 2013 at 5:34 PM, Stefan Beller
> <stefanbeller@googlemail.com> wrote:
> > When checking the previous lines in that function, we can deduce that
> > hsize must always be smaller than (1u<<31), since 506049c7df2c6
> > (fix >4GiB source delta assertion failure), because entries is
> > capped at an upper bound of 0xfffffffeU, so hsize contains a maximum
> > value of 0x3fffffff, which is smaller than (1u<<31), so the value of
> > 'i' will never be larger than 31.
> >
> > Signed-off-by: Stefan Beller <stefanbeller@googlemail.com>
> > ---
> >
> > Eric, thanks for reviewing my patch.
> >
> > I applied the first 2 proposals (deduce, entries), but I disagree on
> > the third, so I reformulated the sentence, as I really meant the variable
> > i and not it as a pronoun.
> 
> Thanks. Adding the quotes around 'i' makes your meaning clear. Without
> the quotes, apparently it was ambiguous, and my brain read it as a
> misspelling of 'it'.
> 
> > Do I understand right, you're suggesting to remove the
> > source code comment? I did this now, but I have a bad feeling with it.
> >
> > The change of this patch surely removes dead code as of now and makes it
> > more readable. But also it could become alive again, once somebody
> > changes things nearby and forgets about the assumption, hsize not
> > exceeding a certain size. That's why I put a comment in there, so
> > the future changes nearby may be more careful.
> 
> Indeed, I feel uncomfortable with the patch in general for the very
> reason that you state: it might become live again. Without the patch,
> the code remains safe without any extra effort. With this patch, even
> with the in-code comment, someone making changes needs to take special
> care. Sometimes it makes sense to leave safeties in place, even if
> they can't be triggered _today_; and safeties (such as i < 31) also
> serve as documentation.

That's also a valid argument.  I don't think this loop is going to 
appear on any trace profile either.

I personally have no strong opinion here.


Nicolas

```

## brian m. carlson, 2013-08-16 11:43

Subject: Re: [PATCH] create_delta_index: simplify condition always evaluating to true
Message-ID: <20130816114346.GC5123@vauxhall.crustytoothpaste.net>
URL: https://gitlist.dev/e/20130816114346.GC5123%40vauxhall.crustytoothpaste.net
In-Reply-To: <1376595460-6546-1-git-send-email-stefanbeller@googlemail.com>

```
On Thu, Aug 15, 2013 at 09:37:40PM +0200, Stefan Beller wrote:
> When checking the previous lines in that function, we can deduct that
> hsize must always be smaller than (1u<<31), since 506049c7df2c6
> (fix >4GiB source delta assertion failure), because the entries is

the entries are

> capped at an upper bound of 0xfffffffeU, so hsize contains a maximum
> value of 0x3fffffff, which is smaller than (1u<<31), so i will never
> be larger than 31.

-- 
brian m. carlson / brian with sandals: Houston, Texas, US
+1 832 623 2791 | http://www.crustytoothpaste.net/~bmc | My opinion only
OpenPGP: RSA v4 4096b: 88AC E9B2 9196 305B A994 7552 F1BA 225C 0223 B187

```

## Eric Sunshine, 2013-08-16 12:40

Subject: Re: [PATCH] create_delta_index: simplify condition always evaluating to true
Message-ID: <CAPig+cT7KcoVgxnDXUgYuxbpcTkUGqi8m56ArFbYxS5ubftxCw@mail.gmail.com>
URL: https://gitlist.dev/e/CAPig%2BcT7KcoVgxnDXUgYuxbpcTkUGqi8m56ArFbYxS5ubftxCw%40mail.gmail.com
In-Reply-To: <20130816114346.GC5123@vauxhall.crustytoothpaste.net>

```
On Fri, Aug 16, 2013 at 7:43 AM, brian m. carlson
<sandals@crustytoothpaste.net> wrote:
> On Thu, Aug 15, 2013 at 09:37:40PM +0200, Stefan Beller wrote:
>> When checking the previous lines in that function, we can deduct that
>> hsize must always be smaller than (1u<<31), since 506049c7df2c6
>> (fix >4GiB source delta assertion failure), because the entries is
>
> the entries are

'entries' is a variable name, thus singular. Stefan corrected it in v2
to say "because entries is capped..." (though it would have been even
a bit clearer to add quotes around 'entries').

>> capped at an upper bound of 0xfffffffeU, so hsize contains a maximum
>> value of 0x3fffffff, which is smaller than (1u<<31), so i will never
>> be larger than 31.

```

## Philip Oakley, 2013-08-16 16:47

Subject: Re: [PATCH] create_delta_index: simplify condition always evaluating to true
Message-ID: <EE5B338564E14F89B349550B37741AFF@PhilipOakley>
URL: https://gitlist.dev/e/EE5B338564E14F89B349550B37741AFF%40PhilipOakley
In-Reply-To: <CAPig+cQ5Y9irLk=9Bhz09c=5yzZEcyMKn2kbhcrO_zDpgmkhGw@mail.gmail.com>

```
From: "Eric Sunshine" <sunshine@sunshineco.com>
> On Thu, Aug 15, 2013 at 5:34 PM, Stefan Beller
> <stefanbeller@googlemail.com> wrote:
>> When checking the previous lines in that function, we can deduce that
>> hsize must always be smaller than (1u<<31), since 506049c7df2c6
>> (fix >4GiB source delta assertion failure), because entries is
>> capped at an upper bound of 0xfffffffeU, so hsize contains a maximum
>> value of 0x3fffffff, which is smaller than (1u<<31), so the value of
>> 'i' will never be larger than 31.
>>
>> Signed-off-by: Stefan Beller <stefanbeller@googlemail.com>
>> ---
>>
>> Eric, thanks for reviewing my patch.
>>
>> I applied the first 2 proposals (deduce, entries), but I disagree on
>> the third, so I reformulated the sentence, as I really meant the 
>> variable
>> i and not it as a pronoun.
>
> Thanks. Adding the quotes around 'i' makes your meaning clear. Without
> the quotes, apparently it was ambiguous, and my brain read it as a
> misspelling of 'it'.
>
>> Do I understand right, you're suggesting to remove the
>> source code comment? I did this now, but I have a bad feeling with 
>> it.
>>
>> The change of this patch surely removes dead code as of now and makes 
>> it
>> more readable. But also it could become alive again, once somebody
>> changes things nearby and forgets about the assumption, hsize not
>> exceeding a certain size. That's why I put a comment in there, so
>> the future changes nearby may be more careful.
>
> Indeed, I feel uncomfortable with the patch in general for the very
> reason that you state: it might become live again. Without the patch,
> the code remains safe without any extra effort.

The problem is that without the patch (or some change) the code was 
already unsafe.

The code sequence  ' (1u << i) < hsize && i < 31 ' is a multi step 
process, whose first step requires that 'i' is already less that 31, 
otherwise the result (1u << i)  is undefined (and  'undef_val < hsize' 
can therefore be assumed to be 'false'), and so the later test  i < 31 
can always be optimised away as dead code ('i' is already less than 31, 
or the short circuit 'and' applies).

Simply swapping around the code such that the i < 31 test is performed 
first would also solve the (latent optimisation) problem.

Section 2.2 of the "Undefined behavior: What happened to my code?" paper 
on http://css.csail.mit.edu/stack/ discusses this issue with an example 
from the Linux kernel.

> With this patch, even
> with the in-code comment, someone making changes needs to take special
> care. Sometimes it makes sense to leave safeties in place, even if
> they can't be triggered _today_; and safeties (such as i < 31) also
> serve as documentation.
>
>>
>> Thanks,
>> Stefan
>>
>>
>>  diff-delta.c | 2 +-
>>  1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/diff-delta.c b/diff-delta.c
>> index 93385e1..3797ce6 100644
>> --- a/diff-delta.c
>> +++ b/diff-delta.c
>> @@ -155,7 +155,7 @@ struct delta_index * create_delta_index(const 
>> void *buf, unsigned long bufsize)
>>                 entries = 0xfffffffeU / RABIN_WINDOW;
>>         }
>>         hsize = entries / 4;
>> -       for (i = 4; (1u << i) < hsize && i < 31; i++);
>> +       for (i = 4; (1u << i) < hsize; i++);
>>         hsize = 1 << i;
>>         hmask = hsize - 1;
>>
>> --
>> 1.8.4.rc3.1.gc1ebd90
>>
> --

Philip 

```

## Stefan Beller, 2013-08-16 21:22

Subject: [PATCH] create_delta_index: simplify condition always evaluating to true
Message-ID: <1376688157-8374-1-git-send-email-stefanbeller@googlemail.com>
URL: https://gitlist.dev/e/1376688157-8374-1-git-send-email-stefanbeller%40googlemail.com
In-Reply-To: <EE5B338564E14F89B349550B37741AFF@PhilipOakley>

```
The code sequence  ' (1u << i) < hsize && i < 31 ' is a multi step
process, whose first step requires that 'i' is already less that 31,
otherwise the result (1u << i)  is undefined (and  'undef_val < hsize'
can therefore be assumed to be 'false'), and so the later test  i < 31
can always be optimized away as dead code ('i' is already less than 31,
or the short circuit 'and' applies).

So we need to get rid of that code. One way would be to exchange the 
order of the conditions, so the expression 'i < 31 && (1u << i) < hsize' 
would remove that optimized unstable code already.

However when checking the previous lines in that function, we can deduce
that 'hsize' must always be smaller than (1u<<31), since 506049c7df2c6
(fix >4GiB source delta assertion failure), because 'entries' is
capped at an upper bound of 0xfffffffeU, so 'hsize' contains a maximum
value of 0x3fffffff, which is smaller than (1u<<31), so the value of
'i' will never be larger than 31 and we can remove that condition 
entirely.

Signed-off-by: Stefan Beller <stefanbeller@googlemail.com>
Acked-by: Nicolas Pitre <nico@fluxnic.net>
---

Philip, 
thanks for the wording of your mail. I get quickly derailed from the
warnings of the STACK tool and write the other commit messages than
I ought to write. I think the wording of your mail nails it pretty
good, so I put it in the commit message as well.

Stefan


 diff-delta.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/diff-delta.c b/diff-delta.c
index 93385e1..3797ce6 100644
--- a/diff-delta.c
+++ b/diff-delta.c
@@ -155,7 +155,7 @@ struct delta_index * create_delta_index(const void *buf, unsigned long bufsize)
 		entries = 0xfffffffeU / RABIN_WINDOW;
 	}
 	hsize = entries / 4;
-	for (i = 4; (1u << i) < hsize && i < 31; i++);
+	for (i = 4; (1u << i) < hsize; i++);
 	hsize = 1 << i;
 	hmask = hsize - 1;
 
-- 
1.8.4.rc3.2.g2c2b664

```

## Philip Oakley, 2013-08-17 06:58

Subject: Re: [PATCH] create_delta_index: simplify condition always evaluating to true
Message-ID: <E5FB18126E7F42D79E085F3924AC9E07@PhilipOakley>
URL: https://gitlist.dev/e/E5FB18126E7F42D79E085F3924AC9E07%40PhilipOakley
In-Reply-To: <1376688157-8374-1-git-send-email-stefanbeller@googlemail.com>

```
From: "Stefan Beller" <stefanbeller@googlemail.com>
> The code sequence  ' (1u << i) < hsize && i < 31 ' is a multi step
> process, whose first step requires that 'i' is already less that 31,
> otherwise the result (1u << i)  is undefined (and  'undef_val < hsize'
> can therefore be assumed to be 'false'), and so the later test  i < 31
> can always be optimized away as dead code ('i' is already less than 
> 31,
> or the short circuit 'and' applies).
>
> So we need to get rid of that code. One way would be to exchange the
> order of the conditions, so the expression 'i < 31 && (1u << i) < 
> hsize'
> would remove that optimized unstable code already.
>
> However when checking the previous lines in that function, we can 
> deduce
> that 'hsize' must always be smaller than (1u<<31), since 506049c7df2c6
> (fix >4GiB source delta assertion failure), because 'entries' is
> capped at an upper bound of 0xfffffffeU, so 'hsize' contains a maximum
> value of 0x3fffffff, which is smaller than (1u<<31), so the value of
> 'i' will never be larger than 31 and we can remove that condition
> entirely.
>
> Signed-off-by: Stefan Beller <stefanbeller@googlemail.com>
> Acked-by: Nicolas Pitre <nico@fluxnic.net>
> ---

Acked-by: Philip Oakley philipoakley@iee.org

If there are concens that future upgrades may make the (i < 31) test a 
necessity that it should be added before the &&. If the compiler is able 
to determine it's dead code in that case then that's OK, at least it 
would be future proof. Then again it's probably not likely in the near 
future.

>
> Philip,
> thanks for the wording of your mail. I get quickly derailed from the
> warnings of the STACK tool and write the other commit messages than
> I ought to write. I think the wording of your mail nails it pretty
> good, so I put it in the commit message as well.
>
> Stefan
>
>
> diff-delta.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/diff-delta.c b/diff-delta.c
> index 93385e1..3797ce6 100644
> --- a/diff-delta.c
> +++ b/diff-delta.c
> @@ -155,7 +155,7 @@ struct delta_index * create_delta_index(const void 
> *buf, unsigned long bufsize)
>  entries = 0xfffffffeU / RABIN_WINDOW;
>  }
>  hsize = entries / 4;
> - for (i = 4; (1u << i) < hsize && i < 31; i++);
> + for (i = 4; (1u << i) < hsize; i++);
>  hsize = 1 << i;
>  hmask = hsize - 1;
>
> -- 
> 1.8.4.rc3.2.g2c2b664
>
>
Philip 

```
