]> sigrok.org Git - libsigrok.git/commitdiff
Replace g_malloc{0,} with g_try_malloc{0,}.
authorUwe Hermann <redacted>
Sat, 16 Apr 2011 12:17:51 +0000 (14:17 +0200)
committerUwe Hermann <redacted>
Sat, 16 Apr 2011 16:08:15 +0000 (18:08 +0200)
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.

datastore.c
device.c
hardware/asix-sigma/asix-sigma.c
hardware/openbench-logic-sniffer/ols.c
hardware/saleae-logic/saleae-logic.c
hardware/zeroplus-logic-cube/zeroplus.c
output/output_ols.c
session_driver.c
session_file.c
strutil.c

index 7f35ee5aae5b343101d579be4d6ea1ef03169e73..11ae0e15ce039d69efb6e8b93515536d4a0be211 100644 (file)
@@ -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;
index 6e9a30086b610a590ac9c3190ff5698751758166..0cc5e55256978230eb565aa879ad22b74d31ff2e 100644 (file)
--- 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) {
index 2722d0db10fd5a2339e2bef2eab0b02c8cd31740..598d0b065ad22c0ee68524a05cf31892d07b3da2 100644 (file)
@@ -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. */
index ccdd8a03e3a59c72f434a4f7cf3d571d9b1d9026..96e961ab9d22835cf79f0318b76b337c60979181 100644 (file)
@@ -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;
index 72dbe1a40666115f21adafecc99b6c5c86e84e1d..5557201f962e79d2b57f0ac6bc616f17296a3e1d 100644 (file)
@@ -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,
index 7ce5c61b2fb185aa91ac1a95665050d6e98bd750..a2d616f21f7f7713ce8a47e8cc250bcf8ee8b4f3 100644 (file)
@@ -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);
index df94c8c92bb117b1f3490ddbeacdcb791ca5d772..5097de4b4c05a90f1f744c3e7025dab97be3b1e1 100644 (file)
@@ -29,6 +29,7 @@
 #include <string.h>
 #include <glib.h>
 #include <sigrok.h>
+#include <sigrok-internal.h>
 #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;
index b69cf2435015a193024457c7e44bb1d7cdc0b6bf..9ad4fb8ab8678f8a7760f52ff89ddabdd13326eb 100644 (file)
@@ -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",
index 764cb68fde38f05b1d6adf7e7589ad4525861016..0e3c56bec13be086a4cf7ce10f725c17363773a2 100644 (file)
@@ -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);
index e5278dfe3fda303156eb05b62bc95577f71b4158..8b0203b01765cd06ff8fb35b07fb0df0309f0a1c 100644 (file)
--- a/strutil.c
+++ b/strutil.c
@@ -22,6 +22,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <sigrok.h>
+#include <sigrok-internal.h>
 
 /**
  * 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)