From: Uwe Hermann Date: Sat, 16 Apr 2011 12:17:51 +0000 (+0200) Subject: Replace g_malloc{0,} with g_try_malloc{0,}. X-Git-Tag: libsigrok-0.1.0~285 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=b53738baf76219237e0a6629905981d7a1f2508e;p=libsigrok.git Replace g_malloc{0,} with g_try_malloc{0,}. The g_malloc()/g_malloc0() versions exit/segfault if not enough memory is available, which is not a good thing in libsigrok. Instead, we use the g_try_malloc()/g_try_malloc0() variants, which return NULL if not enough memory is available, so that the caller can handle the error properly. --- diff --git a/datastore.c b/datastore.c index 7f35ee5a..11ae0e15 100644 --- a/datastore.c +++ b/datastore.c @@ -34,8 +34,10 @@ int sr_datastore_new(int unitsize, struct sr_datastore **ds) if (unitsize <= 0) return SR_ERR; /* TODO: Different error? */ - if (!(*ds = g_malloc(sizeof(struct sr_datastore)))) + if (!(*ds = g_try_malloc(sizeof(struct sr_datastore)))) { + sr_err("ds: %s: ds malloc failed", __func__); return SR_ERR_MALLOC; + } (*ds)->ds_unitsize = unitsize; (*ds)->num_units = 0; diff --git a/device.c b/device.c index 6e9a3008..0cc5e552 100644 --- a/device.c +++ b/device.c @@ -86,7 +86,11 @@ struct sr_device *sr_device_new(struct sr_device_plugin *plugin, int plugin_inde struct sr_device *device; int i; - device = g_malloc0(sizeof(struct sr_device)); + if (!(device = g_try_malloc0(sizeof(struct sr_device)))) { + sr_err("dev: %s: device malloc failed", __func__); + return NULL; + } + device->plugin = plugin; device->plugin_index = plugin_index; devices = g_slist_append(devices, device); @@ -154,7 +158,13 @@ void sr_device_probe_add(struct sr_device *device, const char *name) int probenum; probenum = g_slist_length(device->probes) + 1; - p = g_malloc0(sizeof(struct sr_probe)); + + if (!(p = g_try_malloc0(sizeof(struct sr_probe)))) { + sr_err("dev: %s: p malloc failed", __func__); + // return SR_ERR_MALLOC; + return; /* FIXME: should return int. */ + } + p->index = probenum; p->enabled = TRUE; if (name) { diff --git a/hardware/asix-sigma/asix-sigma.c b/hardware/asix-sigma/asix-sigma.c index 2722d0db..598d0b06 100644 --- a/hardware/asix-sigma/asix-sigma.c +++ b/hardware/asix-sigma/asix-sigma.c @@ -318,25 +318,27 @@ static int bin2bitbang(const char *filename, f = g_fopen(filename, "rb"); if (!f) { sr_warn("g_fopen(\"%s\", \"rb\")", filename); - return -1; + return SR_ERR; } if (-1 == fseek(f, 0, SEEK_END)) { sr_warn("fseek on %s failed", filename); fclose(f); - return -1; + return SR_ERR; } file_size = ftell(f); fseek(f, 0, SEEK_SET); - compressed_buf = g_malloc(file_size); - firmware = g_malloc(buffer_size); + if (!(compressed_buf = g_try_malloc(file_size))) { + sr_err("asix: %s: compressed_buf malloc failed", __func__); + return SR_ERR_MALLOC; + } - if (!compressed_buf || !firmware) { - sr_warn("Error allocating buffers"); - return -1; + if (!(firmware = g_try_malloc(buffer_size))) { + sr_err("asix: %s: firmware malloc failed", __func__); + return SR_ERR_MALLOC; } csize = 0; @@ -352,18 +354,17 @@ static int bin2bitbang(const char *filename, g_free(compressed_buf); g_free(firmware); sr_warn("Could not unpack Sigma firmware. (Error %d)\n", ret); - return -1; + return SR_ERR; } g_free(compressed_buf); *buf_size = fwsize * 2 * 8; - *buf = p = (unsigned char *)g_malloc(*buf_size); - + *buf = p = (unsigned char *)g_try_malloc(*buf_size); if (!p) { - sr_warn("Error allocating buffers"); - return -1; + sr_err("asix: %s: buf/p malloc failed", __func__); + return SR_ERR_MALLOC; } for (i = 0; i < fwsize; ++i) { @@ -382,21 +383,24 @@ static int bin2bitbang(const char *filename, "offset=%ld, file_size=%ld, buf_size=%zd\n", filename, offset, file_size, *buf_size); - return -1; + return SR_ERR; } - return 0; + return SR_OK; } static int hw_init(const char *deviceinfo) { struct sr_device_instance *sdi; - struct sigma *sigma = g_malloc(sizeof(struct sigma)); + struct sigma *sigma; + /* Avoid compiler warnings. */ deviceinfo = deviceinfo; - if (!sigma) - return 0; + if (!(sigma = g_try_malloc(sizeof(struct sigma)))) { + sr_err("asix: %s: sigma malloc failed", __func__); + return 0; /* FIXME: Should be SR_ERR_MALLOC. */ + } ftdi_init(&sigma->ftdic); @@ -484,10 +488,10 @@ static int upload_firmware(int firmware_idx, struct sigma *sigma) snprintf(firmware_path, sizeof(firmware_path), "%s/%s", FIRMWARE_DIR, firmware_files[firmware_idx]); - if (-1 == bin2bitbang(firmware_path, &buf, &buf_size)) { + if ((ret = bin2bitbang(firmware_path, &buf, &buf_size)) != SR_OK) { sr_warn("An error occured while reading the firmware: %s", firmware_path); - return SR_ERR; + return ret; } /* Upload firmare. */ diff --git a/hardware/openbench-logic-sniffer/ols.c b/hardware/openbench-logic-sniffer/ols.c index ccdd8a03..96e961ab 100644 --- a/hardware/openbench-logic-sniffer/ols.c +++ b/hardware/openbench-logic-sniffer/ols.c @@ -174,7 +174,11 @@ static struct ols_device *ols_device_new(void) { struct ols_device *ols; - ols = g_malloc0(sizeof(struct ols_device)); + if (!(ols = g_try_malloc0(sizeof(struct ols_device)))) { + sr_err("ols: %s: ols malloc failed", __func__); + return NULL; + } + ols->trigger_at = -1; ols->probe_mask = 0xffffffff; ols->cur_samplerate = SR_KHZ(200); @@ -651,7 +655,12 @@ static int receive_data(int fd, int revents, void *user_data) if (ols->sample[0] & 0x80 && !(ols->last_sample[0] & 0x80)) { count = (int)(*ols->sample) & 0x7fffffff; - buffer = g_malloc(count); + if (!(buffer = g_try_malloc(count))) { + sr_err("ols: %s: buffer malloc " + "failed", __func__); + return FALSE; + } + buflen = 0; for (i = 0; i < count; i++) { memcpy(buffer + buflen, ols->last_sample, 4); @@ -877,11 +886,17 @@ static int hw_start_acquisition(int device_index, gpointer session_device_id) sr_source_add(sdi->serial->fd, G_IO_IN, -1, receive_data, session_device_id); + if (!(packet = g_try_malloc(sizeof(struct sr_datafeed_packet)))) { + sr_err("ols: %s: packet malloc failed", __func__); + return SR_ERR_MALLOC; + } + + if (!(header = g_try_malloc(sizeof(struct sr_datafeed_header)))) { + sr_err("ols: %s: header malloc failed", __func__); + return SR_ERR_MALLOC; + } + /* Send header packet to the session bus. */ - packet = g_malloc(sizeof(struct sr_datafeed_packet)); - header = g_malloc(sizeof(struct sr_datafeed_header)); - if (!packet || !header) - return SR_ERR; packet->type = SR_DF_HEADER; packet->length = sizeof(struct sr_datafeed_header); packet->payload = (unsigned char *)header; diff --git a/hardware/saleae-logic/saleae-logic.c b/hardware/saleae-logic/saleae-logic.c index 72dbe1a4..5557201f 100644 --- a/hardware/saleae-logic/saleae-logic.c +++ b/hardware/saleae-logic/saleae-logic.c @@ -569,7 +569,12 @@ void receive_transfer(struct libusb_transfer *transfer) user_data = transfer->user_data; /* Fire off a new request. */ - new_buf = g_malloc(4096); + if (!(new_buf = g_try_malloc(4096))) { + sr_err("saleae: %s: new_buf malloc failed", __func__); + // return SR_ERR_MALLOC; + return; /* FIXME */ + } + transfer->buffer = new_buf; transfer->length = 4096; if (libusb_submit_transfer(transfer) != 0) { @@ -680,15 +685,23 @@ static int hw_start_acquisition(int device_index, gpointer session_device_id) if (!(sdi = sr_get_device_instance(device_instances, device_index))) return SR_ERR; - packet = g_malloc(sizeof(struct sr_datafeed_packet)); - header = g_malloc(sizeof(struct sr_datafeed_header)); - if (!packet || !header) - return SR_ERR; + if (!(packet = g_try_malloc(sizeof(struct sr_datafeed_packet)))) { + sr_err("saleae: %s: packet malloc failed", __func__); + return SR_ERR_MALLOC; + } + + if (!(header = g_try_malloc(sizeof(struct sr_datafeed_header)))) { + sr_err("saleae: %s: header malloc failed", __func__); + return SR_ERR_MALLOC; + } /* Start with 2K transfer, subsequently increased to 4K. */ size = 2048; for (i = 0; i < NUM_SIMUL_TRANSFERS; i++) { - buf = g_malloc(size); + if (!(buf = g_try_malloc(size))) { + sr_err("saleae: %s: buf malloc failed", __func__); + return SR_ERR_MALLOC; + } transfer = libusb_alloc_transfer(0); libusb_fill_bulk_transfer(transfer, sdi->usb->devhdl, 2 | LIBUSB_ENDPOINT_IN, buf, size, diff --git a/hardware/zeroplus-logic-cube/zeroplus.c b/hardware/zeroplus-logic-cube/zeroplus.c index 7ce5c61b..a2d616f2 100644 --- a/hardware/zeroplus-logic-cube/zeroplus.c +++ b/hardware/zeroplus-logic-cube/zeroplus.c @@ -518,9 +518,11 @@ static int hw_start_acquisition(int device_index, gpointer session_device_id) header.num_analog_probes = 0; sr_session_bus(session_device_id, &packet); - buf = g_malloc(PACKET_SIZE); - if (!buf) - return SR_ERR; + if (!(buf = g_try_malloc(PACKET_SIZE))) { + sr_err("zeroplus: %s: buf malloc failed", __func__); + return SR_ERR_MALLOC; + } + analyzer_read_start(sdi->usb->devhdl); /* Send the incoming transfer to the session bus. */ for (packet_num = 0; packet_num < (memory_size * 4 / PACKET_SIZE); diff --git a/output/output_ols.c b/output/output_ols.c index df94c8c9..5097de4b 100644 --- a/output/output_ols.c +++ b/output/output_ols.c @@ -29,6 +29,7 @@ #include #include #include +#include #include "config.h" struct context { @@ -45,8 +46,10 @@ static int init(struct sr_output *o) uint64_t samplerate; int num_enabled_probes; - if (!(ctx = g_malloc(sizeof(struct context)))) + if (!(ctx = g_try_malloc(sizeof(struct context)))) { + sr_err("ols out: %s: ctx malloc failed", __func__); return SR_ERR_MALLOC; + } o->internal = ctx; ctx->num_samples = 0; diff --git a/session_driver.c b/session_driver.c index b69cf243..9ad4fb8a 100644 --- a/session_driver.c +++ b/session_driver.c @@ -83,7 +83,12 @@ static int feed_chunk(int fd, int revents, void *user_data) /* already done with this instance */ continue; - buf = g_malloc(CHUNKSIZE); + if (!(buf = g_try_malloc(CHUNKSIZE))) { + sr_err("session: %s: buf malloc failed", __func__); + // return SR_ERR_MALLOC; + return FALSE; + } + ret = zip_fread(vdevice->capfile, buf, CHUNKSIZE); if (ret > 0) { got_data = TRUE; @@ -139,9 +144,12 @@ static int hw_opendev(int device_index) NULL, NULL, NULL); if (!sdi) return SR_ERR; - sdi->priv = g_malloc0(sizeof(struct session_vdevice)); - if (!sdi->priv) - return SR_ERR; + + if (!(sdi->priv = g_try_malloc0(sizeof(struct session_vdevice)))) { + sr_err("session: %s: sdi->priv malloc failed", __func__); + return SR_ERR_MALLOC; + } + device_instances = g_slist_append(device_instances, sdi); return SR_OK; @@ -250,11 +258,17 @@ static int hw_start_acquisition(int device_index, gpointer session_device_id) /* freewheeling source */ sr_session_source_add(-1, 0, 0, feed_chunk, session_device_id); + if (!(packet = g_try_malloc(sizeof(struct sr_datafeed_packet)))) { + sr_err("session: %s: packet malloc failed", __func__); + return SR_ERR_MALLOC; + } + + if (!(header = g_try_malloc(sizeof(struct sr_datafeed_header)))) { + sr_err("session: %s: header malloc failed", __func__); + return SR_ERR_MALLOC; + } + /* Send header packet to the session bus. */ - packet = g_malloc(sizeof(struct sr_datafeed_packet)); - header = g_malloc(sizeof(struct sr_datafeed_header)); - if (!packet || !header) - return SR_ERR; packet->type = SR_DF_HEADER; packet->length = sizeof(struct sr_datafeed_header); packet->payload = (unsigned char *)header; @@ -271,7 +285,6 @@ static int hw_start_acquisition(int device_index, gpointer session_device_id) return SR_OK; } - struct sr_device_plugin session_driver = { "session", "Session-emulating driver", diff --git a/session_file.c b/session_file.c index 764cb68f..0e3c56be 100644 --- a/session_file.c +++ b/session_file.c @@ -66,7 +66,12 @@ int sr_session_load(const char *filename) sr_dbg("Not a valid sigrok session file."); return SR_ERR; } - metafile = g_malloc(zs.size); + + if (!(metafile = g_try_malloc(zs.size))) { + sr_err("session file: %s: metafile malloc failed", __func__); + return SR_ERR_MALLOC; + } + zf = zip_fopen_index(archive, zs.index, 0); zip_fread(zf, metafile, zs.size); zip_fclose(zf); diff --git a/strutil.c b/strutil.c index e5278dfe..8b0203b0 100644 --- a/strutil.c +++ b/strutil.c @@ -22,6 +22,7 @@ #include #include #include +#include /** * Convert a numeric samplerate value to its "natural" string representation. @@ -114,7 +115,12 @@ char **sr_parse_triggerstring(struct sr_device *device, max_probes = g_slist_length(device->probes); error = FALSE; - triggerlist = g_malloc0(max_probes * sizeof(char *)); + + if (!(triggerlist = g_try_malloc0(max_probes * sizeof(char *)))) { + sr_err("session file: %s: metafile malloc failed", __func__); + return NULL; + } + tokens = g_strsplit(triggerstring, ",", max_probes); trigger_types = device->plugin->get_device_info(0, SR_DI_TRIGGER_TYPES); if (trigger_types == NULL)