Skip to contentSkip to conversation

libfdt: Correct signed/unsigned comparisons

PATCH In review Simon Glass 3 participants 10 messages Jan 12, 2020 – Sep 22, 2020 5 messages not in the archive

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libfdt: Correct signed/unsigned comparisons

These warnings appear when building U-Boot on x86 and some other targets. Correct them by adding casts.

Example:

scripts/dtc/libfdt/fdt.c: In function ‘fdt_offset_ptr’:
scripts/dtc/libfdt/fdt.c:137:18: warning: comparison of integer expressions of different signedness: ‘unsigned int’ and ‘int’ [-Wsign-compare]
if ((absoffset < offset)

---

 libfdt/fdt.c          |  4 ++--
 libfdt/fdt_overlay.c  |  2 +-
 libfdt/fdt_ro.c       | 13 +++++++------
 libfdt/fdt_strerror.c |  2 +-
 libfdt/fdt_sw.c       |  9 +++++----
 libfdt/fdt_wip.c      |  2 +-
 6 files changed, 17 insertions(+), 15 deletions(-)
libfdt/fdt.c +2 -2
index d6ce7c0..8b29dbd 100644
@@ -116,7 +116,7 @@ const void *fdt_offset_ptr(const void *fdt, int offset, unsigned int len)
 	unsigned absoffset = offset + fdt_off_dt_struct(fdt);
 
 	if ((absoffset < offset)
-	    || ((absoffset + len) < absoffset)
+	    || ((absoffset + len) < (unsigned int)absoffset)
 	    || (absoffset + len) > fdt_totalsize(fdt))
 		return NULL;
 
@@ -283,7 +283,7 @@ int fdt_move(const void *fdt, void *buf, int bufsize)
 {
 	FDT_RO_PROBE(fdt);
 
-	if (fdt_totalsize(fdt) > bufsize)
+	if (fdt_totalsize(fdt) > (unsigned int)bufsize)
 		return -FDT_ERR_NOSPACE;
 
 	memmove(buf, fdt, fdt_totalsize(fdt));
libfdt/fdt_overlay.c +1 -1
index b310e49..dba4f52 100644
@@ -250,7 +250,7 @@ static int overlay_update_local_node_references(void *fdto,
 			return tree_len;
 		}
 
-		for (i = 0; i < (fixup_len / sizeof(uint32_t)); i++) {
+		for (i = 0; i < (int)(fixup_len / sizeof(uint32_t)); i++) {
 			fdt32_t adj_val;
 			uint32_t poffset;
 
libfdt/fdt_ro.c +7 -6
index a5c2797..e4c90d4 100644
@@ -44,7 +44,7 @@ const char *fdt_get_string(const void *fdt, int stroffset, int *lenp)
 		goto fail;
 
 	err = -FDT_ERR_BADOFFSET;
-	if (absoffset >= totalsize)
+	if (absoffset >= (unsigned int)totalsize)
 		goto fail;
 	len = totalsize - absoffset;
 
@@ -52,14 +52,14 @@ const char *fdt_get_string(const void *fdt, int stroffset, int *lenp)
 		if (stroffset < 0)
 			goto fail;
 		if (fdt_version(fdt) >= 17) {
-			if (stroffset >= fdt_size_dt_strings(fdt))
+			if ((unsigned int)stroffset >= fdt_size_dt_strings(fdt))
 				goto fail;
 			if ((fdt_size_dt_strings(fdt) - stroffset) < len)
 				len = fdt_size_dt_strings(fdt) - stroffset;
 		}
 	} else if (fdt_magic(fdt) == FDT_SW_MAGIC) {
 		if ((stroffset >= 0)
-		    || (stroffset < -fdt_size_dt_strings(fdt)))
+		    || ((unsigned int)stroffset < -fdt_size_dt_strings(fdt)))
 			goto fail;
 		if ((-stroffset) < len)
 			len = -stroffset;
@@ -151,9 +151,10 @@ static const struct fdt_reserve_entry *fdt_mem_rsv(const void *fdt, int n)
 	int offset = n * sizeof(struct fdt_reserve_entry);
 	int absoffset = fdt_off_mem_rsvmap(fdt) + offset;
 
-	if (absoffset < fdt_off_mem_rsvmap(fdt))
+	if ((unsigned int)absoffset < fdt_off_mem_rsvmap(fdt))
 		return NULL;
-	if (absoffset > fdt_totalsize(fdt) - sizeof(struct fdt_reserve_entry))
+	if ((unsigned int)absoffset > fdt_totalsize(fdt) -
+	    sizeof(struct fdt_reserve_entry))
 		return NULL;
 	return fdt_mem_rsv_(fdt, n);
 }
@@ -658,7 +659,7 @@ int fdt_node_offset_by_phandle(const void *fdt, uint32_t phandle)
 {
 	int offset;
 
-	if ((phandle == 0) || (phandle == -1))
+	if ((phandle == 0) || (phandle == (uint32_t)-1))
 		return -FDT_ERR_BADPHANDLE;
 
 	FDT_RO_PROBE(fdt);
libfdt/fdt_strerror.c +1 -1
index 768db66..c02c6ef 100644
@@ -48,7 +48,7 @@ const char *fdt_strerror(int errval)
 		return "<valid offset/length>";
 	else if (errval == 0)
 		return "<no error>";
-	else if (errval > -FDT_ERRTABSIZE) {
+	else if (errval > (int)-FDT_ERRTABSIZE) {
 		const char *s = fdt_errtable[-errval].str;
 
 		if (s)
libfdt/fdt_sw.c +5 -4
index 76bea22..e37d785 100644
@@ -95,7 +95,8 @@ static void *fdt_grab_space_(void *fdt, size_t len)
 	spaceleft = fdt_totalsize(fdt) - fdt_off_dt_struct(fdt)
 		- fdt_size_dt_strings(fdt);
 
-	if ((offset + len < offset) || (offset + len > spaceleft))
+	if ((offset + len < (size_t)offset) ||
+	    (offset + len > (size_t)spaceleft))
 		return NULL;
 
 	fdt_set_size_dt_struct(fdt, offset + len);
@@ -108,7 +109,7 @@ int fdt_create_with_flags(void *buf, int bufsize, uint32_t flags)
 					 sizeof(struct fdt_reserve_entry));
 	void *fdt = buf;
 
-	if (bufsize < hdrsize)
+	if ((unsigned int)bufsize < hdrsize)
 		return -FDT_ERR_NOSPACE;
 
 	if (flags & ~FDT_CREATE_FLAGS_ALL)
@@ -154,7 +155,7 @@ int fdt_resize(void *fdt, void *buf, int bufsize)
 	if ((headsize + tailsize) > fdt_totalsize(fdt))
 		return -FDT_ERR_INTERNAL;
 
-	if ((headsize + tailsize) > bufsize)
+	if ((headsize + tailsize) > (size_t)bufsize)
 		return -FDT_ERR_NOSPACE;
 
 	oldtail = (char *)fdt + fdt_totalsize(fdt) - tailsize;
@@ -248,7 +249,7 @@ static int fdt_add_string_(void *fdt, const char *s)
 
 	offset = -strtabsize - len;
 	struct_top = fdt_off_dt_struct(fdt) + fdt_size_dt_struct(fdt);
-	if (fdt_totalsize(fdt) + offset < struct_top)
+	if (fdt_totalsize(fdt) + offset < (unsigned int)struct_top)
 		return 0; /* no more room :( */
 
 	memcpy(strtab + offset, s, len);
libfdt/fdt_wip.c +1 -1
index f64139e..82db674 100644
@@ -23,7 +23,7 @@ int fdt_setprop_inplace_namelen_partial(void *fdt, int nodeoffset,
 	if (!propval)
 		return proplen;
 
-	if (proplen < (len + idx))
+	if ((unsigned int)proplen < (len + idx))
 		return -FDT_ERR_NOSPACE;
 
 	memcpy((char *)propval + idx, val, len);
Signature · 1 line
2.25.0.rc1.283.g88dfdc4193-goog

A message that is not in the archive (sent off-list or not yet received): 20200112185209.75847-1-sjg-F7+t8E8rja9g9hUCZPvPmw@public.gmane.org

#2 lore raw
Quoted 10 lines · Simon Glass

These warnings appear when building U-Boot on x86 and some other targets. Correct them by adding casts.

Example:

scripts/dtc/libfdt/fdt.c: In function ‘fdt_offset_ptr’:
scripts/dtc/libfdt/fdt.c:137:18: warning: comparison of integer expressions of different signedness: ‘unsigned int’ and ‘int’ [-Wsign-compare]
if ((absoffset < offset)

Hmm. So squashing warnings is certainly a good thing in general.

Unfortunately, I'm really uncomfortable with most of these changes. In a number of cases they are outright wrong. In most of the others, the code was already correct. I dislike adding casts to suppress spurious warnings on correct code because that can end up hiding real problems which might be introduced by future changes.

Case by case details below.

Quoted 15 lines

---

 libfdt/fdt.c          |  4 ++--
 libfdt/fdt_overlay.c  |  2 +-
 libfdt/fdt_ro.c       | 13 +++++++------
 libfdt/fdt_strerror.c |  2 +-
 libfdt/fdt_sw.c       |  9 +++++----
 libfdt/fdt_wip.c      |  2 +-
 6 files changed, 17 insertions(+), 15 deletions(-)
libfdt/fdt.c +0 -0
index d6ce7c0..8b29dbd 100644

libfdt/fdt.c const void *fdt_offset_ptr(const void *fdt, int offset, unsigned int len) · line 119

Quoted 5 lines
libfdt/fdt.c +1 -1
@@ -116,7 +116,7 @@ const void *fdt_offset_ptr(const void *fdt, int offset, unsigned int len)
 	unsigned absoffset = offset + fdt_off_dt_struct(fdt);
 
 	if ((absoffset < offset)
-	    || ((absoffset + len) < absoffset)
+	    || ((absoffset + len) < (unsigned int)absoffset)

I'm baffled by this. Both absoffset and len are defined as unsigned, so how is a signed comparison appearing here?

libfdt/fdt.c int fdt_move(const void *fdt, void *buf, int bufsize) · line 286

Quoted 9 lines
libfdt/fdt.c +1 -1
 	    || (absoffset + len) > fdt_totalsize(fdt))
 		return NULL;
 
@@ -283,7 +283,7 @@ int fdt_move(const void *fdt, void *buf, int bufsize)
 {
 	FDT_RO_PROBE(fdt);
 
-	if (fdt_totalsize(fdt) > bufsize)
+	if (fdt_totalsize(fdt) > (unsigned int)bufsize)

I don't like this change. The comparison is correct as it stands, even though the types are different: fdt_totalsize() > INT_MAX (which should always result in a false comparison) is an error in the dtb, and that's checked by fdt_ro_probe_().

Worse, this means that passing in -1 to what is a signed int parameter will now make the test *pass*, which really doesn't seem right.

libfdt/fdt_overlay.c static int overlay_update_local_node_references(void *fdto, · line 253

Quoted 13 lines
libfdt/fdt.c +0 -0
 		return -FDT_ERR_NOSPACE;
 
 	memmove(buf, fdt, fdt_totalsize(fdt));
libfdt/fdt_overlay.c +1 -1
index b310e49..dba4f52 100644
@@ -250,7 +250,7 @@ static int overlay_update_local_node_references(void *fdto,
 			return tree_len;
 		}
 
-		for (i = 0; i < (fixup_len / sizeof(uint32_t)); i++) {
+		for (i = 0; i < (int)(fixup_len / sizeof(uint32_t)); i++) {

Hrm. Not sure how to handle that. The explicit cast is ugly (and casts in general make things more fragile), but there is a real type difference. It's pretty easy to see that this usage is safe, since an int divided by sizeof(uint32_t) is clearly within the range of an int.

Quoted 8 lines
libfdt/fdt_overlay.c +0 -0
 			fdt32_t adj_val;
 			uint32_t poffset;
 
libfdt/fdt_ro.c +0 -0
index a5c2797..e4c90d4 100644

libfdt/fdt_ro.c const char *fdt_get_string(const void *fdt, int stroffset, int *lenp) · line 48

Quoted 6 lines
libfdt/fdt_ro.c +1 -1
@@ -44,7 +44,7 @@ const char *fdt_get_string(const void *fdt, int stroffset, int *lenp)
 		goto fail;
 
 	err = -FDT_ERR_BADOFFSET;
-	if (absoffset >= totalsize)
+	if (absoffset >= (unsigned int)totalsize)
 		goto fail;

Again, this test is correct, despite the difference in types. This time the change is safe, because we've just verified that totalsize >= 0, but assuming here still makes the code a bit more fragile against rearrangements.

libfdt/fdt_ro.c const char *fdt_get_string(const void *fdt, int stroffset, int *lenp) · line 55

Quoted 8 lines
libfdt/fdt_ro.c +1 -1
 	len = totalsize - absoffset;
 
@@ -52,14 +52,14 @@ const char *fdt_get_string(const void *fdt, int stroffset, int *lenp)
 		if (stroffset < 0)
 			goto fail;
 		if (fdt_version(fdt) >= 17) {
-			if (stroffset >= fdt_size_dt_strings(fdt))
+			if ((unsigned int)stroffset >= fdt_size_dt_strings(fdt))

Again, the test is correct despite the type difference. We've checked for the stroffset < 0 case a few lines above.

libfdt/fdt_ro.c const char *fdt_get_string(const void *fdt, int stroffset, int *lenp) · line 62

Quoted 8 lines
libfdt/fdt_ro.c +1 -1
 				goto fail;
 			if ((fdt_size_dt_strings(fdt) - stroffset) < len)
 				len = fdt_size_dt_strings(fdt) - stroffset;
 		}
 	} else if (fdt_magic(fdt) == FDT_SW_MAGIC) {
 		if ((stroffset >= 0)
-		    || (stroffset < -fdt_size_dt_strings(fdt)))
+		    || ((unsigned int)stroffset < -fdt_size_dt_strings(fdt)))

This is 100% wrong. In the SW case stroffset *will* be negative (we've just checked against that above), and forcing it to an unsigned will give us the wrong result.

libfdt/fdt_ro.c static const struct fdt_reserve_entry *fdt_mem_rsv(const void *fdt, int n) · line 155

Quoted 10 lines
libfdt/fdt_ro.c +1 -1
 			goto fail;
 		if ((-stroffset) < len)
 			len = -stroffset;
@@ -151,9 +151,10 @@ static const struct fdt_reserve_entry *fdt_mem_rsv(const void *fdt, int n)
 	int offset = n * sizeof(struct fdt_reserve_entry);
 	int absoffset = fdt_off_mem_rsvmap(fdt) + offset;
 
-	if (absoffset < fdt_off_mem_rsvmap(fdt))
+	if ((unsigned int)absoffset < fdt_off_mem_rsvmap(fdt))
 		return NULL;

I think there's a real bug here, but rather than casting it would be preferable to change the type of absoffset and offset to unsigned.

libfdt/fdt_ro.c static const struct fdt_reserve_entry *fdt_mem_rsv(const void *fdt, int n) · line 157

Quoted 3 lines
libfdt/fdt_ro.c +2 -1
-	if (absoffset > fdt_totalsize(fdt) - sizeof(struct fdt_reserve_entry))
+	if ((unsigned int)absoffset > fdt_totalsize(fdt) -
+	    sizeof(struct fdt_reserve_entry))

Same here.

libfdt/fdt_ro.c int fdt_node_offset_by_phandle(const void *fdt, uint32_t phandle) · line 662

Quoted 9 lines
libfdt/fdt_ro.c +1 -1
 		return NULL;
 	return fdt_mem_rsv_(fdt, n);
 }
@@ -658,7 +659,7 @@ int fdt_node_offset_by_phandle(const void *fdt, uint32_t phandle)
 {
 	int offset;
 
-	if ((phandle == 0) || (phandle == -1))
+	if ((phandle == 0) || (phandle == (uint32_t)-1))

This change is ok.

libfdt/fdt_strerror.c const char *fdt_strerror(int errval) · line 51

Quoted 13 lines
libfdt/fdt_ro.c +0 -0
 		return -FDT_ERR_BADPHANDLE;
 
 	FDT_RO_PROBE(fdt);
libfdt/fdt_strerror.c +1 -1
index 768db66..c02c6ef 100644
@@ -48,7 +48,7 @@ const char *fdt_strerror(int errval)
 		return "<valid offset/length>";
 	else if (errval == 0)
 		return "<no error>";
-	else if (errval > -FDT_ERRTABSIZE) {
+	else if (errval > (int)-FDT_ERRTABSIZE) {

This one confuses me as well. If the unary - on the RHS was being interpreted unsigned, I can't see how this could have ever worked. But I have gotten meaningful results from fdt_strerror(), so that doesn't seem right.

But if FDT_ERRTABSIZE is being coerced to signed before applying the unary -, then I don't see why there's any cause to warn.

Quoted 8 lines
libfdt/fdt_strerror.c +0 -0
 		const char *s = fdt_errtable[-errval].str;
 
 		if (s)
libfdt/fdt_sw.c +0 -0
index 76bea22..e37d785 100644

libfdt/fdt_sw.c static void *fdt_grab_space_(void *fdt, size_t len) · line 100

Quoted 7 lines
libfdt/fdt_sw.c +2 -1
@@ -95,7 +95,8 @@ static void *fdt_grab_space_(void *fdt, size_t len)
 	spaceleft = fdt_totalsize(fdt) - fdt_off_dt_struct(fdt)
 		- fdt_size_dt_strings(fdt);
 
-	if ((offset + len < offset) || (offset + len > spaceleft))
+	if ((offset + len < (size_t)offset) ||
+	    (offset + len > (size_t)spaceleft))
 		return NULL;

Looks like changing the type of offset would be preferable.

libfdt/fdt_sw.c int fdt_create_with_flags(void *buf, int bufsize, uint32_t flags) · line 113

Quoted 9 lines
libfdt/fdt_sw.c +1 -1
 	fdt_set_size_dt_struct(fdt, offset + len);
@@ -108,7 +109,7 @@ int fdt_create_with_flags(void *buf, int bufsize, uint32_t flags)
 					 sizeof(struct fdt_reserve_entry));
 	void *fdt = buf;
 
-	if (bufsize < hdrsize)
+	if ((unsigned int)bufsize < hdrsize)
 		return -FDT_ERR_NOSPACE;

As with some of the earlier things, the existing comparison is correct, and the changed one is actively dangerous if the caller passes a negative bufsize.

libfdt/fdt_sw.c int fdt_resize(void *fdt, void *buf, int bufsize) · line 158

Quoted 8 lines
libfdt/fdt_sw.c +1 -1
 	if (flags & ~FDT_CREATE_FLAGS_ALL)
@@ -154,7 +155,7 @@ int fdt_resize(void *fdt, void *buf, int bufsize)
 	if ((headsize + tailsize) > fdt_totalsize(fdt))
 		return -FDT_ERR_INTERNAL;
 
-	if ((headsize + tailsize) > bufsize)
+	if ((headsize + tailsize) > (size_t)bufsize)

Ditto.

libfdt/fdt_sw.c static int fdt_add_string_(void *fdt, const char *s) · line 253

Quoted 10 lines
libfdt/fdt_sw.c +1 -1
 		return -FDT_ERR_NOSPACE;
 
 	oldtail = (char *)fdt + fdt_totalsize(fdt) - tailsize;
@@ -248,7 +249,7 @@ static int fdt_add_string_(void *fdt, const char *s)
 
 	offset = -strtabsize - len;
 	struct_top = fdt_off_dt_struct(fdt) + fdt_size_dt_struct(fdt);
-	if (fdt_totalsize(fdt) + offset < struct_top)
+	if (fdt_totalsize(fdt) + offset < (unsigned int)struct_top)
 		return 0; /* no more room :( */

struct_top is generated as the sum of two unsigneds, so changing its type would be preferable.

libfdt/fdt_wip.c int fdt_setprop_inplace_namelen_partial(void *fdt, int nodeoffset, · line 26

Quoted 11 lines
libfdt/fdt_sw.c +0 -0
 	memcpy(strtab + offset, s, len);
libfdt/fdt_wip.c +1 -1
index f64139e..82db674 100644
@@ -23,7 +23,7 @@ int fdt_setprop_inplace_namelen_partial(void *fdt, int nodeoffset,
 	if (!propval)
 		return proplen;
 
-	if (proplen < (len + idx))
+	if ((unsigned int)proplen < (len + idx))

Oof. this is a bit nasty. We're not checking for overflow of len + idx at all, which is a bigger issue than the signedness.

Quoted 3 lines
libfdt/fdt_wip.c +0 -0
 		return -FDT_ERR_NOSPACE;
 
 	memcpy((char *)propval + idx, val, len);
Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
#3 lore raw

Hi David,

Quoted 15 lines · David Gibson
Quoted 10 lines · Simon Glass

These warnings appear when building U-Boot on x86 and some other targets. Correct them by adding casts.

Example:

scripts/dtc/libfdt/fdt.c: In function ‘fdt_offset_ptr’:
scripts/dtc/libfdt/fdt.c:137:18: warning: comparison of integer expressions of different signedness: ‘unsigned int’ and ‘int’ [-Wsign-compare]
if ((absoffset < offset)

Hmm. So squashing warnings is certainly a good thing in general.

Quoted 7 lines

Unfortunately, I'm really uncomfortable with most of these changes. In a number of cases they are outright wrong. In most of the others, the code was already correct. I dislike adding casts to suppress spurious warnings on correct code because that can end up hiding real problems which might be introduced by future changes.

Case by case details below.

Thanks for the review. I agree this is all really horrible, particularly in light of the fact that it doesn't fix bugs and perhaps introduces some.

This was just a quick patch to silence the warnings. If we do make fixes here we should really make sure that there are test cases to trigger each check. I suspect we have some but not all.

What do you think we should do? The warnings are going to be very annoying for people. I could perhaps split the patch up and work through things one by one.

Regards,
Simon

A message that is not in the archive (sent off-list or not yet received): CAPnjgZ2vbphSbvohBD3YTswX2WtKdVxgs9Ltg9vFfrEQyr2LGw-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org

#5 lore raw
Quoted 26 lines · Simon Glass

Hi David,

Quoted 15 lines · David Gibson
Quoted 10 lines · Simon Glass

These warnings appear when building U-Boot on x86 and some other targets. Correct them by adding casts.

Example:

scripts/dtc/libfdt/fdt.c: In function ‘fdt_offset_ptr’:
scripts/dtc/libfdt/fdt.c:137:18: warning: comparison of integer expressions of different signedness: ‘unsigned int’ and ‘int’ [-Wsign-compare]
if ((absoffset < offset)

Hmm. So squashing warnings is certainly a good thing in general.

Quoted 7 lines

Unfortunately, I'm really uncomfortable with most of these changes. In a number of cases they are outright wrong. In most of the others, the code was already correct. I dislike adding casts to suppress spurious warnings on correct code because that can end up hiding real problems which might be introduced by future changes.

Case by case details below.

Quoted 7 lines

Thanks for the review. I agree this is all really horrible, particularly in light of the fact that it doesn't fix bugs and perhaps introduces some.

This was just a quick patch to silence the warnings. If we do make fixes here we should really make sure that there are test cases to trigger each check. I suspect we have some but not all.

Yeah, adding some safety test cases for egregiously bad input like negative buffer sizes is probably a good idea.

Quoted 3 lines

What do you think we should do? The warnings are going to be very annoying for people. I could perhaps split the patch up and work through things one by one.

Yeah, we want to find some way to remove the warnings, and I think splitting up and working piece by piece will be necessary.

I think the very first step, is to find definitive info on what exactly the defined behaviour of C is with a signed vs. unsigned comparison.

The help text of -Wsign-compare seems to imply that assuming:

	signed int a;
	unsigned int b;

then

	if (a < b) ...

is equivalent to

	if ((unsigned int)a < b) ...

But I thought that this was not the case. Rather, I thought it was supposed to always evaluate to true if b > INT_MAX. We need to know which is the case as a starting point.

Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
#6 lore raw

On 17/01/2020 09:23, David Gibson wrote:

Hi,

Quoted 38 lines · Simon Glass
Quoted 26 lines

Hi David,

Quoted 15 lines · David Gibson
Quoted 10 lines · Simon Glass

These warnings appear when building U-Boot on x86 and some other targets. Correct them by adding casts.

Example:

scripts/dtc/libfdt/fdt.c: In function ‘fdt_offset_ptr’:
scripts/dtc/libfdt/fdt.c:137:18: warning: comparison of integer expressions of different signedness: ‘unsigned int’ and ‘int’ [-Wsign-compare]
if ((absoffset < offset)

Hmm. So squashing warnings is certainly a good thing in general.

Quoted 7 lines

Unfortunately, I'm really uncomfortable with most of these changes. In a number of cases they are outright wrong. In most of the others, the code was already correct. I dislike adding casts to suppress spurious warnings on correct code because that can end up hiding real problems which might be introduced by future changes.

Case by case details below.

Quoted 7 lines

Thanks for the review. I agree this is all really horrible, particularly in light of the fact that it doesn't fix bugs and perhaps introduces some.

This was just a quick patch to silence the warnings. If we do make fixes here we should really make sure that there are test cases to trigger each check. I suspect we have some but not all.

Yeah, adding some safety test cases for egregiously bad input like negative buffer sizes is probably a good idea.

Quoted 6 lines
Quoted 3 lines

What do you think we should do? The warnings are going to be very annoying for people. I could perhaps split the patch up and work through things one by one.

Yeah, we want to find some way to remove the warnings, and I think splitting up and working piece by piece will be necessary.

Has anyone done anything on that front?
If not, I would take a deep breath and try to tackle this one by one. I was grudgingly ignoring this in U-Boot so far, but it popped up in Trusted Firmware now as well, so I have a business reason (TM).

Cheers,
Andre

Quoted 19 lines

I think the very first step, is to find definitive info on what exactly the defined behaviour of C is with a signed vs. unsigned comparison.

The help text of -Wsign-compare seems to imply that assuming:

	signed int a;
	unsigned int b;

then

	if (a < b) ...

is equivalent to

	if ((unsigned int)a < b) ...

But I thought that this was not the case. Rather, I thought it was supposed to always evaluate to true if b > INT_MAX. We need to know which is the case as a starting point.

A message that is not in the archive (sent off-list or not yet received): a6622ba6-71f5-135a-f215-bc3741d9b721-5wv7dgnIgG8@public.gmane.org

#8 lore raw

Hi André,

Quoted 47 lines · André Przywara

On 17/01/2020 09:23, David Gibson wrote:

Hi,

Quoted 42 lines · Simon Glass
Quoted 26 lines

Hi David,

Quoted 15 lines · David Gibson
Quoted 10 lines · Simon Glass

These warnings appear when building U-Boot on x86 and some other targets. Correct them by adding casts.

Example:

scripts/dtc/libfdt/fdt.c: In function ‘fdt_offset_ptr’:
scripts/dtc/libfdt/fdt.c:137:18: warning: comparison of integer expressions of different signedness: ‘unsigned int’ and ‘int’ [-Wsign-compare]
if ((absoffset < offset)

Hmm. So squashing warnings is certainly a good thing in general.

Quoted 7 lines

Unfortunately, I'm really uncomfortable with most of these changes. In a number of cases they are outright wrong. In most of the others, the code was already correct. I dislike adding casts to suppress spurious warnings on correct code because that can end up hiding real problems which might be introduced by future changes.

Case by case details below.

Quoted 7 lines

Thanks for the review. I agree this is all really horrible, particularly in light of the fact that it doesn't fix bugs and perhaps introduces some.

This was just a quick patch to silence the warnings. If we do make fixes here we should really make sure that there are test cases to trigger each check. I suspect we have some but not all.

Yeah, adding some safety test cases for egregiously bad input like negative buffer sizes is probably a good idea.

Quoted 3 lines

What do you think we should do? The warnings are going to be very annoying for people. I could perhaps split the patch up and work through things one by one.

Quoted 7 lines
Quoted 2 lines

Yeah, we want to find some way to remove the warnings, and I think splitting up and working piece by piece will be necessary.

Has anyone done anything on that front?
If not, I would take a deep breath and try to tackle this one by one. I was grudgingly ignoring this in U-Boot so far, but it popped up in Trusted Firmware now as well, so I have a business reason (TM).

No, I never got back to it. It would be great if you could take it on!

Regards,
Simon

#9 lore raw
In reply to a message not in the archive
Quoted 46 lines · André Przywara

On 17/01/2020 09:23, David Gibson wrote:

Hi,

Quoted 42 lines · Simon Glass
Quoted 26 lines

Hi David,

Quoted 15 lines · David Gibson
Quoted 10 lines · Simon Glass

These warnings appear when building U-Boot on x86 and some other targets. Correct them by adding casts.

Example:

scripts/dtc/libfdt/fdt.c: In function ‘fdt_offset_ptr’:
scripts/dtc/libfdt/fdt.c:137:18: warning: comparison of integer expressions of different signedness: ‘unsigned int’ and ‘int’ [-Wsign-compare]
if ((absoffset < offset)

Hmm. So squashing warnings is certainly a good thing in general.

Quoted 7 lines

Unfortunately, I'm really uncomfortable with most of these changes. In a number of cases they are outright wrong. In most of the others, the code was already correct. I dislike adding casts to suppress spurious warnings on correct code because that can end up hiding real problems which might be introduced by future changes.

Case by case details below.

Quoted 7 lines

Thanks for the review. I agree this is all really horrible, particularly in light of the fact that it doesn't fix bugs and perhaps introduces some.

This was just a quick patch to silence the warnings. If we do make fixes here we should really make sure that there are test cases to trigger each check. I suspect we have some but not all.

Yeah, adding some safety test cases for egregiously bad input like negative buffer sizes is probably a good idea.

Quoted 3 lines

What do you think we should do? The warnings are going to be very annoying for people. I could perhaps split the patch up and work through things one by one.

Quoted 26 lines
Quoted 2 lines

Yeah, we want to find some way to remove the warnings, and I think splitting up and working piece by piece will be necessary.

Has anyone done anything on that front?
If not, I would take a deep breath and try to tackle this one by one. I was grudgingly ignoring this in U-Boot so far, but it popped up in Trusted Firmware now as well, so I have a business reason (TM).

Quoted 18 lines

I think the very first step, is to find definitive info on what exactly the defined behaviour of C is with a signed vs. unsigned comparison.

The help text of -Wsign-compare seems to imply that assuming:

	signed int a;
	unsigned int b;

then

	if (a < b) ...

is equivalent to

	if ((unsigned int)a < b) ...

But I thought that this was not the case. Rather, I thought it was supposed to always evaluate to true if b > INT_MAX. We need to know which is the case as a starting point.

I since had a brief look at this, and it appears I was wrong about this behaviour, which makes this whole project rather more urgent. I'd still appreciate someone looking at the standards to double check, though.

I'm unlikely to have time to look at this myself, though, so I'd be very happy if you took this on, and I'll do my absolute best to review and merge promptly.

Fwiw, the more "bite-size" the patches are, the easier it is for me to review. So I'd suggest fixing just a small set of related cases at a time, with a clear justification for why the new semantics are correct in the commit message.

Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson

A message that is not in the archive (sent off-list or not yet received): [email protected]

#11 lore raw

On 21/09/2020 07:00, David Gibson wrote:

Hi David,

Quoted 79 lines · André Przywara
Quoted 46 lines · David Gibson

Hi,

Quoted 42 lines · Simon Glass
Quoted 26 lines

Hi David,

Quoted 15 lines · David Gibson
Quoted 10 lines · Simon Glass

These warnings appear when building U-Boot on x86 and some other targets. Correct them by adding casts.

Example:

scripts/dtc/libfdt/fdt.c: In function ‘fdt_offset_ptr’:
scripts/dtc/libfdt/fdt.c:137:18: warning: comparison of integer expressions of different signedness: ‘unsigned int’ and ‘int’ [-Wsign-compare]
if ((absoffset < offset)

Hmm. So squashing warnings is certainly a good thing in general.

Quoted 7 lines

Unfortunately, I'm really uncomfortable with most of these changes. In a number of cases they are outright wrong. In most of the others, the code was already correct. I dislike adding casts to suppress spurious warnings on correct code because that can end up hiding real problems which might be introduced by future changes.

Case by case details below.

Quoted 7 lines

Thanks for the review. I agree this is all really horrible, particularly in light of the fact that it doesn't fix bugs and perhaps introduces some.

This was just a quick patch to silence the warnings. If we do make fixes here we should really make sure that there are test cases to trigger each check. I suspect we have some but not all.

Yeah, adding some safety test cases for egregiously bad input like negative buffer sizes is probably a good idea.

Quoted 3 lines

What do you think we should do? The warnings are going to be very annoying for people. I could perhaps split the patch up and work through things one by one.

Quoted 26 lines
Quoted 2 lines

Yeah, we want to find some way to remove the warnings, and I think splitting up and working piece by piece will be necessary.

Has anyone done anything on that front?
If not, I would take a deep breath and try to tackle this one by one. I was grudgingly ignoring this in U-Boot so far, but it popped up in Trusted Firmware now as well, so I have a business reason (TM).

Quoted 18 lines

I think the very first step, is to find definitive info on what exactly the defined behaviour of C is with a signed vs. unsigned comparison.

The help text of -Wsign-compare seems to imply that assuming:

	signed int a;
	unsigned int b;

then

	if (a < b) ...

is equivalent to

	if ((unsigned int)a < b) ...

But I thought that this was not the case. Rather, I thought it was supposed to always evaluate to true if b > INT_MAX. We need to know which is the case as a starting point.

I since had a brief look at this, and it appears I was wrong about this behaviour, which makes this whole project rather more urgent. I'd still appreciate someone looking at the standards to double check, though.

Quoted 8 lines

I'm unlikely to have time to look at this myself, though, so I'd be very happy if you took this on, and I'll do my absolute best to review and merge promptly.

Fwiw, the more "bite-size" the patches are, the easier it is for me to review. So I'd suggest fixing just a small set of related cases at a time, with a clear justification for why the new semantics are correct in the commit message.

Yes, breaking this down (as suggested earlier) was the plan. Either by group of functions or by type of fix used, not decided yet. And I will try to add as much rationale to the commit messages as possible.

- I guess I can't change the external interface, to amend the signedness in function parameters?

- Can we assume (or stipulate?) that a DTB is always smaller than 2GB? The DT spec doesn't explicitly mention a limit, but the header uses uint32_t for size fields. libfdt seems to assume the the actual structure block is smaller than 2GB already (by using ints for node offsets). So that leaves the corner case of totalsize being potentially larger than 2GB only. Do we care about this?

Cheers,
Andre

A message that is not in the archive (sent off-list or not yet received): bc44bc93-0356-8e15-20f7-4cce77edba27-5wv7dgnIgG8@public.gmane.org

#13 lore raw

Hi André,

Quoted 100 lines · André Przywara

On 21/09/2020 07:00, David Gibson wrote:

Hi David,

Quoted 79 lines · André Przywara
Quoted 46 lines · David Gibson

Hi,

Quoted 42 lines · Simon Glass
Quoted 26 lines

Hi David,

Quoted 15 lines · David Gibson
Quoted 10 lines · Simon Glass

These warnings appear when building U-Boot on x86 and some other targets. Correct them by adding casts.

Example:

scripts/dtc/libfdt/fdt.c: In function ‘fdt_offset_ptr’:
scripts/dtc/libfdt/fdt.c:137:18: warning: comparison of integer expressions of different signedness: ‘unsigned int’ and ‘int’ [-Wsign-compare]
if ((absoffset < offset)

Hmm. So squashing warnings is certainly a good thing in general.

Quoted 7 lines

Unfortunately, I'm really uncomfortable with most of these changes. In a number of cases they are outright wrong. In most of the others, the code was already correct. I dislike adding casts to suppress spurious warnings on correct code because that can end up hiding real problems which might be introduced by future changes.

Case by case details below.

Quoted 7 lines

Thanks for the review. I agree this is all really horrible, particularly in light of the fact that it doesn't fix bugs and perhaps introduces some.

This was just a quick patch to silence the warnings. If we do make fixes here we should really make sure that there are test cases to trigger each check. I suspect we have some but not all.

Yeah, adding some safety test cases for egregiously bad input like negative buffer sizes is probably a good idea.

Quoted 3 lines

What do you think we should do? The warnings are going to be very annoying for people. I could perhaps split the patch up and work through things one by one.

Quoted 26 lines
Quoted 2 lines

Yeah, we want to find some way to remove the warnings, and I think splitting up and working piece by piece will be necessary.

Has anyone done anything on that front?
If not, I would take a deep breath and try to tackle this one by one. I was grudgingly ignoring this in U-Boot so far, but it popped up in Trusted Firmware now as well, so I have a business reason (TM).

Quoted 18 lines

I think the very first step, is to find definitive info on what exactly the defined behaviour of C is with a signed vs. unsigned comparison.

The help text of -Wsign-compare seems to imply that assuming:

    signed int a;
    unsigned int b;

then

    if (a < b) ...

is equivalent to

    if ((unsigned int)a < b) ...

But I thought that this was not the case. Rather, I thought it was supposed to always evaluate to true if b > INT_MAX. We need to know which is the case as a starting point.

I since had a brief look at this, and it appears I was wrong about this behaviour, which makes this whole project rather more urgent. I'd still appreciate someone looking at the standards to double check, though.

Quoted 8 lines

I'm unlikely to have time to look at this myself, though, so I'd be very happy if you took this on, and I'll do my absolute best to review and merge promptly.

Fwiw, the more "bite-size" the patches are, the easier it is for me to review. So I'd suggest fixing just a small set of related cases at a time, with a clear justification for why the new semantics are correct in the commit message.

Yes, breaking this down (as suggested earlier) was the plan. Either by group of functions or by type of fix used, not decided yet. And I will try to add as much rationale to the commit messages as possible.

- I guess I can't change the external interface, to amend the signedness in function parameters?

Quoted 6 lines

- Can we assume (or stipulate?) that a DTB is always smaller than 2GB? The DT spec doesn't explicitly mention a limit, but the header uses uint32_t for size fields. libfdt seems to assume the the actual structure block is smaller than 2GB already (by using ints for node offsets). So that leaves the corner case of totalsize being potentially larger than 2GB only. Do we care about this?

I feel that 2GB should be enough for everyone(TM). U-Boot supports images being outside the FIT as well.

Regards,
Simon

#14 lore raw
In reply to a message not in the archive
Quoted 87 lines · André Przywara

On 21/09/2020 07:00, David Gibson wrote:

Hi David,

Quoted 83 lines · André Przywara
Quoted 46 lines · David Gibson

Hi,

Quoted 42 lines · Simon Glass
Quoted 26 lines

Hi David,

Quoted 15 lines · David Gibson
Quoted 10 lines · Simon Glass

These warnings appear when building U-Boot on x86 and some other targets. Correct them by adding casts.

Example:

scripts/dtc/libfdt/fdt.c: In function ‘fdt_offset_ptr’:
scripts/dtc/libfdt/fdt.c:137:18: warning: comparison of integer expressions of different signedness: ‘unsigned int’ and ‘int’ [-Wsign-compare]
if ((absoffset < offset)

Hmm. So squashing warnings is certainly a good thing in general.

Quoted 7 lines

Unfortunately, I'm really uncomfortable with most of these changes. In a number of cases they are outright wrong. In most of the others, the code was already correct. I dislike adding casts to suppress spurious warnings on correct code because that can end up hiding real problems which might be introduced by future changes.

Case by case details below.

Quoted 7 lines

Thanks for the review. I agree this is all really horrible, particularly in light of the fact that it doesn't fix bugs and perhaps introduces some.

This was just a quick patch to silence the warnings. If we do make fixes here we should really make sure that there are test cases to trigger each check. I suspect we have some but not all.

Yeah, adding some safety test cases for egregiously bad input like negative buffer sizes is probably a good idea.

Quoted 3 lines

What do you think we should do? The warnings are going to be very annoying for people. I could perhaps split the patch up and work through things one by one.

Quoted 26 lines
Quoted 2 lines

Yeah, we want to find some way to remove the warnings, and I think splitting up and working piece by piece will be necessary.

Has anyone done anything on that front?
If not, I would take a deep breath and try to tackle this one by one. I was grudgingly ignoring this in U-Boot so far, but it popped up in Trusted Firmware now as well, so I have a business reason (TM).

Quoted 18 lines

I think the very first step, is to find definitive info on what exactly the defined behaviour of C is with a signed vs. unsigned comparison.

The help text of -Wsign-compare seems to imply that assuming:

	signed int a;
	unsigned int b;

then

	if (a < b) ...

is equivalent to

	if ((unsigned int)a < b) ...

But I thought that this was not the case. Rather, I thought it was supposed to always evaluate to true if b > INT_MAX. We need to know which is the case as a starting point.

I since had a brief look at this, and it appears I was wrong about this behaviour, which makes this whole project rather more urgent. I'd still appreciate someone looking at the standards to double check, though.

I'm unlikely to have time to look at this myself, though, so I'd be very happy if you took this on, and I'll do my absolute best to review and merge promptly.

Quoted 8 lines
Quoted 4 lines

Fwiw, the more "bite-size" the patches are, the easier it is for me to review. So I'd suggest fixing just a small set of related cases at a time, with a clear justification for why the new semantics are correct in the commit message.

Yes, breaking this down (as suggested earlier) was the plan. Either by group of functions or by type of fix used, not decided yet. And I will try to add as much rationale to the commit messages as possible.

👍

Quoted 2 lines

- I guess I can't change the external interface, to amend the signedness in function parameters?

On the external interface? No, I don't think that's worth the damage.

On internal functions, absolutely feel free to update the interface if that's the cleanest approach.

Quoted 1 line

- Can we assume (or stipulate?) that a DTB is always smaller than 2GB?

Yes. In fact, fdt_check_header() already verifies that.

Quoted 5 lines

The DT spec doesn't explicitly mention a limit, but the header uses uint32_t for size fields. libfdt seems to assume the the actual structure block is smaller than 2GB already (by using ints for node offsets). So that leaves the corner case of totalsize being potentially larger than 2GB only. Do we care about this?

No, as above we already check that that totalsize < 2^31.

Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson