From: Uwe Hermann Date: Mon, 23 Mar 2015 18:54:53 +0000 (+0100) Subject: Some more g_try_*alloc() fixes. X-Git-Tag: libsigrok-0.4.0~564 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=a95f142e88fa5368adfabf87544acfdeed7d7604;p=libsigrok.git Some more g_try_*alloc() fixes. As per documented rules in HACKING, we don't check "small" allocations. --- diff --git a/src/hardware/agilent-dmm/api.c b/src/hardware/agilent-dmm/api.c index 19a4144c..e8ef427b 100644 --- a/src/hardware/agilent-dmm/api.c +++ b/src/hardware/agilent-dmm/api.c @@ -122,10 +122,7 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options) } len = 128; - if (!(buf = g_try_malloc(len))) { - sr_err("Serial buffer malloc failed."); - return NULL; - } + buf = g_malloc(len); serial_readline(serial, &buf, &len, 250); if (!len) return NULL; diff --git a/src/hardware/fluke-dmm/fluke.c b/src/hardware/fluke-dmm/fluke.c index c098ced4..e708518d 100644 --- a/src/hardware/fluke-dmm/fluke.c +++ b/src/hardware/fluke-dmm/fluke.c @@ -59,10 +59,8 @@ static struct sr_datafeed_analog *handle_qm_18x(const struct sr_dev_inst *sdi, while (*e && *e == ' ') e++; - if (!(analog = g_try_malloc0(sizeof(struct sr_datafeed_analog)))) - return NULL; - if (!(analog->data = g_try_malloc(sizeof(float)))) - return NULL; + analog = g_malloc0(sizeof(struct sr_datafeed_analog)); + analog->data = g_malloc(sizeof(float)); analog->channels = sdi->channels; analog->num_samples = 1; if (is_oor) @@ -170,10 +168,8 @@ static struct sr_datafeed_analog *handle_qm_28x(const struct sr_dev_inst *sdi, return NULL; } - if (!(analog = g_try_malloc0(sizeof(struct sr_datafeed_analog)))) - return NULL; - if (!(analog->data = g_try_malloc(sizeof(float)))) - return NULL; + analog = g_malloc0(sizeof(struct sr_datafeed_analog)); + analog->data = g_malloc(sizeof(float)); analog->channels = sdi->channels; analog->num_samples = 1; *analog->data = fvalue; diff --git a/src/hardware/hameg-hmo/protocol.c b/src/hardware/hameg-hmo/protocol.c index 8c5589bd..77027f1d 100644 --- a/src/hardware/hameg-hmo/protocol.c +++ b/src/hardware/hameg-hmo/protocol.c @@ -543,33 +543,15 @@ static struct scope_state *scope_state_new(const struct scope_config *config) { struct scope_state *state; - if (!(state = g_try_malloc0(sizeof(struct scope_state)))) - return NULL; - - if (!(state->analog_channels = g_try_malloc0_n(config->analog_channels, - sizeof(struct analog_channel_state)))) - goto fail; - - if (!(state->digital_channels = g_try_malloc0_n( - config->digital_channels, sizeof(gboolean)))) - goto fail; - - if (!(state->digital_pods = g_try_malloc0_n(config->digital_pods, - sizeof(gboolean)))) - goto fail; + state = g_malloc0(sizeof(struct scope_state)); + state->analog_channels = g_malloc0_n(config->analog_channels, + sizeof(struct analog_channel_state)); + state->digital_channels = g_malloc0_n( + config->digital_channels, sizeof(gboolean)); + state->digital_pods = g_malloc0_n(config->digital_pods, + sizeof(gboolean)); return state; - -fail: - if (state->analog_channels) - g_free(state->analog_channels); - if (state->digital_channels) - g_free(state->digital_channels); - if (state->digital_pods) - g_free(state->digital_pods); - g_free(state); - - return NULL; } SR_PRIV void hmo_scope_state_free(struct scope_state *state) diff --git a/src/hardware/hantek-dso/api.c b/src/hardware/hantek-dso/api.c index 1676085e..2b039ed2 100644 --- a/src/hardware/hantek-dso/api.c +++ b/src/hardware/hantek-dso/api.c @@ -827,8 +827,7 @@ static void receive_transfer(struct libusb_transfer *transfer) } } else { /* Already past the trigger point, just send it all out. */ - send_chunk(sdi, transfer->buffer, - num_samples); + send_chunk(sdi, transfer->buffer, num_samples); } devc->samp_received += num_samples; @@ -939,8 +938,7 @@ static int handle_event(int fd, int revents, void *cb_data) devc->trigger_offset = trigger_offset; num_channels = (devc->ch1_enabled && devc->ch2_enabled) ? 2 : 1; - /* TODO: Check malloc return value. */ - devc->framebuf = g_try_malloc(devc->framesize * num_channels * 2); + devc->framebuf = g_malloc(devc->framesize * num_channels * 2); devc->samp_buffered = devc->samp_received = 0; /* Tell the scope to send us the first frame. */ diff --git a/src/hardware/lascar-el-usb/api.c b/src/hardware/lascar-el-usb/api.c index ce63bc67..14273ce3 100644 --- a/src/hardware/lascar-el-usb/api.c +++ b/src/hardware/lascar-el-usb/api.c @@ -433,7 +433,7 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data) usb_source_add(sdi->session, drvc->sr_ctx, 100, lascar_el_usb_handle_events, (void *)sdi); - buf = g_try_malloc(4096); + buf = g_malloc(4096); libusb_fill_bulk_transfer(xfer_in, usb->devhdl, LASCAR_EP_IN, buf, 4096, lascar_el_usb_receive_transfer, cb_data, 100); if ((ret = libusb_submit_transfer(xfer_in) != 0)) { diff --git a/src/hardware/link-mso19/protocol.c b/src/hardware/link-mso19/protocol.c index fafd83c3..11fa8a61 100644 --- a/src/hardware/link-mso19/protocol.c +++ b/src/hardware/link-mso19/protocol.c @@ -42,11 +42,7 @@ SR_PRIV int mso_send_control_message(struct sr_serial_dev_inst *serial, if (serial->fd < 0) goto ret; - if (!(buf = g_try_malloc(s))) { - sr_err("Failed to malloc message buffer."); - ret = SR_ERR_MALLOC; - goto ret; - } + buf = g_malloc(s); p = buf; memcpy(p, mso_head, sizeof(mso_head)); diff --git a/src/hardware/norma-dmm/api.c b/src/hardware/norma-dmm/api.c index bf44df27..2743c234 100644 --- a/src/hardware/norma-dmm/api.c +++ b/src/hardware/norma-dmm/api.c @@ -113,10 +113,7 @@ static GSList *scan(struct sr_dev_driver* drv, GSList *options) serial_flush(serial); - if (!(buf = g_try_malloc(BUF_MAX))) { - sr_err("Serial buffer malloc failed."); - return NULL; - } + buf = g_malloc(BUF_MAX); snprintf(req, sizeof(req), "%s\r\n", nmadmm_requests[NMADMM_REQ_IDN].req_str); diff --git a/src/hardware/pipistrello-ols/api.c b/src/hardware/pipistrello-ols/api.c index defb3978..b84ff193 100644 --- a/src/hardware/pipistrello-ols/api.c +++ b/src/hardware/pipistrello-ols/api.c @@ -110,10 +110,7 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options) devc->flag_reg = 0; /* Allocate memory for the incoming ftdi data. */ - if (!(devc->ftdi_buf = g_try_malloc0(FTDI_BUF_SIZE))) { - sr_err("ftdi_buf malloc failed."); - goto err_free_devc; - } + devc->ftdi_buf = g_malloc0(FTDI_BUF_SIZE); /* Allocate memory for the FTDI context (ftdic) and initialize it. */ if (!(devc->ftdic = ftdi_new())) { @@ -197,7 +194,6 @@ err_free_ftdic: ftdi_free(devc->ftdic); /* NOT free() or g_free()! */ err_free_ftdi_buf: g_free(devc->ftdi_buf); -err_free_devc: g_free(devc); return NULL; diff --git a/src/hardware/rigol-ds/api.c b/src/hardware/rigol-ds/api.c index 111eff06..955b6acc 100644 --- a/src/hardware/rigol-ds/api.c +++ b/src/hardware/rigol-ds/api.c @@ -388,16 +388,16 @@ static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi) devc->num_timebases = &timebases[i] - devc->timebases + 1; } - for (i = 0; i < NUM_VDIV; i++) - if (!memcmp(&devc->model->series->min_vdiv, &vdivs[i], sizeof(uint64_t[2]))) { + for (i = 0; i < NUM_VDIV; i++) { + if (!memcmp(&devc->model->series->min_vdiv, + &vdivs[i], sizeof(uint64_t[2]))) { devc->vdivs = &vdivs[i]; devc->num_vdivs = NUM_VDIV - i; } + } - if (!(devc->buffer = g_try_malloc(ACQ_BUFFER_SIZE))) - return NULL; - if (!(devc->data = g_try_malloc(ACQ_BUFFER_SIZE * sizeof(float)))) - return NULL; + devc->buffer = g_malloc(ACQ_BUFFER_SIZE); + devc->data = g_malloc(ACQ_BUFFER_SIZE * sizeof(float)); devc->data_source = DATA_SOURCE_LIVE; diff --git a/src/hardware/sysclk-lwla/protocol.c b/src/hardware/sysclk-lwla/protocol.c index 2fd4dffa..7b8c4885 100644 --- a/src/hardware/sysclk-lwla/protocol.c +++ b/src/hardware/sysclk-lwla/protocol.c @@ -959,11 +959,7 @@ SR_PRIV struct acquisition_state *lwla_alloc_acquisition_state(void) { struct acquisition_state *acq; - acq = g_try_new0(struct acquisition_state, 1); - if (!acq) { - sr_err("Acquisition state malloc failed."); - return NULL; - } + acq = g_malloc0(sizeof(struct acquisition_state)); acq->xfer_in = libusb_alloc_transfer(0); if (!acq->xfer_in) { diff --git a/src/hardware/testo/api.c b/src/hardware/testo/api.c index 64d292dd..983451ea 100644 --- a/src/hardware/testo/api.c +++ b/src/hardware/testo/api.c @@ -490,7 +490,7 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data) if (testo_request_packet(sdi) != SR_OK) return SR_ERR; - buf = g_try_malloc(MAX_REPLY_SIZE); + buf = g_malloc(MAX_REPLY_SIZE); transfer = libusb_alloc_transfer(0); libusb_fill_bulk_transfer(transfer, usb->devhdl, EP_IN, buf, MAX_REPLY_SIZE, receive_transfer, (void *)sdi, 100); diff --git a/src/hardware/victor-dmm/api.c b/src/hardware/victor-dmm/api.c index 6548fb44..bed4ce15 100644 --- a/src/hardware/victor-dmm/api.c +++ b/src/hardware/victor-dmm/api.c @@ -395,7 +395,7 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data) usb_source_add(sdi->session, drvc->sr_ctx, 100, handle_events, (void *)sdi); - buf = g_try_malloc(DMM_DATA_SIZE); + buf = g_malloc(DMM_DATA_SIZE); transfer = libusb_alloc_transfer(0); /* Each transfer request gets 100ms to arrive before it's restarted. * The device only sends 1 transfer/second no matter how many diff --git a/src/hardware/zeroplus-logic-cube/api.c b/src/hardware/zeroplus-logic-cube/api.c index 48845c7a..5de6f317 100644 --- a/src/hardware/zeroplus-logic-cube/api.c +++ b/src/hardware/zeroplus-logic-cube/api.c @@ -592,10 +592,7 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi, return SR_OK; } - if (!(buf = g_try_malloc(PACKET_SIZE))) { - sr_err("Packet buffer malloc failed."); - return SR_ERR_MALLOC; - } + buf = g_malloc(PACKET_SIZE); /* Check if the trigger is in the samples we are throwing away */ trigger_now = now_address == trigger_address || diff --git a/src/session_driver.c b/src/session_driver.c index 8bf7fc6f..ae1b2118 100644 --- a/src/session_driver.c +++ b/src/session_driver.c @@ -117,10 +117,7 @@ static int receive_data(int fd, int revents, void *cb_data) } } - if (!(buf = g_try_malloc(CHUNKSIZE))) { - sr_err("%s: buf malloc failed", __func__); - return FALSE; - } + buf = g_malloc(CHUNKSIZE); ret = zip_fread(vdev->capfile, buf, CHUNKSIZE / vdev->unitsize * vdev->unitsize);