secret: Fix memory leak in virSecretLoad

If the virSecretLoadValue fails, the code jumped to cleanup without
setting @ret = obj, thus calling virSecretObjListRemove which only
accounts for the object reference related to adding the object to
the list during virSecretObjListAdd, but does not account for the
reference to the object itself as the return of @ret would be NULL
so the caller wouldn't call virSecretObjEndAPI on the object recently
added thus reducing the refcnt to zero.

This patch will perform the ObjListRemove in the failure path of
virSecretLoadValue and Unref @obj in order to perform clean up
and return @obj as NULL. The @def will be freed as part of the
virObjectUnref.
This commit is contained in:
John Ferlan 2017-07-25 09:02:09 -04:00
parent e4c0aff215
commit d04bc0278d

View File

@ -914,7 +914,6 @@ virSecretLoad(virSecretObjListPtr secrets,
{
virSecretDefPtr def = NULL;
virSecretObjPtr obj = NULL;
virSecretObjPtr ret = NULL;
if (!(def = virSecretDefParseFile(path)))
goto cleanup;
@ -926,16 +925,15 @@ virSecretLoad(virSecretObjListPtr secrets,
goto cleanup;
def = NULL;
if (virSecretLoadValue(obj) < 0)
goto cleanup;
ret = obj;
obj = NULL;
if (virSecretLoadValue(obj) < 0) {
virSecretObjListRemove(secrets, obj);
virObjectUnref(obj);
obj = NULL;
}
cleanup:
virSecretObjListRemove(secrets, obj);
virSecretDefFree(def);
return ret;
return obj;
}