VCD: Improve error handling/checking.

Check malloc()/calloc() return values and so on. Also, add missing
free() calls and some TODOs. Simplify some code.
This commit is contained in:
Uwe Hermann 2010-05-08 16:09:25 +02:00
parent d2b36a10d7
commit 6b5e3ceefc
1 changed files with 33 additions and 24 deletions

View File

@ -45,7 +45,6 @@ $dumpvars\n";
const char *vcd_header_comment = "\ const char *vcd_header_comment = "\
$comment\n Acquisition with %d/%d probes at %s\n$end\n"; $comment\n Acquisition with %d/%d probes at %s\n$end\n";
static int init(struct output *o) static int init(struct output *o)
{ {
/* Maximum header length */ /* Maximum header length */
@ -59,8 +58,7 @@ static int init(struct output *o)
char *c, *samplerate_s; char *c, *samplerate_s;
char wbuf[1000], comment[128]; char wbuf[1000], comment[128];
ctx = malloc(sizeof(struct context)); if (!(ctx = calloc(1, sizeof(struct context))))
if (ctx == NULL)
return SIGROK_ERR_MALLOC; return SIGROK_ERR_MALLOC;
o->internal = ctx; o->internal = ctx;
ctx->num_enabled_probes = 0; ctx->num_enabled_probes = 0;
@ -75,42 +73,51 @@ static int init(struct output *o)
/* TODO: Allow for configuration via o->param. */ /* TODO: Allow for configuration via o->param. */
ctx->header = calloc(1, MAX_HEADER_LEN + 1); if (!(ctx->header = calloc(1, MAX_HEADER_LEN + 1))) {
if (ctx->header == NULL) free(ctx);
return SIGROK_ERR_MALLOC; return SIGROK_ERR_MALLOC;
}
num_probes = g_slist_length(o->device->probes); num_probes = g_slist_length(o->device->probes);
comment[0] = '\0';
if (o->device->plugin) { if (o->device->plugin) {
/* TODO: Handle num_probes == 0, too many probes, etc. */ /* TODO: Handle num_probes == 0, too many probes, etc. */
/* TODO: Error handling. */
samplerate = *((uint64_t *) o->device->plugin->get_device_info( samplerate = *((uint64_t *) o->device->plugin->get_device_info(
o->device->plugin_index, DI_CUR_SAMPLERATE)); o->device->plugin_index, DI_CUR_SAMPLERATE));
if ((samplerate_s = sigrok_samplerate_string(samplerate)) == NULL) if (!((samplerate_s = sigrok_samplerate_string(samplerate)))) {
free(ctx->header);
free(ctx);
return SIGROK_ERR; return SIGROK_ERR;
snprintf(comment, 127, vcd_header_comment, ctx->num_enabled_probes, }
num_probes, samplerate_s); /* TODO: Handle sprintf() errors. */
snprintf(comment, 127, vcd_header_comment,
ctx->num_enabled_probes, num_probes, samplerate_s);
free(samplerate_s); free(samplerate_s);
} }
else
comment[0] = '\0';
/* Wires / channels */ /* Wires / channels */
wbuf[0] = '\0'; wbuf[0] = '\0';
for (i = 0; i < ctx->num_enabled_probes; i++) { for (i = 0; i < ctx->num_enabled_probes; i++) {
c = (char *)&wbuf + strlen((char *)&wbuf); c = (char *)&wbuf + strlen((char *)&wbuf);
/* TODO: Needs fixing for very large number of probes. */
/* TODO: Handle sprintf() errors. */
sprintf(c, "$var wire 1 %c channel%s $end\n", sprintf(c, "$var wire 1 %c channel%s $end\n",
(char)('!' + i), ctx->probelist[i]); (char)('!' + i), ctx->probelist[i]);
} }
/* TODO: Date: File or signals? Make y/n configurable. */ /* TODO: Date: File or signals? Make y/n configurable. */
b = snprintf(ctx->header, MAX_HEADER_LEN, vcd_header, "TODO: Date", b = snprintf(ctx->header, MAX_HEADER_LEN, vcd_header, "TODO: Date",
PACKAGE_STRING, comment, 1, "ns", PACKAGE, (char *)&wbuf); PACKAGE_STRING, comment, 1, "ns", PACKAGE, (char *)&wbuf);
/* TODO: Handle snprintf errors. */ /* TODO: Handle snprintf() errors. */
ctx->prevbits = calloc(sizeof(int), num_probes); if (!(ctx->prevbits = calloc(sizeof(int), num_probes))) {
if (ctx->prevbits == NULL) free(ctx->header);
free(ctx);
return SIGROK_ERR_MALLOC; return SIGROK_ERR_MALLOC;
}
return 0; return SIGROK_OK;
} }
static int event(struct output *o, int event_type, char **data_out, static int event(struct output *o, int event_type, char **data_out,
@ -123,12 +130,14 @@ static int event(struct output *o, int event_type, char **data_out,
ctx = o->internal; ctx = o->internal;
switch (event_type) { switch (event_type) {
case DF_TRIGGER: case DF_TRIGGER:
/* TODO */
break; break;
case DF_END: case DF_END:
outlen = strlen("$dumpoff\n$end\n"); outlen = strlen("$dumpoff\n$end\n");
outbuf = malloc(outlen + 1); if (!(outbuf = malloc(outlen + 1)))
if (outbuf == NULL)
return SIGROK_ERR_MALLOC; return SIGROK_ERR_MALLOC;
/* TODO: Bug? Drop the + 1? */
/* TODO: Handle snprintf() errors. */
snprintf(outbuf, outlen + 1, "$dumpoff\n$end\n"); snprintf(outbuf, outlen + 1, "$dumpoff\n$end\n");
*data_out = outbuf; *data_out = outbuf;
*length_out = outlen; *length_out = outlen;
@ -153,16 +162,17 @@ static int data(struct output *o, char *data_in, uint64_t length_in,
outsize = 0; outsize = 0;
if (ctx->header) if (ctx->header)
outsize = strlen(ctx->header); outsize = strlen(ctx->header);
outbuf = calloc(1, outsize + 1 + 10000); /* FIXME: Use realloc(). */
if (outbuf == NULL) /* FIXME: Use realloc(). */
return SIGROK_ERR_MALLOC; if (!(outbuf = calloc(1, outsize + 1 + 10000)))
return SIGROK_ERR_MALLOC; /* TODO: free()? What to free? */
outbuf[0] = '\0';
if (ctx->header) { if (ctx->header) {
/* The header is still here, this must be the first packet. */ /* The header is still here, this must be the first packet. */
strncpy(outbuf, ctx->header, outsize); strncpy(outbuf, ctx->header, outsize);
free(ctx->header); free(ctx->header);
ctx->header = NULL; ctx->header = NULL;
} else {
outbuf[0] = 0;
} }
/* TODO: Are disabled probes handled correctly? */ /* TODO: Are disabled probes handled correctly? */
@ -187,8 +197,7 @@ static int data(struct output *o, char *data_in, uint64_t length_in,
sprintf(c, "#%i\n", offset * 1 /* TODO */); sprintf(c, "#%i\n", offset * 1 /* TODO */);
c = outbuf + strlen(outbuf); c = outbuf + strlen(outbuf);
sprintf(c, "%i%c\n", curbit, sprintf(c, "%i%c\n", curbit, (char)('!' + p));
(char)('!' + p /* FIXME? */));
} }
} }