From c2c4a0def11bd2a589aa62f8f501ff20ef5f99e4 Mon Sep 17 00:00:00 2001 From: Uwe Hermann Date: Sat, 11 Feb 2012 20:06:46 +0100 Subject: [PATCH] Use g_try_malloc/g_free/g_strdup consistently. Avoid plain malloc()/free() in sr/srd, especially in the API calls. Also avoid g_malloc*() in favor of g_try_malloc*(). Use g_strdup() instead of strdup() so that we can use g_free() consistently everywhere. Exceptions: Stuff that is allocated via other libs (not using glib), should also be properly free'd using the respective free-ing function (instead of g_free()). Examples: Stuff allocated by libusb, libftdi, etc. Also, use sr_err() instead of sr_warn() for actual errors. sr_warn() is meant for non-fatal/uncritical warnings. --- parsers.c | 4 +++- sigrok-cli.c | 16 ++++++++-------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/parsers.c b/parsers.c index 2dbbd93..fdf8562 100644 --- a/parsers.c +++ b/parsers.c @@ -33,7 +33,9 @@ char **parse_probestring(int max_probes, const char *probestring) error = FALSE; range = NULL; - probelist = g_malloc0(max_probes * sizeof(char *)); + if (!(probelist = g_try_malloc0(max_probes * sizeof(char *)))) { + /* TODO: Handle errors. */ + } tokens = g_strsplit(probestring, ",", max_probes); for (i = 0; tokens[i]; i++) { diff --git a/sigrok-cli.c b/sigrok-cli.c index d51c893..86ac278 100644 --- a/sigrok-cli.c +++ b/sigrok-cli.c @@ -234,17 +234,17 @@ static void show_device_detail(void) if (!(s = sr_samplerate_string(samplerates->low))) continue; printf(" (%s", s); - free(s); + g_free(s); /* high */ if (!(s = sr_samplerate_string(samplerates->high))) continue; printf(" - %s", s); - free(s); + g_free(s); /* step */ if (!(s = sr_samplerate_string(samplerates->step))) continue; printf(" in steps of %s)\n", s); - free(s); + g_free(s); } else { printf(" - supported samplerates:\n"); for (i = 0; samplerates->list[i]; i++) { @@ -313,7 +313,7 @@ static void datafeed_in(struct sr_device *device, struct sr_datafeed_packet *pac case SR_DF_HEADER: g_message("cli: Received SR_DF_HEADER"); /* Initialize the output module. */ - if (!(o = malloc(sizeof(struct sr_output)))) { + if (!(o = g_try_malloc(sizeof(struct sr_output)))) { printf("Output module malloc failed.\n"); exit(1); } @@ -370,7 +370,7 @@ static void datafeed_in(struct sr_device *device, struct sr_datafeed_packet *pac if (output_len) { if (outfile) fwrite(output_buf, 1, output_len, outfile); - free(output_buf); + g_free(output_buf); output_len = 0; } } @@ -383,7 +383,7 @@ static void datafeed_in(struct sr_device *device, struct sr_datafeed_packet *pac sr_session_halt(); if (outfile && outfile != stdout) fclose(outfile); - free(o); + g_free(o); o = NULL; break; case SR_DF_TRIGGER: @@ -447,7 +447,7 @@ static void datafeed_in(struct sr_device *device, struct sr_datafeed_packet *pac o->format->data(o, filter_out, filter_out_len, &output_buf, &output_len); if (output_len) { fwrite(output_buf, 1, output_len, outfile); - free(output_buf); + g_free(output_buf); } } @@ -646,7 +646,7 @@ static void load_input_file_format(void) } /* Initialize the input module. */ - if (!(in = malloc(sizeof(struct sr_input)))) { + if (!(in = g_try_malloc(sizeof(struct sr_input)))) { printf("Failed to allocate input module.\n"); exit(1); } -- 2.30.2