]> git.ipfire.org Git - thirdparty/lldpd.git/commitdiff
lib/cpp: Fix memory leaks in LldpCtl
authorPM Holtmo <pm.holtmo@gmail.com>
Fri, 23 May 2025 14:10:25 +0000 (16:10 +0200)
committerVincent Bernat <vincent@bernat.ch>
Tue, 27 May 2025 21:33:11 +0000 (23:33 +0200)
LldpCtl::GetInterfaces() and LldpAtom::GetAtomList() did not decrement ref for retrieved atoms
Ensure parent atom outlives its children atoms

src/lib/lldpctl.hpp

index 7f7eabd6ca001cd2e728d950751cc0896021b736..e2e301a7ea203d3ba7dba03a8ef4d01b056a7a3d 100644 (file)
@@ -233,8 +233,10 @@ class LldpAtom {
                lldpctl_atom_t *atom;
                lldpctl_atom_foreach(it, atom)
                {
-                       list.emplace_back(atom, true, conn_);
+                       list.emplace_back(atom, true, conn_,
+                               std::make_unique<LldpAtom>(*this));
                }
+               ::lldpctl_atom_dec_ref(it);
 
                return list;
        }
@@ -364,7 +366,7 @@ class LldpCtl {
 
        std::list<LldpAtom> GetInterfaces() const
        {
-               const auto &it { ::lldpctl_get_interfaces(conn_.get()) };
+               auto *it { ::lldpctl_get_interfaces(conn_.get()) };
 
                std::list<LldpAtom> list;
                lldpctl_atom_t *atom;
@@ -372,6 +374,7 @@ class LldpCtl {
                {
                        list.emplace_back(atom, true, conn_);
                }
+               ::lldpctl_atom_dec_ref(it);
 
                return list;
        }