Browse Source

libfdt: Abolish encoding of error codes into pointers

This patch abolishes the non-standard and confusing encoding of errors
into pointer return values.  The only functions still returning such a
potentially encoded pointer are fdt_get_property() and fdt_getprop().
Those functions also return a length via an (int *).  With this patch
those functions instead now return NULL on any error, and return the
code indicating the type of error in the length paramater.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
main
David Gibson 18 years ago
parent
commit
a7ee95ded6
  1. 41
      fdt_ro.c
  2. 58
      fdt_rw.c
  3. 10
      fdt_wip.c
  4. 4
      libfdt.h
  5. 1
      libfdt_internal.h
  6. 16
      tests/del_property.c
  7. 15
      tests/nop_property.c
  8. 13
      tests/notfound.c
  9. 5
      tests/testutils.c
  10. 4
      tests/truncated_property.c

41
fdt_ro.c

@ -30,13 +30,6 @@ @@ -30,13 +30,6 @@
return OFFSET_ERROR(err); \
}

#define PTR_CHECK_HEADER(fdt) \
{ \
int err; \
if ((err = _fdt_check_header(fdt)) != 0) \
return PTR_ERROR(err); \
}

static int offset_streq(const void *fdt, int offset,
const char *s, int len)
{
@ -153,25 +146,30 @@ struct fdt_property *fdt_get_property(const void *fdt, @@ -153,25 +146,30 @@ struct fdt_property *fdt_get_property(const void *fdt,
struct fdt_property *prop;
int namestroff;
int offset, nextoffset;
int err;

PTR_CHECK_HEADER(fdt);
if ((err = _fdt_check_header(fdt)) != 0)
goto fail;

err = FDT_ERR_BADOFFSET;
if (nodeoffset % FDT_TAGSIZE)
return PTR_ERROR(FDT_ERR_BADOFFSET);
goto fail;

tag = _fdt_next_tag(fdt, nodeoffset, &nextoffset);
if (tag != FDT_BEGIN_NODE)
return PTR_ERROR(FDT_ERR_BADOFFSET);
goto fail;

do {
offset = nextoffset;
err = FDT_ERR_INTERNAL;
if (offset % FDT_TAGSIZE)
return PTR_ERROR(FDT_ERR_INTERNAL);
goto fail;

tag = _fdt_next_tag(fdt, offset, &nextoffset);
switch (tag) {
case FDT_END:
return PTR_ERROR(FDT_ERR_TRUNCATED);
err = FDT_ERR_TRUNCATED;
goto fail;

case FDT_BEGIN_NODE:
level++;
@ -185,9 +183,10 @@ struct fdt_property *fdt_get_property(const void *fdt, @@ -185,9 +183,10 @@ struct fdt_property *fdt_get_property(const void *fdt,
if (level != 0)
continue;

err = FDT_ERR_BADSTRUCTURE;
prop = fdt_offset_ptr_typed(fdt, offset, prop);
if (! prop)
return PTR_ERROR(FDT_ERR_BADSTRUCTURE);
goto fail;
namestroff = fdt32_to_cpu(prop->nameoff);
if (streq(fdt_string(fdt, namestroff), name)) {
/* Found it! */
@ -195,7 +194,7 @@ struct fdt_property *fdt_get_property(const void *fdt, @@ -195,7 +194,7 @@ struct fdt_property *fdt_get_property(const void *fdt,
prop = fdt_offset_ptr(fdt, offset,
sizeof(*prop)+len);
if (! prop)
return PTR_ERROR(FDT_ERR_BADSTRUCTURE);
goto fail;

if (lenp)
*lenp = len;
@ -208,22 +207,26 @@ struct fdt_property *fdt_get_property(const void *fdt, @@ -208,22 +207,26 @@ struct fdt_property *fdt_get_property(const void *fdt,
break;

default:
return PTR_ERROR(FDT_ERR_BADSTRUCTURE);
err = FDT_ERR_BADSTRUCTURE;
goto fail;
}
} while (level >= 0);

return PTR_ERROR(FDT_ERR_NOTFOUND);
err = FDT_ERR_NOTFOUND;
fail:
if (lenp)
*lenp = -err;
return NULL;
}

void *fdt_getprop(const void *fdt, int nodeoffset,
const char *name, int *lenp)
{
const struct fdt_property *prop;
int err;

prop = fdt_get_property(fdt, nodeoffset, name, lenp);
if ((err = fdt_ptr_error(prop)))
return PTR_ERROR(err);
if (! prop)
return NULL;

return prop->data;
}

58
fdt_rw.c

@ -117,31 +117,29 @@ static int _find_add_string(void *fdt, const char *s) @@ -117,31 +117,29 @@ static int _find_add_string(void *fdt, const char *s)
return (new - strtab);
}

static struct fdt_property *_resize_property(void *fdt, int nodeoffset,
const char *name, int len)
static int _resize_property(void *fdt, int nodeoffset, const char *name, int len,
struct fdt_property **prop)
{
struct fdt_property *prop;
int oldlen;
int err;

prop = fdt_get_property(fdt, nodeoffset, name, &oldlen);
if ((err = fdt_ptr_error(prop)))
return PTR_ERROR(err);
*prop = fdt_get_property(fdt, nodeoffset, name, &oldlen);
if (! (*prop))
return -oldlen;

if ((err = _blob_splice_struct(fdt, prop->data,
if ((err = _blob_splice_struct(fdt, (*prop)->data,
ALIGN(oldlen, FDT_TAGSIZE),
ALIGN(len, FDT_TAGSIZE))))
return PTR_ERROR(err);
return err;

prop->len = cpu_to_fdt32(len);
return prop;
(*prop)->len = cpu_to_fdt32(len);
return 0;
}

static struct fdt_property *_add_property(void *fdt, int nodeoffset,
const char *name, int len)
static int _add_property(void *fdt, int nodeoffset, const char *name, int len,
struct fdt_property **prop)
{
uint32_t tag;
struct fdt_property *prop;
int proplen;
int nextoffset;
int namestroff;
@ -149,23 +147,23 @@ static struct fdt_property *_add_property(void *fdt, int nodeoffset, @@ -149,23 +147,23 @@ static struct fdt_property *_add_property(void *fdt, int nodeoffset,

tag = _fdt_next_tag(fdt, nodeoffset, &nextoffset);
if (tag != FDT_BEGIN_NODE)
return PTR_ERROR(FDT_ERR_BADOFFSET);
return FDT_ERR_BADOFFSET;

namestroff = _find_add_string(fdt, name);
if (namestroff < 0)
return PTR_ERROR(-namestroff);
return -namestroff;

prop = _fdt_offset_ptr(fdt, nextoffset);
proplen = sizeof(*prop) + ALIGN(len, FDT_TAGSIZE);
*prop = _fdt_offset_ptr(fdt, nextoffset);
proplen = sizeof(**prop) + ALIGN(len, FDT_TAGSIZE);

err = _blob_splice_struct(fdt, prop, 0, proplen);
err = _blob_splice_struct(fdt, *prop, 0, proplen);
if (err)
return PTR_ERROR(err);
return err;

prop->tag = cpu_to_fdt32(FDT_PROP);
prop->nameoff = cpu_to_fdt32(namestroff);
prop->len = cpu_to_fdt32(len);
return prop;
(*prop)->tag = cpu_to_fdt32(FDT_PROP);
(*prop)->nameoff = cpu_to_fdt32(namestroff);
(*prop)->len = cpu_to_fdt32(len);
return 0;
}

int fdt_setprop(void *fdt, int nodeoffset, const char *name,
@ -177,12 +175,9 @@ int fdt_setprop(void *fdt, int nodeoffset, const char *name, @@ -177,12 +175,9 @@ int fdt_setprop(void *fdt, int nodeoffset, const char *name,
if ((err = rw_check_header(fdt)))
return err;

prop = _resize_property(fdt, nodeoffset, name, len);
err = fdt_ptr_error(prop);
if (err == FDT_ERR_NOTFOUND) {
prop = _add_property(fdt, nodeoffset, name, len);
err = fdt_ptr_error(prop);
}
err = _resize_property(fdt, nodeoffset, name, len, &prop);
if (err == FDT_ERR_NOTFOUND)
err = _add_property(fdt, nodeoffset, name, len, &prop);
if (err)
return err;

@ -194,13 +189,12 @@ int fdt_delprop(void *fdt, int nodeoffset, const char *name) @@ -194,13 +189,12 @@ int fdt_delprop(void *fdt, int nodeoffset, const char *name)
{
struct fdt_property *prop;
int len, proplen;
int err;

RW_OFFSET_CHECK_HEADER(fdt);

prop = fdt_get_property(fdt, nodeoffset, name, &len);
if ((err = fdt_ptr_error(prop)))
return err;
if (! prop)
return -len;

proplen = sizeof(*prop) + ALIGN(len, FDT_TAGSIZE);
return _blob_splice_struct(fdt, prop, proplen, 0);

10
fdt_wip.c

@ -28,11 +28,10 @@ int fdt_setprop_inplace(void *fdt, int nodeoffset, const char *name, @@ -28,11 +28,10 @@ int fdt_setprop_inplace(void *fdt, int nodeoffset, const char *name,
{
void *propval;
int proplen;
int err;

propval = fdt_getprop(fdt, nodeoffset, name, &proplen);
if ((err = fdt_ptr_error(propval)))
return err;
if (! propval)
return -proplen;

if (proplen != len)
return FDT_ERR_SIZE_MISMATCH;
@ -53,11 +52,10 @@ int fdt_nop_property(void *fdt, int nodeoffset, const char *name) @@ -53,11 +52,10 @@ int fdt_nop_property(void *fdt, int nodeoffset, const char *name)
{
struct fdt_property *prop;
int len;
int err;

prop = fdt_get_property(fdt, nodeoffset, name, &len);
if ((err = fdt_ptr_error(prop)))
return err;
if (! prop)
return -len;

nop_region(prop, len + sizeof(*prop));


4
libfdt.h

@ -69,8 +69,8 @@ void *fdt_offset_ptr(const void *fdt, int offset, int checklen); @@ -69,8 +69,8 @@ void *fdt_offset_ptr(const void *fdt, int offset, int checklen);
#define fdt_offset_error(offset) \
( (offset) < 0 ? -(offset) : 0 )

#define fdt_ptr_error(ptr) \
( (((long)(ptr) < 0) && ((long)(ptr) >= -FDT_ERR_MAX)) ? -(long)(ptr) : 0 )
#define fdt_ptrlen_error(p, len) \
( (p) ? 0 : -(len) )

int fdt_move(const void *fdt, void *buf, int bufsize);


1
libfdt_internal.h

@ -37,7 +37,6 @@ static inline void *_fdt_offset_ptr(const struct fdt_header *fdt, int offset) @@ -37,7 +37,6 @@ static inline void *_fdt_offset_ptr(const struct fdt_header *fdt, int offset)
}

#define OFFSET_ERROR(code) -(code)
#define PTR_ERROR(code) (void *)(-(long)(code))

#define SW_MAGIC (~FDT_MAGIC)


16
tests/del_property.c

@ -34,7 +34,7 @@ int main(int argc, char *argv[]) @@ -34,7 +34,7 @@ int main(int argc, char *argv[])
void *fdt;
uint32_t *intp;
char *strp;
int err;
int err, lenerr;
int oldsize, delsize, newsize;

test_init(argc, argv);
@ -49,11 +49,10 @@ int main(int argc, char *argv[]) @@ -49,11 +49,10 @@ int main(int argc, char *argv[])
if (err)
FAIL("Failed to delete \"prop-int\": %s", fdt_strerror(err));

intp = fdt_getprop(fdt, 0, "prop-int", NULL);
err = fdt_ptr_error(intp);
if (! err)
intp = fdt_getprop(fdt, 0, "prop-int", &lenerr);
if (intp)
FAIL("prop-int still present after deletion");
if (err != FDT_ERR_NOTFOUND)
if ((err = fdt_ptrlen_error(intp, lenerr)) != FDT_ERR_NOTFOUND)
FAIL("Unexpected error on second getprop: %s", fdt_strerror(err));

strp = check_getprop(fdt, 0, "prop-str", strlen(TEST_STRING_1)+1,
@ -63,11 +62,10 @@ int main(int argc, char *argv[]) @@ -63,11 +62,10 @@ int main(int argc, char *argv[])
if (err)
FAIL("Failed to delete \"prop-str\": %s", fdt_strerror(err));

strp = fdt_getprop(fdt, 0, "prop-str", NULL);
err = fdt_ptr_error(strp);
if (! err)
strp = fdt_getprop(fdt, 0, "prop-str", &lenerr);
if (strp)
FAIL("prop-str still present after deletion");
if (err != FDT_ERR_NOTFOUND)
if ((err = fdt_ptrlen_error(strp, lenerr)) != FDT_ERR_NOTFOUND)
FAIL("Unexpected error on second getprop: %s", fdt_strerror(err));

delsize = fdt_totalsize(fdt);

15
tests/nop_property.c

@ -35,6 +35,7 @@ int main(int argc, char *argv[]) @@ -35,6 +35,7 @@ int main(int argc, char *argv[])
uint32_t *intp;
char *strp;
int err;
int lenerr;

test_init(argc, argv);
fdt = load_blob_arg(argc, argv);
@ -46,11 +47,10 @@ int main(int argc, char *argv[]) @@ -46,11 +47,10 @@ int main(int argc, char *argv[])
if (err)
FAIL("Failed to nop \"prop-int\": %s", fdt_strerror(err));

intp = fdt_getprop(fdt, 0, "prop-int", NULL);
err = fdt_ptr_error(intp);
if (! err)
intp = fdt_getprop(fdt, 0, "prop-int", &lenerr);
if (intp)
FAIL("prop-int still present after nopping");
if (err != FDT_ERR_NOTFOUND)
if ((err = fdt_ptrlen_error(intp, lenerr)) != FDT_ERR_NOTFOUND)
FAIL("Unexpected error on second getprop: %s", fdt_strerror(err));
strp = check_getprop(fdt, 0, "prop-str", strlen(TEST_STRING_1)+1,
@ -60,11 +60,10 @@ int main(int argc, char *argv[]) @@ -60,11 +60,10 @@ int main(int argc, char *argv[])
if (err)
FAIL("Failed to nop \"prop-str\": %s", fdt_strerror(err));

strp = fdt_getprop(fdt, 0, "prop-str", NULL);
err = fdt_ptr_error(strp);
if (! err)
strp = fdt_getprop(fdt, 0, "prop-str", &lenerr);
if (strp)
FAIL("prop-str still present after nopping");
if (err != FDT_ERR_NOTFOUND)
if ((err = fdt_ptrlen_error(strp, lenerr)) != FDT_ERR_NOTFOUND)
FAIL("Unexpected error on second getprop: %s", fdt_strerror(err));

PASS();

13
tests/notfound.c

@ -42,24 +42,25 @@ int main(int argc, char *argv[]) @@ -42,24 +42,25 @@ int main(int argc, char *argv[])
int subnode1_offset;
void *val;
int err;
int lenerr;

test_init(argc, argv);
fdt = load_blob_arg(argc, argv);

prop = fdt_get_property(fdt, 0, "nonexistant-property", NULL);
prop = fdt_get_property(fdt, 0, "nonexistant-property", &lenerr);
check_error("fdt_get_property(\"nonexistant-property\")",
fdt_ptr_error(prop));
fdt_ptrlen_error(prop, lenerr));

val = fdt_getprop(fdt, 0, "nonexistant-property", NULL);
val = fdt_getprop(fdt, 0, "nonexistant-property", &lenerr);
check_error("fdt_getprop(\"nonexistant-property\"",
fdt_ptr_error(val));
fdt_ptrlen_error(val, lenerr));

subnode1_offset = fdt_subnode_offset(fdt, 0, "subnode1");
if ((err = fdt_offset_error(subnode1_offset)))
FAIL("Couldn't find subnode1: %s", fdt_strerror(err));

val = fdt_getprop(fdt, subnode1_offset, "prop-str", NULL);
check_error("fdt_getprop(\"prop-str\")", fdt_ptr_error(val));
val = fdt_getprop(fdt, subnode1_offset, "prop-str", &lenerr);
check_error("fdt_getprop(\"prop-str\")", fdt_ptrlen_error(val, lenerr));

offset = fdt_subnode_offset(fdt, 0, "nonexistant-subnode");
check_error("fdt_subnode_offset(\"nonexistant-subnode\")",

5
tests/testutils.c

@ -140,11 +140,10 @@ void *check_getprop(void *fdt, int nodeoffset, const char *name, @@ -140,11 +140,10 @@ void *check_getprop(void *fdt, int nodeoffset, const char *name,
{
void *propval;
int proplen;
int err;

propval = fdt_getprop(fdt, nodeoffset, name, &proplen);
if ((err = fdt_ptr_error(propval)))
FAIL("fdt_getprop(\"%s\"): %s", name, fdt_strerror(err));
if (! propval)
FAIL("fdt_getprop(\"%s\"): %s", name, fdt_strerror(-proplen));

if (proplen != len)
FAIL("Size mismatch on property \"%s\": %d insead of %d",

4
tests/truncated_property.c

@ -38,9 +38,9 @@ int main(int argc, char *argv[]) @@ -38,9 +38,9 @@ int main(int argc, char *argv[])
test_init(argc, argv);

prop = fdt_getprop(fdt, 0, "truncated", &len);
err = fdt_ptr_error(prop);
if (! err)
if (prop)
FAIL("fdt_getprop() succeeded on truncated property");
err = fdt_ptrlen_error(prop, len);
if (err != FDT_ERR_BADSTRUCTURE)
FAIL("fdt_getprop() failed with \"%s\" instead of \"%s\"",
fdt_strerror(err), fdt_strerror(FDT_ERR_BADSTRUCTURE));

Loading…
Cancel
Save