GPLv2 release
[centaur.git] / src / libelfu / model / elf.c
index 46dc120951c3dfe99f11de9736480693506cc40b..41169dd3910e681b17c2b034e4fc64272b5f240a 100644 (file)
@@ -1,3 +1,18 @@
+/* This file is part of centaur.
+ *
+ * centaur is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License 2 as
+ * published by the Free Software Foundation.
+
+ * centaur is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+
+ * You should have received a copy of the GNU General Public License
+ * along with centaur.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
 #include <assert.h>
 #include <stdlib.h>
 #include <string.h>
@@ -29,22 +44,30 @@ ElfuElf* elfu_mElfAlloc()
 
 void elfu_mElfDestroy(ElfuElf* me)
 {
-  ElfuPhdr *mp;
-  ElfuScn *ms;
-
   assert(me);
 
-  CIRCLEQ_INIT(&me->phdrList);
-  CIRCLEQ_INIT(&me->orphanScnList);
+  if (!CIRCLEQ_EMPTY(&me->phdrList)) {
+    ElfuPhdr *nextmp;
 
-  CIRCLEQ_FOREACH(mp, &me->phdrList, elem) {
-    CIRCLEQ_REMOVE(&me->phdrList, mp, elem);
-    elfu_mPhdrDestroy(mp);
+    nextmp = CIRCLEQ_FIRST(&me->phdrList);
+    while ((void*)nextmp != (void*)&me->phdrList) {
+      ElfuPhdr *curmp = nextmp;
+      nextmp = CIRCLEQ_NEXT(curmp, elem);
+      CIRCLEQ_REMOVE(&me->phdrList, curmp, elem);
+      elfu_mPhdrDestroy(curmp);
+    }
   }
 
-  CIRCLEQ_FOREACH(ms, &me->orphanScnList, elem) {
-    CIRCLEQ_REMOVE(&me->orphanScnList, ms, elem);
-    elfu_mScnDestroy(ms);
+  if (!CIRCLEQ_EMPTY(&me->orphanScnList)) {
+    ElfuScn *nextms;
+
+    nextms = CIRCLEQ_FIRST(&me->orphanScnList);
+    while ((void*)nextms != (void*)&me->orphanScnList) {
+      ElfuScn *curms = nextms;
+      nextms = CIRCLEQ_NEXT(curms, elemChildScn);
+      CIRCLEQ_REMOVE(&me->orphanScnList, curms, elemChildScn);
+      elfu_mScnDestroy(curms);
+    }
   }
 
   free(me);