* [PATCH] Use dynamic allocation for buffer when reporting tags
@ 2010-11-01 12:04 Peter Rajnoha
2010-11-01 13:08 ` Peter Rajnoha
0 siblings, 1 reply; 4+ messages in thread
From: Peter Rajnoha @ 2010-11-01 12:04 UTC (permalink / raw)
To: lvm-devel
There's been a patch done recently to use dynamic allocation
for metadata tags buffer which works fine now even with large
set of tags/long tags (rhbz #633033).
But we need the same dynamic allocation when trying to output
the list of tags in reports which still use constant size
buffer and hence makes problems (rhbz #648219).
Peter
---
libdm/libdm-report.c | 48 +++++++++++++++++++++++++++++++++++++++---------
1 files changed, 39 insertions(+), 9 deletions(-)
diff --git a/libdm/libdm-report.c b/libdm/libdm-report.c
index 7631e21..55b8b80 100644
--- a/libdm/libdm-report.c
+++ b/libdm/libdm-report.c
@@ -757,7 +757,8 @@ static int _report_headings(struct dm_report *rh)
{
struct field_properties *fp;
const char *heading;
- char buf[1024];
+ char *buf;
+ size_t buf_size = 0;
if (rh->flags & RH_HEADINGS_PRINTED)
return 1;
@@ -773,6 +774,18 @@ static int _report_headings(struct dm_report *rh)
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 @@ static int _report_headings(struct dm_report *rh)
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,13 @@ static int _report_headings(struct dm_report *rh)
}
log_print("%s", (char *) dm_pool_end_object(rh->mem));
+ dm_free(buf);
+
return 1;
bad:
+ if (buf)
+ dm_free(buf);
dm_pool_abandon_object(rh->mem);
return 0;
}
@@ -892,7 +909,8 @@ static int _output_field(struct dm_report *rh, struct dm_report_field *field)
int32_t width;
uint32_t align;
const char *repstr;
- char buf[4096];
+ char *buf;
+ 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))) {
@@ -935,25 +953,33 @@ static int _output_field(struct dm_report *rh, struct dm_report_field *field)
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;
}
}
}
@@ -966,6 +992,10 @@ static int _output_field(struct dm_report *rh, struct dm_report_field *field)
}
return 1;
+
+bad:
+ dm_free(buf);
+ return 0;
}
static int _output_as_rows(struct dm_report *rh)
^ permalink raw reply related [flat|nested] 4+ messages in thread
* [PATCH] Use dynamic allocation for buffer when reporting tags
2010-11-01 12:04 [PATCH] Use dynamic allocation for buffer when reporting tags Peter Rajnoha
@ 2010-11-01 13:08 ` Peter Rajnoha
2010-11-01 13:16 ` Milan Broz
2010-11-01 13:21 ` Zdenek Kabelac
0 siblings, 2 replies; 4+ messages in thread
From: Peter Rajnoha @ 2010-11-01 13:08 UTC (permalink / raw)
To: lvm-devel
OK, here's updated version (based on quick hints from Milan).
Peter
---
libdm/libdm-report.c | 52 ++++++++++++++++++++++++++++++++++++++++---------
1 files changed, 42 insertions(+), 10 deletions(-)
diff --git a/libdm/libdm-report.c b/libdm/libdm-report.c
index 7631e21..951ecb3 100644
--- a/libdm/libdm-report.c
+++ b/libdm/libdm-report.c
@@ -757,7 +757,8 @@ static int _report_headings(struct dm_report *rh)
{
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 @@ static int _report_headings(struct dm_report *rh)
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 @@ static int _report_headings(struct dm_report *rh)
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 @@ static int _report_headings(struct dm_report *rh)
}
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 @@ static int _output_field(struct dm_report *rh, struct dm_report_field *field)
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 @@ static int _output_field(struct dm_report *rh, struct dm_report_field *field)
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 @@ static int _output_field(struct dm_report *rh, struct dm_report_field *field)
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 @@ static int _output_field(struct dm_report *rh, struct dm_report_field *field)
!(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)
^ permalink raw reply related [flat|nested] 4+ messages in thread
* [PATCH] Use dynamic allocation for buffer when reporting tags
2010-11-01 13:08 ` Peter Rajnoha
@ 2010-11-01 13:16 ` Milan Broz
2010-11-01 13:21 ` Zdenek Kabelac
1 sibling, 0 replies; 4+ messages in thread
From: Milan Broz @ 2010-11-01 13:16 UTC (permalink / raw)
To: lvm-devel
On 11/01/2010 02:08 PM, Peter Rajnoha wrote:
> OK, here's updated version (based on quick hints from Milan).
ack + tested, works for my test script
> @@ -902,11 +919,13 @@ static int _output_field(struct dm_report *rh, struct dm_report_field *field)
>
> 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;
just mention in changelog that this is additional memleak fix.
Milan
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH] Use dynamic allocation for buffer when reporting tags
2010-11-01 13:08 ` Peter Rajnoha
2010-11-01 13:16 ` Milan Broz
@ 2010-11-01 13:21 ` Zdenek Kabelac
1 sibling, 0 replies; 4+ messages in thread
From: Zdenek Kabelac @ 2010-11-01 13:21 UTC (permalink / raw)
To: lvm-devel
Dne 1.11.2010 14:08, Peter Rajnoha napsal(a):
> OK, here's updated version (based on quick hints from Milan).
>
> Peter
> ---
> libdm/libdm-report.c | 52 ++++++++++++++++++++++++++++++++++++++++---------
> 1 files changed, 42 insertions(+), 10 deletions(-)
>
> diff --git a/libdm/libdm-report.c b/libdm/libdm-report.c
> index 7631e21..951ecb3 100644
> --- a/libdm/libdm-report.c
> +++ b/libdm/libdm-report.c
> @@ -902,11 +919,13 @@ static int _output_field(struct dm_report *rh, struct dm_report_field *field)
>
> 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;
> }
Maybe swith to use dm_strdup() and dm_free()
Zdenek
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2010-11-01 13:21 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-11-01 12:04 [PATCH] Use dynamic allocation for buffer when reporting tags Peter Rajnoha
2010-11-01 13:08 ` Peter Rajnoha
2010-11-01 13:16 ` Milan Broz
2010-11-01 13:21 ` Zdenek Kabelac
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.