Skip to contentSkip to conversation

create_delta_index: simplify condition always evaluating to true

PATCH In review Stefan Beller 6 participants 14 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 create_delta_index: simplify condition always evaluating to true

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.

---
 diff-delta.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
diff-delta.c +8 -1
index 93385e1..54da95b 100644
@@ -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;
 
Signature · 1 line
1.8.4.rc3.498.g5af1768
#1 lore raw
Quoted 1 line · Stefan Beller

When checking the previous lines in that function, we can deduct that

s/deduct/deduce/

Quoted 2 lines

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.

Quoted 2 lines

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/

Quoted 12 lines

be larger than 31.

---
 diff-delta.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
diff-delta.c +0 -0
index 93385e1..54da95b 100644

diff-delta.c struct delta_index * create_delta_index(const void *buf, unsigned long bufsize) · line 163

Quoted 10 lines
diff-delta.c +7 -0
@@ -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.

Quoted 7 lines
diff-delta.c +1 -2
        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;
 
--
#2 lore raw

PATCH 1/1 create_delta_index: simplify condition always evaluating to true

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.

---

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-delta.c +1 -1
index 93385e1..3797ce6 100644
@@ -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;
 
Signature · 1 line
1.8.4.rc3.1.gc1ebd90
#3 lore raw

From: "Stefan Beller" <[email protected]>

Quoted 16 lines

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.

---

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.

Quoted 8 lines

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/

Quoted 25 lines

Thanks,
Stefan

diff-delta.c | 2 +-

1 file changed, 1 insertion(+), 1 deletion(-)
diff-delta.c +1 -1
index 93385e1..3797ce6 100644
@@ -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;
Signature · 2 lines
-- 
1.8.4.rc3.1.gc1ebd90
#4 lore raw
In reply to Stefan Beller
Quoted 7 lines · Stefan Beller

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.

Quoted 8 lines

---

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'.

Quoted 8 lines

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.

Quoted 24 lines

Thanks,
Stefan

 diff-delta.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
diff-delta.c +1 -2
index 93385e1..3797ce6 100644
@@ -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

#5 lore raw

From: "Eric Sunshine" <[email protected]>

Quoted 31 lines
Quoted 7 lines · Stefan Beller

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.

Quoted 8 lines

---

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'.

Quoted 8 lines

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.

Quoted 3 lines

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.

Quoted 28 lines

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.

Quoted 22 lines

Thanks,
Stefan

 diff-delta.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
diff-delta.c +1 -1
index 93385e1..3797ce6 100644
@@ -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;
Quoted 4 lines
Quoted 3 lines

--
1.8.4.rc3.1.gc1ebd90

--

Philip

#6 lore raw

PATCH 1/1 create_delta_index: simplify condition always evaluating to true

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.

---

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-delta.c +1 -1
index 93385e1..3797ce6 100644
@@ -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;
 
Signature · 1 line
1.8.4.rc3.2.g2c2b664
Acked-by on v1 1/1
#7 lore raw

From: "Stefan Beller" <[email protected]>

Quoted 19 lines

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.

Quoted 3 lines

---

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.

Quoted 31 lines

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-delta.c +1 -1
index 93385e1..3797ce6 100644
@@ -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;
Signature · 2 lines
-- 
1.8.4.rc3.2.g2c2b664

Philip

#8 lore raw
In reply to Eric Sunshine
Quoted 31 lines · Eric Sunshine
Quoted 7 lines · Stefan Beller

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.

Quoted 8 lines

---

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'.

Quoted 8 lines

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.

Quoted 7 lines

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

#9 lore raw
In reply to Stefan Beller
Quoted 3 lines · Stefan Beller

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

Quoted 3 lines

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.

Signature · 3 lines
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
#10 lore raw
Quoted 6 lines · brian m. carlson
Quoted 3 lines · Stefan Beller

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').

Quoted 3 lines
Quoted 3 lines

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.

#11 lore raw
In reply to Stefan Beller

Forwarding to the area expert...

Quoted 32 lines · Stefan Beller

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.

---
 diff-delta.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
diff-delta.c +8 -1
index 93385e1..54da95b 100644
@@ -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;
#12 lore raw

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/[email protected]/msg34101.html
http://www.mail-archive.com/[email protected]/msg34103.html

Stefan

Quoted 36 lines · Junio C Hamano

Forwarding to the area expert...

Quoted 32 lines · Stefan Beller

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.

---
 diff-delta.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
diff-delta.c +8 -1
index 93385e1..54da95b 100644
@@ -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;
Acked-by
#13 lore raw
In reply to Junio C Hamano
Quoted 12 lines · Junio C Hamano

Forwarding to the area expert...

Quoted 8 lines · Stefan Beller

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.

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

Quoted 29 lines
Quoted 14 lines
---
 diff-delta.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
diff-delta.c +2 -0
index 93385e1..54da95b 100644
@@ -154,8 +154,15 @@ struct delta_index * create_delta_index(const void *buf, unsigned long bufsize)
 		 */
 		entries = 0xfffffffeU / RABIN_WINDOW;
 	}
+
+	/*

diff-delta.c struct delta_index * create_delta_index(const void *buf, unsigned long bufsize) · line 167

Quoted 10 lines
diff-delta.c +6 -1
+	 * 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 [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html