From: glenlow Date: Sat, 2 Feb 2008 06:42:23 +0000 (+0000) Subject: gvRenderData outputs non-null terminated string and string length, reduced reallocations X-Git-Tag: LAST_LIBGRAPH~32^2~4784 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=eddb205ce1c69094ee444006782f1aad758a438b;p=graphviz gvRenderData outputs non-null terminated string and string length, reduced reallocations --- diff --git a/lib/gvc/gvc.h b/lib/gvc/gvc.h index d8103e5fe..cd417ec21 100644 --- a/lib/gvc/gvc.h +++ b/lib/gvc/gvc.h @@ -91,7 +91,7 @@ extern int gvRender(GVC_t *gvc, graph_t *g, char *format, FILE *out); extern int gvRenderFilename(GVC_t *gvc, graph_t *g, char *format, char *filename); /* Render layout in a specified format to a malloc'ed string */ -extern int gvRenderData(GVC_t *gvc, graph_t *g, char *format, char **result); +extern int gvRenderData(GVC_t *gvc, graph_t *g, char *format, char **result, unsigned int *length); /* Render layout according to -T and -o options found by gvParseArgs */ extern int gvRenderJobs(GVC_t *gvc, graph_t *g); diff --git a/lib/gvc/gvdevice.c b/lib/gvc/gvdevice.c index fd44b2b34..0c4058afe 100644 --- a/lib/gvc/gvdevice.c +++ b/lib/gvc/gvdevice.c @@ -49,6 +49,8 @@ #include "gvcint.h" #include "gvcproc.h" +static const int PAGE_ALIGN = 4095; /* align to a 4K boundary (less one), typical for Linux, Mac OS X and Windows memory allocation */ + size_t gvdevice_write (GVJ_t * job, const unsigned char *s, unsigned int len) { if (job->gvc->write_fn && job->output_file == stdout) /* externally provided write dicipline */ @@ -59,15 +61,15 @@ size_t gvdevice_write (GVJ_t * job, const unsigned char *s, unsigned int len) #endif } else if (job->output_data) { - if (len > (job->output_data_allocated - (job->output_data_position + 1))) { - job->output_data_allocated = job->output_data_position + len + 1000; + if (len > job->output_data_allocated - job->output_data_position) { + job->output_data_allocated = (job->output_data_position + len + PAGE_ALIGN) & ~PAGE_ALIGN; job->output_data = realloc(job->output_data, job->output_data_allocated); if (!job->output_data) { fprintf(stderr, "failure realloc'ing for result string\n"); return 0; } } - strcpy(job->output_data + job->output_data_position, (char*)s); + memcpy(job->output_data + job->output_data_position, s, len); job->output_data_position += len; return len; } diff --git a/tclpkg/gv/gv.cpp b/tclpkg/gv/gv.cpp index 38614b618..e43d31f8f 100644 --- a/tclpkg/gv/gv.cpp +++ b/tclpkg/gv/gv.cpp @@ -873,9 +873,12 @@ char* renderdata(Agraph_t *g, char *format) { int err; char *data; + unsigned int length; - err = gvRenderData(gvc, g, format, &data); + err = gvRenderData(gvc, g, format, &data, &length); if (err) return NULL; + data = (char*)realloc(data, length + 1); + if (data) data[length] = '\0'; return data; }