[PATCH] libdw: Prevent infinite recursion when processing DW_TAG_imported_unit.

Mark Wielaard mjw at redhat.com
Mon Jan 12 08:42:51 UTC 2015


On Mon, 2015-01-12 at 03:15 +0100, Petr Machata wrote:
> Mark Wielaard <mjw at redhat.com> writes:
> 
> > +  /* Checks the given DIE hasn't been imported yet to prevent cycles.  */
> > +  inline bool imports_contains (Dwarf_Die *die)
> > +  {
> > +    struct Dwarf_Die_Chain *import = imports;
> > +    while (import != NULL)
> > +      {
> > +	if (import->die.addr == die->addr)
> > +	  return true;
> > +	import = import->parent;
> > +      }
> 
> This is equivalent but is IMHO canonical:
> 
>     for (Dwarf_Die_Chain *import = imports; import != NULL;
>          import = import->parent)
>       if (import->die.addr == die->addr)
>         return true;

Yes, nicer, less braces.

Also "fixed up" this:

diff --git a/libdw/libdw_visit_scopes.c b/libdw/libdw_visit_scopes.c
index 8b4ae1f..1b8941f 100644
--- a/libdw/libdw_visit_scopes.c
+++ b/libdw/libdw_visit_scopes.c
@@ -117,13 +117,13 @@ __libdw_visit_scopes (depth, root, imports, previsit, postvisit, arg)
            if (INTUSE(dwarf_formref_die) (attr, &child.die) != NULL
                 && INTUSE(dwarf_child) (&child.die, &child.die) == 0)
              {
-               if (imports_contains (&child.die))
+               if (imports_contains (&orig_child_die))
                  {
                    __libdw_seterrno (DWARF_E_INVALID_DWARF);
                    return -1;
                  }
                struct Dwarf_Die_Chain *orig_imports = imports;
-               struct Dwarf_Die_Chain import = { .die = child.die,
+               struct Dwarf_Die_Chain import = { .die = orig_child_die,
                                                  .parent = orig_imports };
                imports = &import;
                int result = walk_children ();

We were storing and checking against the first child of the imported DIE
instead of the importing DIE. The effect is the same (theoretically
there could be a difference if there were no children, but in that case
we didn't process anything anyway). And I think it is slightly clearer
recording the importing DIEs (and easier when debugging to see how the
import chain looks).

Pushed to master as attached with those two changes.

Thanks,

Mark
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0001-libdw-Prevent-infinite-recursion-when-processing-DW_.patch
Type: text/x-patch
Size: 9003 bytes
Desc: not available
URL: <https://lists.fedorahosted.org/pipermail/elfutils-devel/attachments/20150112/15d361be/attachment.bin>


More information about the elfutils-devel mailing list