master - coverity: fix possible resource leak in _print_historical_lv function
Gitweb: http://git.fedorahosted.org/git/?p=lvm2.git;a=commitdiff;h=d03b1779b4e2edaec221e2c29d3384d4c86052cd Commit: d03b1779b4e2edaec221e2c29d3384d4c86052cd Parent: 7b15ca5c9ac3645b8d714014f89dbcd6731b2b04 Author: Peter Rajnoha <[email protected]> AuthorDate: Mon Mar 7 10:32:41 2016 +0100 Committer: Peter Rajnoha <[email protected]> CommitterDate: Mon Mar 7 10:43:50 2016 +0100
coverity: fix possible resource leak in _print_historical_lv function
The code in _print_historical_lv function works with temporary "descendants_buffer" that is allocated and freed within this function.
When printing text out, we used "outf" macro which called "out_text" fn and it checked return value and if failed, the macro called "return_0" automatically. But since we use the temporary buffer, if any of the out_text calls fails, we need to deallocate this buffer properly - that's the "goto_out", otherwise we'll be leaking memory.
So add new "outfgo" helper macro which does the same as "outf", but it calls "goto_out" instead of "return_0" so we can jump to a cleanup hook at the end.
--- lib/format_text/export.c | 12 ++++++------ lib/format_text/text_export.h | 1 + 2 files changed, 7 insertions(+), 6 deletions(-)
lib/format_text/export.c +6 -6
@@ -854,10 +854,10 @@ static int _print_historical_lv(struct formatter *f, struct historical_logical_v goto_out;outnl (f); -outf (f,"%s {" , hlv->name); +outfgo (f,"%s {" , hlv->name);_inc_indent (f); -outf (f,"id = \"%s\"" , buffer); +outfgo (f,"id = \"%s\"" , buffer);if (!_print_timestamp (f,"creation_time" , hlv->timestamp, buffer,sizeof (buffer))) goto_out;
@@ -867,16 +867,16 @@ static int _print_historical_lv(struct formatter *f, struct historical_logical_vif (hlv->indirect_origin) {if (hlv->indirect_origin->is_historical) -outf (f,"origin = \"%s%s\"" ,HISTORICAL_LV_PREFIX , hlv->indirect_origin->historical->name); +outfgo (f,"origin = \"%s%s\"" ,HISTORICAL_LV_PREFIX , hlv->indirect_origin->historical->name);else -outf (f,"origin = \"%s\"" , hlv->indirect_origin->live->name); +outfgo (f,"origin = \"%s\"" , hlv->indirect_origin->live->name); }if (descendants_buffer) -outf (f,"descendants = %s" , descendants_buffer); +outfgo (f,"descendants = %s" , descendants_buffer);_dec_indent (f); -outf (f,"}" ); +outfgo (f,"}" ); r =1 ; out:
lib/format_text/text_export.h +1 -0
@@ -20,6 +20,7 @@ #define outhint(args...) do {if (!out_hint(args)) return_0;} while (0) #define outfc(args...) do {if (!out_text_with_comment(args)) return_0;} while (0) #define outf(args...) do {if (!out_text(args)) return_0;} while (0) +#define outfgo(args...) do {if (!out_text(args)) goto_out;} while (0)#define outnl (f) do {if (!out_newline(f)) return_0;} while (0)struct formatter ;