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.
This commit is contained in:
Uwe Hermann 2011-04-16 14:17:51 +02:00
parent 50959ddcdc
commit b53738baf7
10 changed files with 122 additions and 49 deletions

View 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;

View File

@ -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) {

View 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. */

View 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;

View 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,

View 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);

View 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;

View 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",

View 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);

View File

@ -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)