public inbox for lvm2-cvs@sourceware.org help / color / mirror / Atom feed
From: prajnoha@sourceware.org To: lvm-devel@redhat.com, lvm2-cvs@sourceware.org Subject: LVM2 ./WHATS_NEW_DM libdm/libdm-report.c Date: Mon, 01 Nov 2010 13:31:00 -0000 [thread overview] Message-ID: <20101101133156.18419.qmail@sourceware.org> (raw) CVSROOT: /cvs/lvm2 Module name: LVM2 Changes by: prajnoha@sourceware.org 2010-11-01 13:31:55 Modified files: . : WHATS_NEW_DM libdm : libdm-report.c Log message: Allocate buffer for reporting functions dynamically to support long outputs. Fix memory leak of field_id in _output_field function. There's been a patch added recently to use dynamic allocation for metadata tags buffer to remove the 4k limit (for writing metadata out). However, when using reporting commands like vgs and lvs, we still need to fix libdm reporting functions themselves to support such long outputs. So the buffer used in those reporting functions is dynamic now. The patch also includes a fix for field_id memory leak which was found in the _output_field function. Patches: http://sourceware.org/cgi-bin/cvsweb.cgi/LVM2/WHATS_NEW_DM.diff?cvsroot=lvm2&r1=1.420&r2=1.421 http://sourceware.org/cgi-bin/cvsweb.cgi/LVM2/libdm/libdm-report.c.diff?cvsroot=lvm2&r1=1.37&r2=1.38 --- LVM2/WHATS_NEW_DM 2010/10/25 16:38:20 1.420 +++ LVM2/WHATS_NEW_DM 2010/11/01 13:31:55 1.421 @@ -1,5 +1,7 @@ Version 1.02.57 =================================== + Fix memory leak of field_id in _output_field function. + Allocate buffer for reporting functions dynamically to support long outputs. Version 1.02.56 - 25th October 2010 =================================== --- LVM2/libdm/libdm-report.c 2010/09/30 21:06:52 1.37 +++ LVM2/libdm/libdm-report.c 2010/11/01 13:31:55 1.38 @@ -757,7 +757,8 @@ { struct field_properties *fp; const char *heading; - char buf[1024]; + char *buf = NULL; + size_t buf_size = 0; if (rh->flags & RH_HEADINGS_PRINTED) return 1; @@ -773,6 +774,18 @@ return 0; } + dm_list_iterate_items(fp, &rh->field_props) { + if (buf_size < fp->width) + buf_size = fp->width; + } + /* Including trailing '\0'! */ + buf_size++; + + if (!(buf = dm_malloc(buf_size))) { + log_error("dm_report: Could not allocate memory for heading buffer."); + goto bad; + } + /* First heading line */ dm_list_iterate_items(fp, &rh->field_props) { if (fp->flags & FLD_HIDDEN) @@ -780,7 +793,7 @@ heading = rh->fields[fp->field_num].heading; if (rh->flags & DM_REPORT_OUTPUT_ALIGNED) { - if (dm_snprintf(buf, sizeof(buf), "%-*.*s", + if (dm_snprintf(buf, buf_size, "%-*.*s", fp->width, fp->width, heading) < 0) { log_error("dm_report: snprintf heading failed"); goto bad; @@ -806,9 +819,12 @@ } log_print("%s", (char *) dm_pool_end_object(rh->mem)); + dm_free(buf); + return 1; bad: + dm_free(buf); dm_pool_abandon_object(rh->mem); return 0; } @@ -892,7 +908,8 @@ int32_t width; uint32_t align; const char *repstr; - char buf[4096]; + char *buf = NULL; + size_t buf_size = 0; if (rh->flags & DM_REPORT_OUTPUT_FIELD_NAME_PREFIX) { if (!(field_id = strdup(rh->fields[field->props->field_num].id))) { @@ -902,11 +919,13 @@ if (!dm_pool_grow_object(rh->mem, rh->output_field_name_prefix, 0)) { log_error("dm_report: Unable to extend output line"); + free(field_id); return 0; } if (!dm_pool_grow_object(rh->mem, _toupperstr(field_id), 0)) { log_error("dm_report: Unable to extend output line"); + free(field_id); return 0; } @@ -935,25 +954,33 @@ if (!(align = field->props->flags & DM_REPORT_FIELD_ALIGN_MASK)) align = (field->props->flags & DM_REPORT_FIELD_TYPE_NUMBER) ? DM_REPORT_FIELD_ALIGN_RIGHT : DM_REPORT_FIELD_ALIGN_LEFT; + + /* Including trailing '\0'! */ + buf_size = width + 1; + if (!(buf = dm_malloc(buf_size))) { + log_error("dm_report: Could not allocate memory for output line buffer."); + return 0; + } + if (align & DM_REPORT_FIELD_ALIGN_LEFT) { - if (dm_snprintf(buf, sizeof(buf), "%-*.*s", + if (dm_snprintf(buf, buf_size, "%-*.*s", width, width, repstr) < 0) { log_error("dm_report: left-aligned snprintf() failed"); - return 0; + goto bad; } if (!dm_pool_grow_object(rh->mem, buf, width)) { log_error("dm_report: Unable to extend output line"); - return 0; + goto bad; } } else if (align & DM_REPORT_FIELD_ALIGN_RIGHT) { - if (dm_snprintf(buf, sizeof(buf), "%*.*s", + if (dm_snprintf(buf, buf_size, "%*.*s", width, width, repstr) < 0) { log_error("dm_report: right-aligned snprintf() failed"); - return 0; + goto bad; } if (!dm_pool_grow_object(rh->mem, buf, width)) { log_error("dm_report: Unable to extend output line"); - return 0; + goto bad; } } } @@ -962,10 +989,15 @@ !(rh->flags & DM_REPORT_OUTPUT_FIELD_UNQUOTED)) if (!dm_pool_grow_object(rh->mem, "\'", 1)) { log_error("dm_report: Unable to extend output line"); - return 0; + goto bad; } + dm_free(buf); return 1; + +bad: + dm_free(buf); + return 0; } static int _output_as_rows(struct dm_report *rh)
next reply other threads:[~2010-11-01 13:31 UTC|newest] Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top 2010-11-01 13:31 prajnoha [this message] -- strict thread matches above, loose matches on Subject: below -- 2012-02-10 14:00 zkabelac 2011-06-12 19:49 agk 2011-01-25 21:51 zkabelac 2010-11-19 13:17 agk 2010-01-07 14:30 zkabelac 2009-01-10 3:01 agk
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20101101133156.18419.qmail@sourceware.org \ --to=prajnoha@sourceware.org \ --cc=lvm-devel@redhat.com \ --cc=lvm2-cvs@sourceware.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).