From: Martin Ling Date: Fri, 26 Apr 2013 21:50:09 +0000 (+0100) Subject: gnuplot: Eliminate fixed-size header buffer based on max probes. X-Git-Tag: dsupstream~41 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=90f680ff006e5ae2fd4935bd885a14bd9927fd99;p=libsigrok.git gnuplot: Eliminate fixed-size header buffer based on max probes. --- diff --git a/output/gnuplot.c b/output/gnuplot.c index 4157323a..7c033a91 100644 --- a/output/gnuplot.c +++ b/output/gnuplot.c @@ -41,9 +41,6 @@ struct context { uint8_t *old_sample; }; -#define MAX_HEADER_LEN \ - (1024 + (SR_MAX_NUM_PROBES * (SR_MAX_PROBENAME_LEN + 10))) - static const char *gnuplot_header = "\ # Sample data in space-separated columns format usable by gnuplot\n\ #\n\ @@ -66,7 +63,7 @@ static int init(struct sr_output *o) GVariant *gvar; uint64_t samplerate; unsigned int i; - int b, num_probes; + int num_probes; char *c, *frequency_s; char wbuf[1000], comment[128]; time_t t; @@ -91,12 +88,6 @@ static int init(struct sr_output *o) return SR_ERR_MALLOC; } - if (!(ctx->header = g_try_malloc0(MAX_HEADER_LEN + 1))) { - sr_err("%s: ctx->header malloc failed", __func__); - g_free(ctx); - return SR_ERR_MALLOC; - } - o->internal = ctx; ctx->num_enabled_probes = 0; for (l = o->sdi->probes; l; l = l->next) { @@ -114,7 +105,6 @@ static int init(struct sr_output *o) samplerate = g_variant_get_uint64(gvar); if (!(frequency_s = sr_samplerate_string(samplerate))) { sr_err("%s: sr_samplerate_string failed", __func__); - g_free(ctx->header); g_free(ctx); g_variant_unref(gvar); return SR_ERR; @@ -137,24 +127,15 @@ static int init(struct sr_output *o) if (!(frequency_s = sr_period_string(samplerate))) { sr_err("%s: sr_period_string failed", __func__); - g_free(ctx->header); g_free(ctx); return SR_ERR; } t = time(NULL); - b = snprintf(ctx->header, MAX_HEADER_LEN, gnuplot_header, - PACKAGE_STRING, ctime(&t), comment, frequency_s, - (char *)&wbuf); + ctx->header = g_strdup_printf(gnuplot_header, PACKAGE_STRING, + ctime(&t), comment, frequency_s, (char *)&wbuf); g_free(frequency_s); - if (b < 0) { - sr_err("%s: sprintf failed", __func__); - g_free(ctx->header); - g_free(ctx); - return SR_ERR; - } - if (!(ctx->old_sample = g_try_malloc0(ctx->unitsize))) { sr_err("%s: ctx->old_sample malloc failed", __func__); g_free(ctx->header);