Skip to contentSkip to conversation

Patches to let Git build with GCC 6 and DEVELOPER=SureWhyNot

PATCH In review Johannes Schindelin 5 participants 17 messages Aug 4, 2016 – Aug 6, 2016

lore
Share & tools
lore
#0 lore raw

cover Patches to let Git build with GCC 6 and DEVELOPER=SureWhyNot

While working on some local setup to allow my poor little laptop to build and test the Windows-specific patches of Git for Windows on top of the upstream branches, my development environment updated to use GCC 6, and these patches were required.

Johannes Schindelin (2):
nedmalloc: fix misleading indentation
nedmalloc: work around overzealous GCC 6 warning

 compat/nedmalloc/nedmalloc.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

Published-As: https://github.com/dscho/git/releases/tag/gcc-6-v1
Fetch-It-Via: git fetch https://github.com/dscho/git gcc-6-v1

Signature · 3 lines
2.9.0.281.g286a8d9

base-commit: 80460f513ebd7851953f5402dd9744236128b240
#1 lore raw

PATCH 1/2 nedmalloc: fix misleading indentation

Some code in nedmalloc is indented in a funny way that could be misinterpreted as if a line after a for loop was included in the loop body, when it is not.

GCC 6 complains about this in DEVELOPER=YepSure mode.

---
 compat/nedmalloc/nedmalloc.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
compat/nedmalloc/nedmalloc.c +4 -4
index a0a16eb..677d1b2 100644
@@ -938,10 +938,10 @@ void **nedpindependent_comalloc(nedpool *p, size_t elems, size_t *sizes, void **
 	void **ret;
 	threadcache *tc;
 	int mymspace;
-    size_t i, *adjustedsizes=(size_t *) alloca(elems*sizeof(size_t));
-    if(!adjustedsizes) return 0;
-    for(i=0; i<elems; i++)
-	adjustedsizes[i]=sizes[i]<sizeof(threadcacheblk) ? sizeof(threadcacheblk) : sizes[i];
+	size_t i, *adjustedsizes=(size_t *) alloca(elems*sizeof(size_t));
+	if(!adjustedsizes) return 0;
+	for(i=0; i<elems; i++)
+		adjustedsizes[i]=sizes[i]<sizeof(threadcacheblk) ? sizeof(threadcacheblk) : sizes[i];
 	GetThreadCache(&p, &tc, &mymspace, 0);
 	GETMSPACE(m, p, tc, mymspace, 0,
 	      ret=mspace_independent_comalloc(m, elems, adjustedsizes, chunks));
Signature · 1 line
2.9.0.281.g286a8d9
#2 lore raw

PATCH 2/2 nedmalloc: work around overzealous GCC 6 warning

In reply to Johannes Schindelin

With GCC 6, the strdup() function is declared with the "nonnull" attribute, stating that it is not allowed to pass a NULL value as parameter.

In nedmalloc()'s reimplementation of strdup(), Postel's Law is heeded and NULL parameters are handled gracefully. GCC 6 complains about that now because it thinks that NULL cannot be passed to strdup() anyway.

Let's just shut up GCC >= 6 in that case and go on with our lives.

See https://gcc.gnu.org/gcc-6/porting_to.html for details.

---
 compat/nedmalloc/nedmalloc.c | 3 +++
 1 file changed, 3 insertions(+)
compat/nedmalloc/nedmalloc.c +3 -0
index 677d1b2..3f28c0b 100644
@@ -956,6 +956,9 @@ void **nedpindependent_comalloc(nedpool *p, size_t elems, size_t *sizes, void **
 char *strdup(const char *s1)
 {
 	char *s2 = 0;
+#if __GNUC__ >= 6
+#pragma GCC diagnostic ignored "-Wnonnull-compare"
+#endif
 	if (s1) {
 		size_t len = strlen(s1) + 1;
 		s2 = malloc(len);
Signature · 1 line
2.9.0.281.g286a8d9
#3 lore raw

Re: [PATCH 2/2] nedmalloc: work around overzealous GCC 6 warning

Quoted 22 lines · Johannes Schindelin

With GCC 6, the strdup() function is declared with the "nonnull" attribute, stating that it is not allowed to pass a NULL value as parameter.

In nedmalloc()'s reimplementation of strdup(), Postel's Law is heeded and NULL parameters are handled gracefully. GCC 6 complains about that now because it thinks that NULL cannot be passed to strdup() anyway.

Let's just shut up GCC >= 6 in that case and go on with our lives.

See https://gcc.gnu.org/gcc-6/porting_to.html for details.

---
 compat/nedmalloc/nedmalloc.c | 3 +++
 1 file changed, 3 insertions(+)
compat/nedmalloc/nedmalloc.c +0 -0
index 677d1b2..3f28c0b 100644

compat/nedmalloc/nedmalloc.c void **nedpindependent_comalloc(nedpool *p, size_t elems, size_t *sizes, void ** · line 964

Quoted 9 lines
compat/nedmalloc/nedmalloc.c +3 -0
@@ -956,6 +956,9 @@ void **nedpindependent_comalloc(nedpool *p, size_t elems, size_t *sizes, void **
 char *strdup(const char *s1)
 {
 	char *s2 = 0;
+#if __GNUC__ >= 6
+#pragma GCC diagnostic ignored "-Wnonnull-compare"
+#endif
 	if (s1) {
 		size_t len = strlen(s1) + 1;
 		s2 = malloc(len);

Is it a common convention to place "#pragma GCC diagnostic" immediately before the statement you want to affect, and have the same pragma in effect until the end of the compilation unit?

I know this function is at the end and it is not worth doing push/ignored/pop dance, and I assumed that it is the reason why we see a single "ignore from here on", which is much simpler, but it is somewhat distracting. It made me wonder if it makes it easier to read and less distracting to have these three lines in front of and outside the function definition, while thinking that it would have a documentation value to have it immediately before the statement you want to affect. Help me convince myself that this is the best place.

Thanks.

#4 lore raw

Re: [PATCH 2/2] nedmalloc: work around overzealous GCC 6 warning

Hi Junio,

Quoted 29 lines · Junio C Hamano
Quoted 27 lines · Johannes Schindelin

With GCC 6, the strdup() function is declared with the "nonnull" attribute, stating that it is not allowed to pass a NULL value as parameter.

In nedmalloc()'s reimplementation of strdup(), Postel's Law is heeded and NULL parameters are handled gracefully. GCC 6 complains about that now because it thinks that NULL cannot be passed to strdup() anyway.

Let's just shut up GCC >= 6 in that case and go on with our lives.

See https://gcc.gnu.org/gcc-6/porting_to.html for details.

---
 compat/nedmalloc/nedmalloc.c | 3 +++
 1 file changed, 3 insertions(+)
compat/nedmalloc/nedmalloc.c +2 -0
index 677d1b2..3f28c0b 100644
@@ -956,6 +956,9 @@ void **nedpindependent_comalloc(nedpool *p, size_t elems, size_t *sizes, void **
 char *strdup(const char *s1)
 {
 	char *s2 = 0;
+#if __GNUC__ >= 6
+#pragma GCC diagnostic ignored "-Wnonnull-compare"
Quoted 8 lines

compat/nedmalloc/nedmalloc.c void **nedpindependent_comalloc(nedpool *p, size_t elems, size_t *sizes, void ** · line 964

Quoted 4 lines
compat/nedmalloc/nedmalloc.c +1 -0
+#endif
 	if (s1) {
 		size_t len = strlen(s1) + 1;
 		s2 = malloc(len);

Is it a common convention to place "#pragma GCC diagnostic" immediately before the statement you want to affect, and have the same pragma in effect until the end of the compilation unit?

Uh oh. This was a brain fart. I somehow confused the way pragmas work with the way __attribute__s work. You are correct, of course, that the pragma affects the entire remainder of the file, not just this statement.

Luckily, René came up with a much more elegant solution, so that Git's history does not have to shame me eternally.

Ciao,
Dscho

#5 lore raw

Re: [PATCH 2/2] nedmalloc: work around overzealous GCC 6 warning

In reply to Johannes Schindelin

Am 04.08.2016 um 18:07 schrieb Johannes Schindelin:

Quoted 9 lines

With GCC 6, the strdup() function is declared with the "nonnull" attribute, stating that it is not allowed to pass a NULL value as parameter.

In nedmalloc()'s reimplementation of strdup(), Postel's Law is heeded and NULL parameters are handled gracefully. GCC 6 complains about that now because it thinks that NULL cannot be passed to strdup() anyway.

Let's just shut up GCC >= 6 in that case and go on with our lives.

This version of strdup() is only compiled if nedmalloc is used instead of the system allocator. That means we can't rely on strdup() being able to take NULL -- some (most?) platforms won't like it. Removing the NULL check would be a more general and overall easier way out, no?

But it should check the result of malloc() before copying.

---
 compat/nedmalloc/nedmalloc.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
compat/nedmalloc/nedmalloc.c +3 -5
index a0a16eb..cc18f0c 100644
@@ -955,12 +955,10 @@ void **nedpindependent_comalloc(nedpool *p, size_t elems, size_t *sizes, void **
  */
 char *strdup(const char *s1)
 {
-	char *s2 = 0;
-	if (s1) {
-		size_t len = strlen(s1) + 1;
-		s2 = malloc(len);
+	size_t len = strlen(s1) + 1;
+	char *s2 = malloc(len);
+	if (s2)
 		memcpy(s2, s1, len);
-	}
 	return s2;
 }
 #endif
Signature · 1 line
2.9.2
#6 lore raw

Re: [PATCH 2/2] nedmalloc: work around overzealous GCC 6 warning

Quoted 4 lines · René Scharfe

This version of strdup() is only compiled if nedmalloc is used instead of the system allocator. That means we can't rely on strdup() being able to take NULL -- some (most?) platforms won't like it. Removing the NULL check would be a more general and overall easier way out, no?

The callers of this version must be prepared to call a version of strdup() that does not accept NULL, so in a sense, passing NULL to this function is already an error in the context of this project.

That sounds like a good rationale to take this more straight-forward approach.

Quoted 26 lines

But it should check the result of malloc() before copying.

---
 compat/nedmalloc/nedmalloc.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
compat/nedmalloc/nedmalloc.c +0 -0
index a0a16eb..cc18f0c 100644
@@ -955,12 +955,10 @@ void **nedpindependent_comalloc(nedpool *p,

size_t elems, size_t *sizes, void **
*/
char *strdup(const char *s1)
{

compat/nedmalloc/nedmalloc.c +3 -5
-	char *s2 = 0;
-	if (s1) {
-		size_t len = strlen(s1) + 1;
-		s2 = malloc(len);
+	size_t len = strlen(s1) + 1;
+	char *s2 = malloc(len);
+	if (s2)
 		memcpy(s2, s1, len);
-	}
 	return s2;
 }
 #endif
#7 lore raw

Re: [PATCH 2/2] nedmalloc: work around overzealous GCC 6 warning

In reply to René Scharfe

Let's try it this way. How about this as a replacement?

-- >8 --
From: Johannes Schindelin <[email protected]>
Date: Thu, 4 Aug 2016 18:07:08 +0200
Subject: [PATCH] nedmalloc: work around overzealous GCC 6 warning

With GCC 6, the strdup() function is declared with the "nonnull" attribute, stating that it is not allowed to pass a NULL value as parameter.

In nedmalloc()'s reimplementation of strdup(), Postel's Law is heeded and NULL parameters are handled gracefully. GCC 6 complains about that now because it thinks that NULL cannot be passed to strdup() anyway.

Because the callers in this project of strdup() must be prepared to call any implementation of strdup() supplied by the platform, so it is pointless to pretend that it is OK to call it with NULL.

Remove the conditional based on NULL-ness of the input; this squelches the warning.

See https://gcc.gnu.org/gcc-6/porting_to.html for details.

---
 compat/nedmalloc/nedmalloc.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
compat/nedmalloc/nedmalloc.c +3 -5
index 677d1b2..88cd78c 100644
@@ -955,12 +955,10 @@ void **nedpindependent_comalloc(nedpool *p, size_t elems, size_t *sizes, void **
  */
 char *strdup(const char *s1)
 {
-	char *s2 = 0;
-	if (s1) {
-		size_t len = strlen(s1) + 1;
-		s2 = malloc(len);
+	size_t len = strlen(s1) + 1;
+	s2 = malloc(len);
+	if (s1)
 		memcpy(s2, s1, len);
-	}
 	return s2;
 }
 #endif
Signature · 1 line
2.9.2-766-gd7972a8
#8 lore raw

Re: [PATCH 2/2] nedmalloc: work around overzealous GCC 6 warning

Am 05.08.2016 um 00:39 schrieb Junio C Hamano:

compat/nedmalloc/nedmalloc.c void **nedpindependent_comalloc(nedpool *p, size_t elems, size_t *sizes, void ** · line 960

Quoted 11 lines
compat/nedmalloc/nedmalloc.c +3 -4
@@ -955,12 +955,10 @@ void **nedpindependent_comalloc(nedpool *p, size_t elems, size_t *sizes, void **
  */
 char *strdup(const char *s1)
 {
-	char *s2 = 0;
-	if (s1) {
-		size_t len = strlen(s1) + 1;
-		s2 = malloc(len);
+	size_t len = strlen(s1) + 1;
+	s2 = malloc(len);
+	if (s1)

It does not make sense to check s1 for NULL when it was passed to strlen() earlier; strlen() does not accept NULL, either...

compat/nedmalloc/nedmalloc.c void **nedpindependent_comalloc(nedpool *p, size_t elems, size_t *sizes, void ** · line 964

Quoted 5 lines
compat/nedmalloc/nedmalloc.c +0 -1
 		memcpy(s2, s1, len);
-	}
 	return s2;
 }
 #endif

-- Hannes

#9 lore raw

Re: [PATCH 2/2] nedmalloc: work around overzealous GCC 6 warning

Am 05.08.2016 um 07:36 schrieb Johannes Sixt:

Quoted 16 lines

Am 05.08.2016 um 00:39 schrieb Junio C Hamano:

compat/nedmalloc/nedmalloc.c void **nedpindependent_comalloc(nedpool *p, size_t elems, size_t *sizes, void ** · line 960

Quoted 12 lines
(quoted diff) +0 -0
@@ -955,12 +955,10 @@ void **nedpindependent_comalloc(nedpool *p,

size_t elems, size_t *sizes, void **
*/
char *strdup(const char *s1)
{

compat/nedmalloc/nedmalloc.c +3 -4
-    char *s2 = 0;
-    if (s1) {
-        size_t len = strlen(s1) + 1;
-        s2 = malloc(len);
+    size_t len = strlen(s1) + 1;
+    s2 = malloc(len);
+    if (s1)

It does not make sense to check s1 for NULL when it was passed to strlen() earlier; strlen() does not accept NULL, either...

Oh! This is a typo. You meant to check s2 for NULL.

And the declaration for s2 should remain, of course.

Quoted 6 lines
Quoted 6 lines
         memcpy(s2, s1, len);
-    }
     return s2;

}
#endif

-- Hannes

#10 lore raw

Re: [PATCH 2/2] nedmalloc: work around overzealous GCC 6 warning

Quoted 3 lines · Johannes Sixt

Oh! This is a typo. You meant to check s2 for NULL.

And the declaration for s2 should remain, of course.

Yeah, the moral of the story is don't try to do something you do not usually do X-<.

The second try (the log message is the same).

I do not know if we want to worry about st_add(1, strlen(s1)) overflow around here, though.

 compat/nedmalloc/nedmalloc.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
compat/nedmalloc/nedmalloc.c +4 -5
index 677d1b2..2d4ef59 100644
@@ -955,12 +955,11 @@ void **nedpindependent_comalloc(nedpool *p, size_t elems, size_t *sizes, void **
  */
 char *strdup(const char *s1)
 {
-	char *s2 = 0;
-	if (s1) {
-		size_t len = strlen(s1) + 1;
-		s2 = malloc(len);
+	size_t len = strlen(s1) + 1;
+	char *s2 = malloc(len);
+
+	if (s2)
 		memcpy(s2, s1, len);
-	}
 	return s2;
 }
 #endif
#11 lore raw

Re: [PATCH 2/2] nedmalloc: work around overzealous GCC 6 warning

compat/nedmalloc/nedmalloc.c void **nedpindependent_comalloc(nedpool *p, size_t elems, size_t *sizes, void ** · line 958

Quoted 4 lines · Junio C Hamano

I do not know if we want to worry about st_add(1, strlen(s1)) overflow around here, though.
[...]

compat/nedmalloc/nedmalloc.c +1 -0
+	size_t len = strlen(s1) + 1;

I wondered that, too, but I don't think it's possible.

To overflow the size_t with "+1", strlen() must return the maximum value that it can hold. But such a string would need one more byte than that, for its trailing NUL. So assuming you cannot have a string that exceeds size_t in the first place, I think it is impossible to overflow here.

-Peff

#12 lore raw

Re: [PATCH 2/2] nedmalloc: work around overzealous GCC 6 warning

In reply to Junio C Hamano

Hi Junio & René,

Quoted 1 line · Junio C Hamano

Let's try it this way. How about this as a replacement?

I like it (with the if (s2) test intead of if (s1), of course). But please record René as author, maybe mentioning myself with a "Diagnosed-by:" line.

FWIW today's pu does pass the test suite without problems in my CI setup.

This setup will from now on test next & pu in the Git for Windows SDK, and rebase Git for Windows' current master to git.git's maint, master, next & pu, every morning after a weekday (unless I forget to turn on my laptop, that is).

Once it stabilizes, I will figure out a way how to publish the logs, because playing Lt Tawney Madison gets old real quick.

Thanks,
Dscho

#13 lore raw

Re: [PATCH 2/2] nedmalloc: work around overzealous GCC 6 warning

Quoted 4 lines · Johannes Schindelin

This setup will from now on test next & pu in the Git for Windows SDK, and rebase Git for Windows' current master to git.git's maint, master, next & pu, every morning after a weekday (unless I forget to turn on my laptop, that is).

Sounds really good. Thanks.

#14 lore raw

Re: [PATCH 2/2] nedmalloc: work around overzealous GCC 6 warning

Hi Junio,

Quoted 8 lines · Junio C Hamano
Quoted 4 lines · Johannes Schindelin

This setup will from now on test next & pu in the Git for Windows SDK, and rebase Git for Windows' current master to git.git's maint, master, next & pu, every morning after a weekday (unless I forget to turn on my laptop, that is).

Sounds really good. Thanks.

Report for today:

- pu and next pass

- rebasing the Windows-specific patches failed because of the merge conflicts when trying to apply the GCC-6 pragma, which I have on Git for Windows' master already.

Note: I fixed said merge conflicts locally, and the way I set this up, the CI job learns the conflict resolution and will apply it automatically next time it runs.

So: all greeeeen today.

Ciao,
Dscho

#15 lore raw

Re: [PATCH 2/2] nedmalloc: work around overzealous GCC 6 warning

In reply to Johannes Schindelin
Quoted 9 lines · Johannes Schindelin

Hi Junio & René,

Quoted 1 line · Junio C Hamano

Let's try it this way. How about this as a replacement?

I like it (with the if (s2) test intead of if (s1), of course). But please record René as author, maybe mentioning myself with a "Diagnosed-by:" line.

Hmph. I cannot do that unilaterally without waiting for René to respond, though. In any case, with only header and footer changes, here is what will appear in 'pu'.

Thanks.

-- >8 --
From: René Scharfe <[email protected]>
Date: Thu, 4 Aug 2016 23:56:54 +0200
Subject: [PATCH] nedmalloc: work around overzealous GCC 6 warning

With GCC 6, the strdup() function is declared with the "nonnull" attribute, stating that it is not allowed to pass a NULL value as parameter.

In nedmalloc()'s reimplementation of strdup(), Postel's Law is heeded and NULL parameters are handled gracefully. GCC 6 complains about that now because it thinks that NULL cannot be passed to strdup() anyway.

Because the callers in this project of strdup() must be prepared to call any implementation of strdup() supplied by the platform, so it is pointless to pretend that it is OK to call it with NULL.

Remove the conditional based on NULL-ness of the input; this squelches the warning.

See https://gcc.gnu.org/gcc-6/porting_to.html for details.

---
 compat/nedmalloc/nedmalloc.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
compat/nedmalloc/nedmalloc.c +4 -5
index 677d1b2..2d4ef59 100644
@@ -955,12 +955,11 @@ void **nedpindependent_comalloc(nedpool *p, size_t elems, size_t *sizes, void **
  */
 char *strdup(const char *s1)
 {
-	char *s2 = 0;
-	if (s1) {
-		size_t len = strlen(s1) + 1;
-		s2 = malloc(len);
+	size_t len = strlen(s1) + 1;
+	char *s2 = malloc(len);
+
+	if (s2)
 		memcpy(s2, s1, len);
-	}
 	return s2;
 }
 #endif
Signature · 1 line
2.9.2-766-gd7972a8
#16 lore raw

Re: [PATCH 2/2] nedmalloc: work around overzealous GCC 6 warning

Am 05.08.2016 um 23:57 schrieb Junio C Hamano:

Quoted 8 lines
Quoted 6 lines · Johannes Schindelin

Hi Junio & René,

Quoted 1 line · Junio C Hamano

Let's try it this way. How about this as a replacement?

Quoted 7 lines
Quoted 3 lines

I like it (with the if (s2) test intead of if (s1), of course). But please record René as author, maybe mentioning myself with a "Diagnosed-by:" line.

Hmph. I cannot do that unilaterally without waiting for René to respond, though. In any case, with only header and footer changes, here is what will appear in 'pu'.

It's fine with me, thanks. :) Minor comments below.

Quoted 15 lines

Thanks.

-- >8 --
From: René Scharfe <[email protected]>
Date: Thu, 4 Aug 2016 23:56:54 +0200
Subject: [PATCH] nedmalloc: work around overzealous GCC 6 warning

With GCC 6, the strdup() function is declared with the "nonnull" attribute, stating that it is not allowed to pass a NULL value as parameter.

In nedmalloc()'s reimplementation of strdup(), Postel's Law is heeded and NULL parameters are handled gracefully. GCC 6 complains about that now because it thinks that NULL cannot be passed to strdup() anyway.

Quoted 6 lines

Because the callers in this project of strdup() must be prepared to call any implementation of strdup() supplied by the platform, so it is pointless to pretend that it is OK to call it with NULL.

Remove the conditional based on NULL-ness of the input; this squelches the warning.

My commit message would have been much shorter, probably too short. But perhaps add here: "Check the return value of malloc() instead to make sure we actually got memory to write to."

Quoted 14 lines

See https://gcc.gnu.org/gcc-6/porting_to.html for details.

---
  compat/nedmalloc/nedmalloc.c | 9 ++++-----
  1 file changed, 4 insertions(+), 5 deletions(-)
compat/nedmalloc/nedmalloc.c +0 -0
index 677d1b2..2d4ef59 100644

compat/nedmalloc/nedmalloc.c void **nedpindependent_comalloc(nedpool *p, size_t elems, size_t *sizes, void ** · line 960

Quoted 10 lines
compat/nedmalloc/nedmalloc.c +3 -4
@@ -955,12 +955,11 @@ void **nedpindependent_comalloc(nedpool *p, size_t elems, size_t *sizes, void **
   */
  char *strdup(const char *s1)
  {
-	char *s2 = 0;
-	if (s1) {
-		size_t len = strlen(s1) + 1;
-		s2 = malloc(len);
+	size_t len = strlen(s1) + 1;
+	char *s2 = malloc(len);
+

You added this blank line. OK.

Quoted 7 lines
compat/nedmalloc/nedmalloc.c +1 -1
+	if (s2)
  		memcpy(s2, s1, len);
-	}
  	return s2;
  }
  #endif