qemu-config: never call the callback after an error, fix leak
PATCH 1/1 qemu-config: never call the callback after an error, fix leak
Ensure that the callback to qemu_config_foreach is never called upon an error, by moving the invocation before the "out" label and ensuring all error cases jump to the label. The qobject_unref however needs to be done in all cases (which Coverity is already complaining about).
The leak is basically impossible to reach, since the only common way to get ferror(fp) is by passing a directory to -readconfig. In that case, the error occurs before qdict is set to anything non-NULL. However, it's theoretically possible to get there after an EIO.
--- util/qemu-config.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-)
util/qemu-config.c +3 -4
@@ -412,16 +412,15 @@ static int qemu_config_foreach(FILE *fp, QEMUConfigCB *cb, void *opaque, goto out; }if (ferror (fp)) { -loc_pop (&loc);error_setg_errno (errp, errno,"Cannot read config file" ); -return res; + goto out; } res = count; -out:if (qdict) {cb (group, qdict, opaque, errp); -qobject_unref (qdict); } +out: +qobject_unref (qdict);loc_pop (&loc);return res; }
Signature · 1 line
2.31.1
Quoted 22 lines · Paolo Bonzini
Ensure that the callback to
qemu_config_foreachis never called upon an error, by moving the invocation before the "out" label and ensuring all error cases jump to the label. Theqobject_unrefhowever needs to be done in all cases (which Coverity is already complaining about).The leak is basically impossible to reach, since the only common way to get ferror(fp) is by passing a directory to -readconfig. In that case, the error occurs before qdict is set to anything non-NULL. However, it's theoretically possible to get there after an EIO.
Reported-by Peter Maydell <[email protected]>Signed-off-by Paolo Bonzini <[email protected]>--- util/qemu-config.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-)util/qemu-config.c +0
-0
util/qemu-config.c static int qemu_config_foreach(FILE *fp, QEMUConfigCB *cb, void *opaque, · line 415
Quoted 5 lines
util/qemu-config.c +0
-1@@ -412,16 +412,15 @@ static int qemu_config_foreach(FILE *fp, QEMUConfigCB *cb, void *opaque, goto out; }if (ferror (fp)) { -loc_pop (&loc);error_setg_errno (errp, errno,"Cannot read config file" );
I'm afraid we now report the error with the wrong location when @errp is &error-fatal.
Quoted 4 lines
util/qemu-config.c +1
-1-return res; + goto out; } res = count;
util/qemu-config.c static int qemu_config_foreach(FILE *fp, QEMUConfigCB *cb, void *opaque, · line 426
Quoted 10 lines
util/qemu-config.c +2
-2-out:if (qdict) {cb (group, qdict, opaque, errp); -qobject_unref (qdict); } +out: +qobject_unref (qdict);loc_pop (&loc);return res; }
Looks like the patch fixes two separate issues:
1. Memory leak on ferror()
2. Callback can run on error.
I *think* this happens when the cb() further up fails, and when a line following the [...] section header cannot be parsed.
Worth fixing the separate bugs in separate patches?
Quoted 5 lines · Markus Armbruster
Looks like the patch fixes two separate issues:
1. Memory leak on
ferror()Fixes: f7544edcd32e602af1aae86714dc7c32350d5d7c
Quoted 7 lines
2. Callback can run on error.
Fixes 37701411397c7b7d709ae92abd347cc593940ee5I*think* this happens when the
cb()further up fails, and when a line following the [...] section header cannot be parsed.Worth fixing the separate bugs in separate patches?
Yes, good idea.
Paolo