From d9b9e94cce65f1af0f2bb263394d1c7f63683ebd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabiano=20Fid=C3=AAncio?= Date: Fri, 11 Jul 2014 17:13:09 +0200 Subject: [PATCH] Prefer using g_malloc0()/g_free() As we already depend on GLib, let's use g_{malloc,new}0() instead of the standard malloc() or the spice_{malloc,new}*() and g_free() instead of the standard free() when possible. Memory allocated by other libraries using malloc() should still be freed by free(). As a side effect of the changes, we are muting a few warnings caught by coverity. --- gtk/channel-cursor.c | 2 +- gtk/channel-display-mjpeg.c | 8 +++----- gtk/channel-display.c | 8 ++++---- gtk/channel-main.c | 12 ++++++------ gtk/channel-record.c | 2 +- gtk/controller/test.c | 4 ++-- gtk/decode-glz.c | 10 +++++----- gtk/decode-jpeg.c | 2 +- gtk/decode-zlib.c | 2 +- gtk/spice-channel.c | 20 ++++++++++---------- gtk/spice-client-gtk.override | 7 ++++--- gtk/spice-session.c | 2 +- gtk/spice-widget.c | 2 +- gtk/usb-device-manager.c | 2 +- 14 files changed, 41 insertions(+), 42 deletions(-) diff --git a/gtk/channel-cursor.c b/gtk/channel-cursor.c index 36c0ca4..3f49641 100644 --- a/gtk/channel-cursor.c +++ b/gtk/channel-cursor.c @@ -367,7 +367,7 @@ static display_cursor *set_cursor(SpiceChannel *channel, SpiceCursor *scursor) g_return_val_if_fail(scursor->data_size != 0, NULL); size = 4 * hdr->width * hdr->height; - cursor = spice_malloc(sizeof(*cursor) + size); + cursor = g_malloc0(sizeof(*cursor) + size); cursor->hdr = *hdr; cursor->default_cursor = FALSE; cursor->refcount = 1; diff --git a/gtk/channel-display-mjpeg.c b/gtk/channel-display-mjpeg.c index 627aab4..7d9beea 100644 --- a/gtk/channel-display-mjpeg.c +++ b/gtk/channel-display-mjpeg.c @@ -71,11 +71,9 @@ void stream_mjpeg_data(display_stream *st) uint8_t *lines[4]; stream_get_dimensions(st, &width, &height); - dest = malloc(width * height * 4); + dest = g_malloc0(width * height * 4); - if (st->out_frame) { - free(st->out_frame); - } + g_free(st->out_frame); st->out_frame = dest; jpeg_read_header(&st->mjpeg_cinfo, 1); @@ -151,6 +149,6 @@ G_GNUC_INTERNAL void stream_mjpeg_cleanup(display_stream *st) { jpeg_destroy_decompress(&st->mjpeg_cinfo); - free(st->out_frame); + g_free(st->out_frame); st->out_frame = NULL; } diff --git a/gtk/channel-display.c b/gtk/channel-display.c index 80df0f3..9170bf4 100644 --- a/gtk/channel-display.c +++ b/gtk/channel-display.c @@ -783,7 +783,7 @@ static void destroy_canvas(display_surface *surface) jpeg_decoder_destroy(surface->jpeg_decoder); if (surface->shmid == -1) { - free(surface->data); + g_free(surface->data); } #ifdef HAVE_SYS_SHM_H else { @@ -1039,7 +1039,7 @@ static void display_handle_stream_create(SpiceChannel *channel, SpiceMsgIn *in) memset(c->streams + n, 0, (c->nstreams - n) * sizeof(c->streams[0])); } g_return_if_fail(c->streams[op->id] == NULL); - c->streams[op->id] = spice_new0(display_stream, 1); + c->streams[op->id] = g_new0(display_stream, 1); st = c->streams[op->id]; st->msg_create = in; @@ -1539,7 +1539,7 @@ static void destroy_stream(SpiceChannel *channel, int id) g_queue_free(st->msgq); if (st->timeout != 0) g_source_remove(st->timeout); - free(st); + g_free(st); c->streams[id] = NULL; } @@ -1551,7 +1551,7 @@ static void clear_streams(SpiceChannel *channel) for (i = 0; i < c->nstreams; i++) { destroy_stream(channel, i); } - free(c->streams); + g_free(c->streams); c->streams = NULL; c->nstreams = 0; } diff --git a/gtk/channel-main.c b/gtk/channel-main.c index 43d8e6d..84e6142 100644 --- a/gtk/channel-main.c +++ b/gtk/channel-main.c @@ -1150,7 +1150,7 @@ gboolean spice_main_send_monitor_config(SpiceMainChannel *channel) } size = sizeof(VDAgentMonitorsConfig) + sizeof(VDAgentMonConfig) * monitors; - mon = spice_malloc0(size); + mon = g_malloc0(size); mon->num_of_monitors = monitors; if (c->disable_display_position == FALSE || @@ -1181,7 +1181,7 @@ gboolean spice_main_send_monitor_config(SpiceMainChannel *channel) monitors_align(mon->monitors, mon->num_of_monitors); agent_msg_queue(channel, VD_AGENT_MONITORS_CONFIG, size, mon); - free(mon); + g_free(mon); spice_channel_wakeup(SPICE_CHANNEL(channel), FALSE); if (c->timer_id != 0) { @@ -1233,7 +1233,7 @@ static void agent_announce_caps(SpiceMainChannel *channel) return; size = sizeof(VDAgentAnnounceCapabilities) + VD_AGENT_CAPS_BYTES; - caps = spice_malloc0(size); + caps = g_malloc0(size); if (!c->agent_caps_received) caps->request = 1; VD_AGENT_SET_CAPABILITY(caps->caps, VD_AGENT_CAP_MOUSE_STATE); @@ -1244,7 +1244,7 @@ static void agent_announce_caps(SpiceMainChannel *channel) VD_AGENT_SET_CAPABILITY(caps->caps, VD_AGENT_CAP_CLIPBOARD_SELECTION); agent_msg_queue(channel, VD_AGENT_ANNOUNCE_CAPABILITIES, size, caps); - free(caps); + g_free(caps); } /* any context: the message is not flushed immediately, @@ -1978,7 +1978,7 @@ static void main_handle_agent_data_msg(SpiceChannel* channel, int* msg_size, guc SPICE_DEBUG("agent msg start: msg_size=%d, protocol=%d, type=%d", c->agent_msg.size, c->agent_msg.protocol, c->agent_msg.type); g_return_if_fail(c->agent_msg_data == NULL); - c->agent_msg_data = g_malloc(c->agent_msg.size); + c->agent_msg_data = g_malloc0(c->agent_msg.size); } } @@ -2839,7 +2839,7 @@ static void file_xfer_send_start_msg_async(SpiceMainChannel *channel, SpiceFileXferTask *task; static uint32_t xfer_id; /* Used to identify task id */ - task = spice_malloc0(sizeof(SpiceFileXferTask)); + task = g_malloc0(sizeof(SpiceFileXferTask)); task->id = ++xfer_id; task->channel = g_object_ref(channel); task->file = g_object_ref(file); diff --git a/gtk/channel-record.c b/gtk/channel-record.c index e1f3ec7..32b8faa 100644 --- a/gtk/channel-record.c +++ b/gtk/channel-record.c @@ -449,7 +449,7 @@ static void record_handle_start(SpiceChannel *channel, SpiceMsgIn *in) c->frame_bytes = FRAME_SIZE * 16 * start->channels / 8; g_free(c->last_frame); - c->last_frame = g_malloc(c->frame_bytes); + c->last_frame = g_malloc0(c->frame_bytes); c->last_frame_current = 0; switch (c->mode) { diff --git a/gtk/controller/test.c b/gtk/controller/test.c index 289ffb8..5093aa3 100644 --- a/gtk/controller/test.c +++ b/gtk/controller/test.c @@ -94,13 +94,13 @@ void send_value (uint32_t id, uint32_t value) void send_data (uint32_t id, uint8_t* data, size_t data_size) { size_t size = sizeof (ControllerData) + data_size; - ControllerData* msg = (ControllerData*)malloc (size); + ControllerData* msg = (ControllerData*)g_malloc0 (size); msg->base.id = id; msg->base.size = (uint32_t)size; memcpy (msg->data, data, data_size); write_to_pipe (msg, size); - free (msg); + g_free (msg); } ssize_t read_from_pipe (void* data, size_t size) diff --git a/gtk/decode-glz.c b/gtk/decode-glz.c index e2626ef..5287389 100644 --- a/gtk/decode-glz.c +++ b/gtk/decode-glz.c @@ -50,7 +50,7 @@ static struct glz_image *glz_image_new(struct glz_image_hdr *hdr, g_return_val_if_fail(type == LZ_IMAGE_TYPE_RGB32 || type == LZ_IMAGE_TYPE_RGBA, NULL); - img = spice_new0(struct glz_image, 1); + img = g_new0(struct glz_image, 1); img->hdr = *hdr; img->surface = alloc_lz_image_surface (opaque, type == LZ_IMAGE_TYPE_RGBA ? PIXMAN_a8r8g8b8 : PIXMAN_x8r8g8b8, @@ -92,7 +92,7 @@ static void glz_decoder_window_resize(SpiceGlzDecoderWindow *w) SPICE_DEBUG("%s: array resize %d -> %d", __FUNCTION__, w->nimages, w->nimages * 2); - new_images = spice_new0(struct glz_image*, w->nimages * 2); + new_images = g_new0(struct glz_image*, w->nimages * 2); for (i = 0; i < w->nimages; i++) { if (w->images[i] == NULL) { /* @@ -438,13 +438,13 @@ void glz_decoder_window_clear(SpiceGlzDecoderWindow *w) w->nimages = 16; g_free(w->images); - w->images = spice_new0(struct glz_image*, w->nimages); + w->images = g_new0(struct glz_image*, w->nimages); w->tail_gap = 0; } SpiceGlzDecoderWindow *glz_decoder_window_new(void) { - SpiceGlzDecoderWindow *w = spice_new0(SpiceGlzDecoderWindow, 1); + SpiceGlzDecoderWindow *w = g_new0(SpiceGlzDecoderWindow, 1); glz_decoder_window_clear(w); return w; } @@ -461,7 +461,7 @@ void glz_decoder_window_destroy(SpiceGlzDecoderWindow *w) SpiceGlzDecoder *glz_decoder_new(SpiceGlzDecoderWindow *w) { - GlibGlzDecoder *d = spice_new0(GlibGlzDecoder, 1); + GlibGlzDecoder *d = g_new0(GlibGlzDecoder, 1); d->base.ops = &glz_decoder_ops; d->window = w; return &d->base; diff --git a/gtk/decode-jpeg.c b/gtk/decode-jpeg.c index 85976d0..b86b90c 100644 --- a/gtk/decode-jpeg.c +++ b/gtk/decode-jpeg.c @@ -163,7 +163,7 @@ static void jpeg_decoder_term_source (j_decompress_ptr cinfo) SpiceJpegDecoder *jpeg_decoder_new(void) { - GlibJpegDecoder *d = spice_new0(GlibJpegDecoder, 1); + GlibJpegDecoder *d = g_new0(GlibJpegDecoder, 1); d->_cinfo.err = jpeg_std_error(&d->_jerr); jpeg_create_decompress(&d->_cinfo); diff --git a/gtk/decode-zlib.c b/gtk/decode-zlib.c index a692020..4c56936 100644 --- a/gtk/decode-zlib.c +++ b/gtk/decode-zlib.c @@ -55,7 +55,7 @@ static SpiceZlibDecoderOps zlib_decoder_ops = { SpiceZlibDecoder *zlib_decoder_new(void) { - GlibZlibDecoder *d = spice_new0(GlibZlibDecoder, 1); + GlibZlibDecoder *d = g_new0(GlibZlibDecoder, 1); int z_ret; d->_z_strm.zalloc = Z_NULL; diff --git a/gtk/spice-channel.c b/gtk/spice-channel.c index 1fa42c0..951da65 100644 --- a/gtk/spice-channel.c +++ b/gtk/spice-channel.c @@ -507,7 +507,7 @@ void spice_msg_in_unref(SpiceMsgIn *in) if (in->parent) { spice_msg_in_unref(in->parent); } else { - free(in->data); + g_free(in->data); } g_slice_free(SpiceMsgIn, in); } @@ -875,7 +875,7 @@ static void spice_channel_write_msg(SpiceChannel *channel, SpiceMsgOut *out) spice_channel_write(channel, data, len); if (free_data) - free(data); + g_free(data); spice_msg_out_unref(out); } @@ -1145,7 +1145,7 @@ static void spice_channel_send_link(SpiceChannel *channel) c->link_hdr.size += (c->link_msg.num_common_caps + c->link_msg.num_channel_caps) * sizeof(uint32_t); - buffer = spice_malloc(sizeof(c->link_hdr) + c->link_hdr.size); + buffer = g_malloc0(sizeof(c->link_hdr) + c->link_hdr.size); p = buffer; memcpy(p, &c->link_hdr, sizeof(c->link_hdr)); p += sizeof(c->link_hdr); @@ -1165,7 +1165,7 @@ static void spice_channel_send_link(SpiceChannel *channel) c->link_msg.num_common_caps, c->link_msg.num_channel_caps); spice_channel_write(channel, buffer, p - buffer); - free(buffer); + g_free(buffer); } /* coroutine context */ @@ -1193,7 +1193,7 @@ static gboolean spice_channel_recv_link_hdr(SpiceChannel *channel, gboolean *swi goto error; } - c->peer_msg = spice_malloc(c->peer_hdr.size); + c->peer_msg = g_malloc0(c->peer_hdr.size); if (c->peer_msg == NULL) { g_warning("invalid peer header size: %u", c->peer_hdr.size); goto error; @@ -1429,7 +1429,7 @@ static gboolean spice_channel_perform_auth_sasl(SpiceChannel *channel) goto error; } - mechlist = g_malloc(len + 1); + mechlist = g_malloc0(len + 1); spice_channel_read(channel, mechlist, len); mechlist[len] = '\0'; if (c->has_error) { @@ -1500,7 +1500,7 @@ restart: /* NB, distinction of NULL vs "" is *critical* in SASL */ if (len > 0) { - serverin = g_malloc(len); + serverin = g_malloc0(len); spice_channel_read(channel, serverin, len); serverin[len - 1] = '\0'; len--; @@ -1582,7 +1582,7 @@ restart: /* NB, distinction of NULL vs "" is *critical* in SASL */ if (len) { - serverin = g_malloc(len); + serverin = g_malloc0(len); spice_channel_read(channel, serverin, len); serverin[len - 1] = '\0'; len--; @@ -1782,7 +1782,7 @@ void spice_channel_recv_msg(SpiceChannel *channel, /* FIXME: do not allow others to take ref on in, and use realloc here? * this would avoid malloc/free on each message? */ - in->data = spice_malloc(msg_size); + in->data = g_malloc0(msg_size); spice_channel_read(channel, in->data, msg_size); if (c->has_error) goto end; @@ -2510,7 +2510,7 @@ static void channel_reset(SpiceChannel *channel, gboolean migrating) } c->fd = -1; - free(c->peer_msg); + g_free(c->peer_msg); c->peer_msg = NULL; c->peer_pos = 0; diff --git a/gtk/spice-client-gtk.override b/gtk/spice-client-gtk.override index 31e4f9e..41aeee3 100644 --- a/gtk/spice-client-gtk.override +++ b/gtk/spice-client-gtk.override @@ -39,21 +39,22 @@ _wrap_spice_display_send_keys(PyGObject *self, return NULL; len = PyList_Size(keyList); - keys = malloc(sizeof(guint)*len); + keys = g_malloc0(sizeof(guint)*len); + for (i = 0 ; i < len ; i++) { PyObject *val; char *sym; val = PyList_GetItem(keyList, i); sym = PyString_AsString(val); if (!sym) { - free(keys); + g_free(keys); return NULL; } keys[i] = gdk_keyval_from_name(sym); } spice_display_send_keys(SPICE_DISPLAY(self->obj), keys, len, kind); - free(keys); + g_free(keys); Py_INCREF(Py_None); return Py_None; diff --git a/gtk/spice-session.c b/gtk/spice-session.c index 9e6c154..6146270 100644 --- a/gtk/spice-session.c +++ b/gtk/spice-session.c @@ -1864,7 +1864,7 @@ void spice_session_channel_new(SpiceSession *session, SpiceChannel *channel) g_return_if_fail(s != NULL); g_return_if_fail(channel != NULL); - item = spice_new0(struct channel, 1); + item = g_new0(struct channel, 1); item->channel = channel; ring_add(&s->channels, &item->link); diff --git a/gtk/spice-widget.c b/gtk/spice-widget.c index 233f36c..7019b03 100644 --- a/gtk/spice-widget.c +++ b/gtk/spice-widget.c @@ -2581,7 +2581,7 @@ GdkPixbuf *spice_display_get_pixbuf(SpiceDisplay *display) /* TODO: ensure d->data has been exposed? */ g_return_val_if_fail(d->data != NULL, NULL); - data = g_malloc(d->area.width * d->area.height * 3); + data = g_malloc0(d->area.width * d->area.height * 3); src = d->data; dest = data; diff --git a/gtk/usb-device-manager.c b/gtk/usb-device-manager.c index 1051d10..5013b6c 100644 --- a/gtk/usb-device-manager.c +++ b/gtk/usb-device-manager.c @@ -1017,7 +1017,7 @@ static int spice_usb_device_manager_hotplug_cb(libusb_context *ctx, void *user_data) { SpiceUsbDeviceManager *self = SPICE_USB_DEVICE_MANAGER(user_data); - struct hotplug_idle_cb_args *args = g_malloc(sizeof(*args)); + struct hotplug_idle_cb_args *args = g_malloc0(sizeof(*args)); args->self = g_object_ref(self); args->device = libusb_ref_device(device); -- 1.9.3