libfdt: Seval cleanups to parameter checking
This patch makes a couple of small cleanups to parameter checking of libfdt functions.
- In several functions which take a node offset, we use an
idiom involving fdt_next_tag() first to check that we have indeed been given a node offset. This patch adds a helper function _fdt_check_node_offset() to encapsulate this usage of fdt_next_tag().
- In fdt_rw.c in several places we have the expanded version of the RW_CHECK_HEADER() macro for no particular reason. This patch replaces those instances with an invocation of the macro; that's what it's for.
--- libfdt/fdt.c | 17 ++++++++++++----- libfdt/fdt_ro.c | 26 ++++++++------------------ libfdt/fdt_rw.c | 23 ++++++++--------------- libfdt/libfdt_internal.h | 1 + 4 files changed, 29 insertions(+), 38 deletions(-)
libfdt/fdt.c +12 -5
@@ -129,16 +129,23 @@ uint32_t fdt_next_tag(const void *fdt, ireturn tag; } +int _fdt_check_node_offset (const void *fdt,int offset) +{ +if ((offset <0 ) || (offset %FDT_TAGSIZE ) + || (fdt_next_tag (fdt, offset, &offset) !=FDT_BEGIN_NODE )) +return -FDT_ERR_BADOFFSET ; + +return offset; +} +int fdt_next_node (const void *fdt,int offset,int *depth) {int nextoffset =0 ;uint32_t tag; -if (offset >=0 ) { - tag =fdt_next_tag (fdt, offset, &nextoffset); -if (tag !=FDT_BEGIN_NODE ) -return -FDT_ERR_BADOFFSET ; - } +if (offset >=0 ) +if ((nextoffset =_fdt_check_node_offset (fdt, offset)) <0 ) +return nextoffset; do { offset = nextoffset;
libfdt/libfdt_internal.h +1 -0
@@ -66,6 +66,7 @@ }uint32_t _fdt_next_tag (const void *fdt,int startoffset,int *nextoffset); +int _fdt_check_node_offset (const void *fdt,int offset);const char *_fdt_find_string (const char *strtab,int tabsize,const char *s);int _fdt_node_end_offset (void *fdt,int nodeoffset);
libfdt/fdt_ro.c +8 -18
@@ -157,16 +157,12 @@ int fdt_path_offset(const void *fdt, conconst char *fdt_get_name (const void *fdt,int nodeoffset,int *len) { -const struct fdt_node_header *nh; +const struct fdt_node_header *nh =_fdt_offset_ptr (fdt, nodeoffset);int err; -if ((err =fdt_check_header (fdt)) !=0 ) - goto fail; - - err = -FDT_ERR_BADOFFSET ; - nh =fdt_offset_ptr (fdt, nodeoffset,sizeof (*nh)); -if (!nh || (fdt32_to_cpu (nh->tag) !=FDT_BEGIN_NODE )) - goto fail; +if (((err =fdt_check_header (fdt)) !=0 ) + || ((err =_fdt_check_node_offset (fdt, nodeoffset)) <0 )) + goto fail;if (len) *len =strlen (nh->name);
@@ -189,17 +185,11 @@ const struct fdt_property *fdt_get_propeint offset, nextoffset;int err; -if ((err =fdt_check_header (fdt)) !=0 ) - goto fail; - - err = -FDT_ERR_BADOFFSET ; -if (nodeoffset %FDT_TAGSIZE ) - goto fail; - - tag =fdt_next_tag (fdt, nodeoffset, &nextoffset); -if (tag !=FDT_BEGIN_NODE ) - goto fail; +if (((err =fdt_check_header (fdt)) !=0 ) + || ((err =_fdt_check_node_offset (fdt, nodeoffset)) <0 )) + goto fail; + nextoffset = err; do { offset = nextoffset;
libfdt/fdt_rw.c +8 -15
@@ -172,8 +172,7 @@ int fdt_add_mem_rsv(void *fdt, uint64_tstruct fdt_reserve_entry *re;int err; -if ((err =rw_check_header (fdt))) -return err; +RW_CHECK_HEADER (fdt); re =_fdt_mem_rsv_w (fdt,fdt_num_mem_rsv (fdt)); err =_blob_splice_mem_rsv (fdt, re,0 ,1 );
@@ -190,8 +189,8 @@ int fdt_del_mem_rsv(void *fdt, int n)struct fdt_reserve_entry *re =_fdt_mem_rsv_w (fdt, n);int err; -if ((err =rw_check_header (fdt))) -return err; +RW_CHECK_HEADER (fdt); +if (n >=fdt_num_mem_rsv (fdt))return -FDT_ERR_NOTFOUND ;
@@ -223,15 +222,13 @@ static int _resize_property(void *fdt, istatic int _add_property (void *fdt,int nodeoffset,const char *name,int len,struct fdt_property **prop) { -uint32_t tag;int proplen;int nextoffset;int namestroff;int err; - tag =fdt_next_tag (fdt, nodeoffset, &nextoffset); -if (tag !=FDT_BEGIN_NODE ) -return -FDT_ERR_BADOFFSET ; +if ((nextoffset =_fdt_check_node_offset (fdt, nodeoffset)) <0 ) +return nextoffset; namestroff =_find_add_string (fdt, name);if (namestroff <0 )
@@ -256,8 +253,7 @@ int fdt_set_name(void *fdt, int nodeoffsint oldlen, newlen;int err; -if ((err =rw_check_header (fdt))) -return err; +RW_CHECK_HEADER (fdt); namep = (char *)fdt_get_name (fdt, nodeoffset, &oldlen);if (!namep)
@@ -280,8 +276,7 @@ int fdt_setprop(void *fdt, int nodeoffsestruct fdt_property *prop;int err; -if ((err =rw_check_header (fdt))) -return err; +RW_CHECK_HEADER (fdt); err =_resize_property (fdt, nodeoffset, name, len, &prop);if (err == -FDT_ERR_NOTFOUND )
@@ -454,9 +449,7 @@ int fdt_pack(void *fdt)int mem_rsv_size;int err; - err =rw_check_header (fdt); -if (err) -return err; +RW_CHECK_HEADER (fdt); mem_rsv_size = (fdt_num_mem_rsv (fdt)+1 ) *sizeof (struct fdt_reserve_entry );
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
Quoted 11 lines · David Gibson
This patch makes a couple of small cleanups to parameter checking of libfdt functions.
- In several functions which take a node offset, we use an
idiom involvingfdt_next_tag()first to check that we have indeed been given a node offset. This patch adds a helper function_fdt_check_node_offset()to encapsulate this usage offdt_next_tag().
- Infdt_rw.cin several places we have the expanded version of theRW_CHECK_HEADER()macro for no particular reason. This patch replaces those instances with an invocation of the macro; that's what it's for.
Quoted 1 line
Signed-off-by David Gibson <[email protected]>
Blah, sorry, ignore. Improved version coming.
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