Some more g_try_*alloc() fixes.

As per documented rules in HACKING, we don't check "small" allocations.
This commit is contained in:
Uwe Hermann 2015-03-23 19:54:53 +01:00
parent 1a46cc62e2
commit a95f142e88
14 changed files with 29 additions and 77 deletions

View File

@ -122,10 +122,7 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options)
} }
len = 128; len = 128;
if (!(buf = g_try_malloc(len))) { buf = g_malloc(len);
sr_err("Serial buffer malloc failed.");
return NULL;
}
serial_readline(serial, &buf, &len, 250); serial_readline(serial, &buf, &len, 250);
if (!len) if (!len)
return NULL; return NULL;

View File

@ -59,10 +59,8 @@ static struct sr_datafeed_analog *handle_qm_18x(const struct sr_dev_inst *sdi,
while (*e && *e == ' ') while (*e && *e == ' ')
e++; e++;
if (!(analog = g_try_malloc0(sizeof(struct sr_datafeed_analog)))) analog = g_malloc0(sizeof(struct sr_datafeed_analog));
return NULL; analog->data = g_malloc(sizeof(float));
if (!(analog->data = g_try_malloc(sizeof(float))))
return NULL;
analog->channels = sdi->channels; analog->channels = sdi->channels;
analog->num_samples = 1; analog->num_samples = 1;
if (is_oor) if (is_oor)
@ -170,10 +168,8 @@ static struct sr_datafeed_analog *handle_qm_28x(const struct sr_dev_inst *sdi,
return NULL; return NULL;
} }
if (!(analog = g_try_malloc0(sizeof(struct sr_datafeed_analog)))) analog = g_malloc0(sizeof(struct sr_datafeed_analog));
return NULL; analog->data = g_malloc(sizeof(float));
if (!(analog->data = g_try_malloc(sizeof(float))))
return NULL;
analog->channels = sdi->channels; analog->channels = sdi->channels;
analog->num_samples = 1; analog->num_samples = 1;
*analog->data = fvalue; *analog->data = fvalue;

View File

@ -543,33 +543,15 @@ static struct scope_state *scope_state_new(const struct scope_config *config)
{ {
struct scope_state *state; struct scope_state *state;
if (!(state = g_try_malloc0(sizeof(struct scope_state)))) state = g_malloc0(sizeof(struct scope_state));
return NULL; state->analog_channels = g_malloc0_n(config->analog_channels,
sizeof(struct analog_channel_state));
if (!(state->analog_channels = g_try_malloc0_n(config->analog_channels, state->digital_channels = g_malloc0_n(
sizeof(struct analog_channel_state)))) config->digital_channels, sizeof(gboolean));
goto fail; state->digital_pods = g_malloc0_n(config->digital_pods,
sizeof(gboolean));
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;
return state; 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) SR_PRIV void hmo_scope_state_free(struct scope_state *state)

View File

@ -827,8 +827,7 @@ static void receive_transfer(struct libusb_transfer *transfer)
} }
} else { } else {
/* Already past the trigger point, just send it all out. */ /* Already past the trigger point, just send it all out. */
send_chunk(sdi, transfer->buffer, send_chunk(sdi, transfer->buffer, num_samples);
num_samples);
} }
devc->samp_received += 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; devc->trigger_offset = trigger_offset;
num_channels = (devc->ch1_enabled && devc->ch2_enabled) ? 2 : 1; num_channels = (devc->ch1_enabled && devc->ch2_enabled) ? 2 : 1;
/* TODO: Check malloc return value. */ devc->framebuf = g_malloc(devc->framesize * num_channels * 2);
devc->framebuf = g_try_malloc(devc->framesize * num_channels * 2);
devc->samp_buffered = devc->samp_received = 0; devc->samp_buffered = devc->samp_received = 0;
/* Tell the scope to send us the first frame. */ /* Tell the scope to send us the first frame. */

View File

@ -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, usb_source_add(sdi->session, drvc->sr_ctx, 100,
lascar_el_usb_handle_events, (void *)sdi); 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, libusb_fill_bulk_transfer(xfer_in, usb->devhdl, LASCAR_EP_IN,
buf, 4096, lascar_el_usb_receive_transfer, cb_data, 100); buf, 4096, lascar_el_usb_receive_transfer, cb_data, 100);
if ((ret = libusb_submit_transfer(xfer_in) != 0)) { if ((ret = libusb_submit_transfer(xfer_in) != 0)) {

View File

@ -42,11 +42,7 @@ SR_PRIV int mso_send_control_message(struct sr_serial_dev_inst *serial,
if (serial->fd < 0) if (serial->fd < 0)
goto ret; goto ret;
if (!(buf = g_try_malloc(s))) { buf = g_malloc(s);
sr_err("Failed to malloc message buffer.");
ret = SR_ERR_MALLOC;
goto ret;
}
p = buf; p = buf;
memcpy(p, mso_head, sizeof(mso_head)); memcpy(p, mso_head, sizeof(mso_head));

View File

@ -113,10 +113,7 @@ static GSList *scan(struct sr_dev_driver* drv, GSList *options)
serial_flush(serial); serial_flush(serial);
if (!(buf = g_try_malloc(BUF_MAX))) { buf = g_malloc(BUF_MAX);
sr_err("Serial buffer malloc failed.");
return NULL;
}
snprintf(req, sizeof(req), "%s\r\n", snprintf(req, sizeof(req), "%s\r\n",
nmadmm_requests[NMADMM_REQ_IDN].req_str); nmadmm_requests[NMADMM_REQ_IDN].req_str);

View File

@ -110,10 +110,7 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options)
devc->flag_reg = 0; devc->flag_reg = 0;
/* Allocate memory for the incoming ftdi data. */ /* Allocate memory for the incoming ftdi data. */
if (!(devc->ftdi_buf = g_try_malloc0(FTDI_BUF_SIZE))) { devc->ftdi_buf = g_malloc0(FTDI_BUF_SIZE);
sr_err("ftdi_buf malloc failed.");
goto err_free_devc;
}
/* Allocate memory for the FTDI context (ftdic) and initialize it. */ /* Allocate memory for the FTDI context (ftdic) and initialize it. */
if (!(devc->ftdic = ftdi_new())) { if (!(devc->ftdic = ftdi_new())) {
@ -197,7 +194,6 @@ err_free_ftdic:
ftdi_free(devc->ftdic); /* NOT free() or g_free()! */ ftdi_free(devc->ftdic); /* NOT free() or g_free()! */
err_free_ftdi_buf: err_free_ftdi_buf:
g_free(devc->ftdi_buf); g_free(devc->ftdi_buf);
err_free_devc:
g_free(devc); g_free(devc);
return NULL; return NULL;

View File

@ -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; devc->num_timebases = &timebases[i] - devc->timebases + 1;
} }
for (i = 0; i < NUM_VDIV; i++) for (i = 0; i < NUM_VDIV; i++) {
if (!memcmp(&devc->model->series->min_vdiv, &vdivs[i], sizeof(uint64_t[2]))) { if (!memcmp(&devc->model->series->min_vdiv,
&vdivs[i], sizeof(uint64_t[2]))) {
devc->vdivs = &vdivs[i]; devc->vdivs = &vdivs[i];
devc->num_vdivs = NUM_VDIV - i; devc->num_vdivs = NUM_VDIV - i;
} }
}
if (!(devc->buffer = g_try_malloc(ACQ_BUFFER_SIZE))) devc->buffer = g_malloc(ACQ_BUFFER_SIZE);
return NULL; devc->data = g_malloc(ACQ_BUFFER_SIZE * sizeof(float));
if (!(devc->data = g_try_malloc(ACQ_BUFFER_SIZE * sizeof(float))))
return NULL;
devc->data_source = DATA_SOURCE_LIVE; devc->data_source = DATA_SOURCE_LIVE;

View File

@ -959,11 +959,7 @@ SR_PRIV struct acquisition_state *lwla_alloc_acquisition_state(void)
{ {
struct acquisition_state *acq; struct acquisition_state *acq;
acq = g_try_new0(struct acquisition_state, 1); acq = g_malloc0(sizeof(struct acquisition_state));
if (!acq) {
sr_err("Acquisition state malloc failed.");
return NULL;
}
acq->xfer_in = libusb_alloc_transfer(0); acq->xfer_in = libusb_alloc_transfer(0);
if (!acq->xfer_in) { if (!acq->xfer_in) {

View File

@ -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) if (testo_request_packet(sdi) != SR_OK)
return SR_ERR; return SR_ERR;
buf = g_try_malloc(MAX_REPLY_SIZE); buf = g_malloc(MAX_REPLY_SIZE);
transfer = libusb_alloc_transfer(0); transfer = libusb_alloc_transfer(0);
libusb_fill_bulk_transfer(transfer, usb->devhdl, EP_IN, buf, libusb_fill_bulk_transfer(transfer, usb->devhdl, EP_IN, buf,
MAX_REPLY_SIZE, receive_transfer, (void *)sdi, 100); MAX_REPLY_SIZE, receive_transfer, (void *)sdi, 100);

View File

@ -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, usb_source_add(sdi->session, drvc->sr_ctx, 100,
handle_events, (void *)sdi); handle_events, (void *)sdi);
buf = g_try_malloc(DMM_DATA_SIZE); buf = g_malloc(DMM_DATA_SIZE);
transfer = libusb_alloc_transfer(0); transfer = libusb_alloc_transfer(0);
/* Each transfer request gets 100ms to arrive before it's restarted. /* Each transfer request gets 100ms to arrive before it's restarted.
* The device only sends 1 transfer/second no matter how many * The device only sends 1 transfer/second no matter how many

View File

@ -592,10 +592,7 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi,
return SR_OK; return SR_OK;
} }
if (!(buf = g_try_malloc(PACKET_SIZE))) { buf = g_malloc(PACKET_SIZE);
sr_err("Packet buffer malloc failed.");
return SR_ERR_MALLOC;
}
/* Check if the trigger is in the samples we are throwing away */ /* Check if the trigger is in the samples we are throwing away */
trigger_now = now_address == trigger_address || trigger_now = now_address == trigger_address ||

View File

@ -117,10 +117,7 @@ static int receive_data(int fd, int revents, void *cb_data)
} }
} }
if (!(buf = g_try_malloc(CHUNKSIZE))) { buf = g_malloc(CHUNKSIZE);
sr_err("%s: buf malloc failed", __func__);
return FALSE;
}
ret = zip_fread(vdev->capfile, buf, ret = zip_fread(vdev->capfile, buf,
CHUNKSIZE / vdev->unitsize * vdev->unitsize); CHUNKSIZE / vdev->unitsize * vdev->unitsize);