Browse Source

tests: Fix signedness comparisons warnings

With -Wsign-compare, compilers warn about a mismatching signedness in
comparisons in various files in the tests/ directory.

For about half of the cases we can simply change the signed variable to
be of an unsigned type, because they will never need to store negative
values (which is the best fix of the problem).

In the remaining cases we can cast the signed variable to an unsigned
type, provided we know for sure it is not negative.
We see two different scenarios here:
- We either just explicitly checked for this variable to be positive
  (if (rc < 0) FAIL();), or
- We rely on a function returning only positive values in the "length"
  pointer if the function returned successfully: which we just checked.

At two occassions we compare with a constant "-1" (even though the
variable is unsigned), so we just change this to ~0U to create an
unsigned comparison value.

Since this is about the tests, let's also add explicit tests for those
values really not being negative.

This fixes "make tests" (but not "make check" yet), when compiled
with -Wsign-compare.

Signed-off-by: Andre Przywara <andre.przywara@arm.com>
Message-Id: <20210618172030.9684-2-andre.przywara@arm.com>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
main
Andre Przywara 3 years ago committed by David Gibson
parent
commit
d966f08fcd
  1. 2
      tests/dumptrees.c
  2. 2
      tests/fs_tree1.c
  3. 4
      tests/get_name.c
  4. 2
      tests/integer-expressions.c
  5. 3
      tests/nopulate.c
  6. 6
      tests/overlay.c
  7. 2
      tests/phandle_format.c
  8. 2
      tests/property_iterate.c
  9. 2
      tests/references.c
  10. 10
      tests/set_name.c
  11. 2
      tests/subnode_iterate.c
  12. 2
      tests/tests.h
  13. 12
      tests/testutils.c

2
tests/dumptrees.c

@ -32,7 +32,7 @@ static struct {


int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
int i; unsigned int i;


if (argc != 2) { if (argc != 2) {
fprintf(stderr, "Missing output directory argument\n"); fprintf(stderr, "Missing output directory argument\n");

2
tests/fs_tree1.c

@ -54,7 +54,7 @@ static void mkfile(const char *name, void *data, size_t len)
rc = write(fd, data, len); rc = write(fd, data, len);
if (rc < 0) if (rc < 0)
FAIL("write(\"%s\"): %s", name, strerror(errno)); FAIL("write(\"%s\"): %s", name, strerror(errno));
if (rc != len) if ((unsigned)rc != len)
FAIL("write(\"%s\"): short write", name); FAIL("write(\"%s\"): short write", name);
rc = close(fd); rc = close(fd);

4
tests/get_name.c

@ -34,12 +34,14 @@ static void check_name(void *fdt, const char *path)
offset, getname, len); offset, getname, len);
if (!getname) if (!getname)
FAIL("fdt_get_name(%d): %s", offset, fdt_strerror(len)); FAIL("fdt_get_name(%d): %s", offset, fdt_strerror(len));
if (len < 0)
FAIL("negative name length (%d) for returned node name\n", len);


if (strcmp(getname, checkname) != 0) if (strcmp(getname, checkname) != 0)
FAIL("fdt_get_name(%s) returned \"%s\" instead of \"%s\"", FAIL("fdt_get_name(%s) returned \"%s\" instead of \"%s\"",
path, getname, checkname); path, getname, checkname);


if (len != strlen(getname)) if ((unsigned)len != strlen(getname))
FAIL("fdt_get_name(%s) returned length %d instead of %zd", FAIL("fdt_get_name(%s) returned length %d instead of %zd",
path, len, strlen(getname)); path, len, strlen(getname));



2
tests/integer-expressions.c

@ -59,7 +59,7 @@ int main(int argc, char *argv[])
void *fdt; void *fdt;
const fdt32_t *res; const fdt32_t *res;
int reslen; int reslen;
int i; unsigned int i;


test_init(argc, argv); test_init(argc, argv);



3
tests/nopulate.c

@ -43,7 +43,8 @@ static int nopulate_struct(char *buf, const char *fdt)
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
char *fdt, *fdt2, *buf; char *fdt, *fdt2, *buf;
int newsize, struct_start, struct_end_old, struct_end_new, delta; int newsize, struct_end_old, struct_end_new, delta;
unsigned int struct_start;
const char *inname; const char *inname;
char outname[PATH_MAX]; char outname[PATH_MAX];



6
tests/overlay.c

@ -35,7 +35,11 @@ static int fdt_getprop_u32_by_poffset(void *fdt, const char *path,
return node_off; return node_off;


val = fdt_getprop(fdt, node_off, name, &len); val = fdt_getprop(fdt, node_off, name, &len);
if (!val || (len < (sizeof(uint32_t) * (poffset + 1)))) if (val && len < 0)
FAIL("fdt_getprop() returns negative length");
if (!val && len < 0)
return len;
if (!val || ((unsigned)len < (sizeof(uint32_t) * (poffset + 1))))
return -FDT_ERR_NOTFOUND; return -FDT_ERR_NOTFOUND;


*out = fdt32_to_cpu(*(val + poffset)); *out = fdt32_to_cpu(*(val + poffset));

2
tests/phandle_format.c

@ -45,7 +45,7 @@ int main(int argc, char *argv[])
FAIL("fdt_path_offset(/node4): %s", fdt_strerror(n4)); FAIL("fdt_path_offset(/node4): %s", fdt_strerror(n4));


h4 = fdt_get_phandle(fdt, n4); h4 = fdt_get_phandle(fdt, n4);
if ((h4 == 0) || (h4 == -1)) if ((h4 == 0) || (h4 == ~0U))
FAIL("/node4 has bad phandle 0x%x\n", h4); FAIL("/node4 has bad phandle 0x%x\n", h4);


if (phandle_format & PHANDLE_LEGACY) if (phandle_format & PHANDLE_LEGACY)

2
tests/property_iterate.c

@ -23,7 +23,7 @@ static void test_node(void *fdt, int parent_offset)
uint32_t properties; uint32_t properties;
const fdt32_t *prop; const fdt32_t *prop;
int offset, property; int offset, property;
int count; unsigned int count;
int len; int len;


/* /*

2
tests/references.c

@ -106,7 +106,7 @@ int main(int argc, char *argv[])
if ((h4 == 0x2000) || (h4 == 0x1) || (h4 == 0)) if ((h4 == 0x2000) || (h4 == 0x1) || (h4 == 0))
FAIL("/node4 has bad phandle, 0x%x", h4); FAIL("/node4 has bad phandle, 0x%x", h4);


if ((h5 == 0) || (h5 == -1)) if ((h5 == 0) || (h5 == ~0U))
FAIL("/node5 has bad phandle, 0x%x", h5); FAIL("/node5 has bad phandle, 0x%x", h5);
if ((h5 == h4) || (h5 == h2) || (h5 == h1)) if ((h5 == h4) || (h5 == h2) || (h5 == h1))
FAIL("/node5 has duplicate phandle, 0x%x", h5); FAIL("/node5 has duplicate phandle, 0x%x", h5);

10
tests/set_name.c

@ -39,7 +39,11 @@ static void check_set_name(void *fdt, const char *path, const char *newname)
FAIL("fdt_get_name(%s) returned \"%s\" instead of \"%s\"", FAIL("fdt_get_name(%s) returned \"%s\" instead of \"%s\"",
path, getname, oldname); path, getname, oldname);


if (len != strlen(getname)) if (len < 0)
FAIL("fdt_get_name(%s) returned negative length: %d",
path, len);

if ((unsigned)len != strlen(getname))
FAIL("fdt_get_name(%s) returned length %d instead of %zd", FAIL("fdt_get_name(%s) returned length %d instead of %zd",
path, len, strlen(getname)); path, len, strlen(getname));


@ -51,12 +55,14 @@ static void check_set_name(void *fdt, const char *path, const char *newname)
getname = fdt_get_name(fdt, offset, &len); getname = fdt_get_name(fdt, offset, &len);
if (!getname) if (!getname)
FAIL("fdt_get_name(%d): %s", offset, fdt_strerror(len)); FAIL("fdt_get_name(%d): %s", offset, fdt_strerror(len));
if (len < 0)
FAIL("negative name length (%d) for returned node name\n", len);


if (strcmp(getname, newname) != 0) if (strcmp(getname, newname) != 0)
FAIL("fdt_get_name(%s) returned \"%s\" instead of \"%s\"", FAIL("fdt_get_name(%s) returned \"%s\" instead of \"%s\"",
path, getname, newname); path, getname, newname);


if (len != strlen(getname)) if ((unsigned)len != strlen(getname))
FAIL("fdt_get_name(%s) returned length %d instead of %zd", FAIL("fdt_get_name(%s) returned length %d instead of %zd",
path, len, strlen(getname)); path, len, strlen(getname));
} }

2
tests/subnode_iterate.c

@ -23,7 +23,7 @@ static void test_node(void *fdt, int parent_offset)
uint32_t subnodes; uint32_t subnodes;
const fdt32_t *prop; const fdt32_t *prop;
int offset; int offset;
int count; unsigned int count;
int len; int len;


/* This property indicates the number of subnodes to expect */ /* This property indicates the number of subnodes to expect */

2
tests/tests.h

@ -83,7 +83,7 @@ void cleanup(void);
void check_mem_rsv(void *fdt, int n, uint64_t addr, uint64_t size); void check_mem_rsv(void *fdt, int n, uint64_t addr, uint64_t size);


void check_property(void *fdt, int nodeoffset, const char *name, void check_property(void *fdt, int nodeoffset, const char *name,
int len, const void *val); unsigned int len, const void *val);
#define check_property_cell(fdt, nodeoffset, name, val) \ #define check_property_cell(fdt, nodeoffset, name, val) \
({ \ ({ \
fdt32_t x = cpu_to_fdt32(val); \ fdt32_t x = cpu_to_fdt32(val); \

12
tests/testutils.c

@ -88,7 +88,7 @@ void check_mem_rsv(void *fdt, int n, uint64_t addr, uint64_t size)
} }


void check_property(void *fdt, int nodeoffset, const char *name, void check_property(void *fdt, int nodeoffset, const char *name,
int len, const void *val) unsigned int len, const void *val)
{ {
const struct fdt_property *prop; const struct fdt_property *prop;
int retlen, namelen; int retlen, namelen;
@ -101,6 +101,9 @@ void check_property(void *fdt, int nodeoffset, const char *name,
if (! prop) if (! prop)
FAIL("Error retrieving \"%s\" pointer: %s", name, FAIL("Error retrieving \"%s\" pointer: %s", name,
fdt_strerror(retlen)); fdt_strerror(retlen));
if (retlen < 0)
FAIL("negative name length (%d) for returned property\n",
retlen);


tag = fdt32_to_cpu(prop->tag); tag = fdt32_to_cpu(prop->tag);
nameoff = fdt32_to_cpu(prop->nameoff); nameoff = fdt32_to_cpu(prop->nameoff);
@ -112,13 +115,16 @@ void check_property(void *fdt, int nodeoffset, const char *name,
propname = fdt_get_string(fdt, nameoff, &namelen); propname = fdt_get_string(fdt, nameoff, &namelen);
if (!propname) if (!propname)
FAIL("Couldn't get property name: %s", fdt_strerror(namelen)); FAIL("Couldn't get property name: %s", fdt_strerror(namelen));
if (namelen != strlen(propname)) if (namelen < 0)
FAIL("negative name length (%d) for returned string\n",
namelen);
if ((unsigned)namelen != strlen(propname))
FAIL("Incorrect prop name length: %d instead of %zd", FAIL("Incorrect prop name length: %d instead of %zd",
namelen, strlen(propname)); namelen, strlen(propname));
if (!streq(propname, name)) if (!streq(propname, name))
FAIL("Property name mismatch \"%s\" instead of \"%s\"", FAIL("Property name mismatch \"%s\" instead of \"%s\"",
propname, name); propname, name);
if (proplen != retlen) if (proplen != (unsigned)retlen)
FAIL("Length retrieved for \"%s\" by fdt_get_property()" FAIL("Length retrieved for \"%s\" by fdt_get_property()"
" differs from stored length (%d != %d)", " differs from stored length (%d != %d)",
name, retlen, proplen); name, retlen, proplen);

Loading…
Cancel
Save