Re: [spice-gtk][PATCH] Prefer using g_malloc0()/g_free()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



ack

----- Original Message -----
> 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/channel-webdav.c          |  4 ++--
>  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 +-
>  15 files changed, 43 insertions(+), 44 deletions(-)
> 
> diff --git a/gtk/channel-cursor.c b/gtk/channel-cursor.c
> index a7d7153..ae788dc 100644
> --- a/gtk/channel-cursor.c
> +++ b/gtk/channel-cursor.c
> @@ -323,7 +323,7 @@ static display_cursor *set_cursor(SpiceChannel *channel,
> SpiceCursor *scursor)
>      g_return_val_if_fail(scursor->data_size != 0, NULL);
>  
>      size = 4u * 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 2ad653e..95d5b33 100644
> --- a/gtk/channel-display-mjpeg.c
> +++ b/gtk/channel-display-mjpeg.c
> @@ -73,11 +73,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);
> @@ -153,6 +151,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 6265334..6fa97aa 100644
> --- a/gtk/channel-display.c
> +++ b/gtk/channel-display.c
> @@ -730,7 +730,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 {
> @@ -986,7 +986,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;
> @@ -1486,7 +1486,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;
>  }
>  
> @@ -1498,7 +1498,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 352499c..7a299a4 100644
> --- a/gtk/channel-main.c
> +++ b/gtk/channel-main.c
> @@ -1050,7 +1050,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 ||
> @@ -1081,7 +1081,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) {
> @@ -1133,7 +1133,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);
> @@ -1144,7 +1144,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,
> @@ -1879,7 +1879,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);
>          }
>      }
>  
> @@ -2740,7 +2740,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 20af3de..946d66f 100644
> --- a/gtk/channel-record.c
> +++ b/gtk/channel-record.c
> @@ -418,7 +418,7 @@ static void record_handle_start(SpiceChannel *channel,
> SpiceMsgIn *in)
>  
>      g_free(c->last_frame);
>      c->frame_bytes = frame_size * 16 * start->channels / 8;
> -    c->last_frame = g_malloc(c->frame_bytes);
> +    c->last_frame = g_malloc0(c->frame_bytes);
>      c->last_frame_current = 0;
>  
>      g_coroutine_signal_emit(channel, signals[SPICE_RECORD_START], 0,
> diff --git a/gtk/channel-webdav.c b/gtk/channel-webdav.c
> index b8b0993..0529b59 100644
> --- a/gtk/channel-webdav.c
> +++ b/gtk/channel-webdav.c
> @@ -388,7 +388,7 @@ static void client_connected(GObject *source_object,
>      client->self = self;
>      client->conn = conn;
>      client->mux.id = GINT64_TO_LE(client->id);
> -    client->mux.buf = g_malloc(MAX_MUX_SIZE);
> +    client->mux.buf = g_malloc0(MAX_MUX_SIZE);
>      client->cancellable = g_cancellable_new();
>  
>      output =
>      g_buffered_output_stream_new(g_io_stream_get_output_stream(G_IO_STREAM(conn)));
> @@ -559,7 +559,7 @@ static void spice_webdav_channel_init(SpiceWebdavChannel
> *channel)
>      c->cancellable = g_cancellable_new();
>      c->clients = g_hash_table_new_full(g_int64_hash, g_int64_equal,
>                                         NULL, client_remove_unref);
> -    c->demux.buf = g_malloc(MAX_MUX_SIZE);
> +    c->demux.buf = g_malloc0(MAX_MUX_SIZE);
>  
>      GOutputStream *ostream =
>      g_io_stream_get_output_stream(G_IO_STREAM(c->stream));
>      c->queue = output_queue_new(ostream);
> diff --git a/gtk/controller/test.c b/gtk/controller/test.c
> index 851f237..f426669 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 b09de00..34a7185 100644
> --- a/gtk/decode-glz.c
> +++ b/gtk/decode-glz.c
> @@ -52,7 +52,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,
> @@ -94,7 +94,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) {
>              /*
> @@ -440,13 +440,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;
>  }
> @@ -463,7 +463,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 cce7b53..db54a31 100644
> --- a/gtk/decode-jpeg.c
> +++ b/gtk/decode-jpeg.c
> @@ -165,7 +165,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 966fc16..a5325c0 100644
> --- a/gtk/decode-zlib.c
> +++ b/gtk/decode-zlib.c
> @@ -57,7 +57,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 1a4a5bf..521f10a 100644
> --- a/gtk/spice-channel.c
> +++ b/gtk/spice-channel.c
> @@ -516,7 +516,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);
>  }
> @@ -866,7 +866,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);
>  }
> @@ -1143,7 +1143,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);
> @@ -1163,7 +1163,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 */
> @@ -1191,7 +1191,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;
> @@ -1427,7 +1427,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) {
> @@ -1498,7 +1498,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--;
> @@ -1580,7 +1580,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--;
> @@ -1780,7 +1780,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;
> @@ -2543,7 +2543,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 7aef787..9c7316e 100644
> --- a/gtk/spice-session.c
> +++ b/gtk/spice-session.c
> @@ -1884,7 +1884,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 9b835f0..154c43e 100644
> --- a/gtk/spice-widget.c
> +++ b/gtk/spice-widget.c
> @@ -2559,7 +2559,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
> 
> _______________________________________________
> Spice-devel mailing list
> Spice-devel@xxxxxxxxxxxxxxxxxxxxx
> http://lists.freedesktop.org/mailman/listinfo/spice-devel
> 
_______________________________________________
Spice-devel mailing list
Spice-devel@xxxxxxxxxxxxxxxxxxxxx
http://lists.freedesktop.org/mailman/listinfo/spice-devel




[Index of Archives]     [Linux ARM Kernel]     [Linux ARM]     [Linux Omap]     [Fedora ARM]     [IETF Annouce]     [Security]     [Bugtraq]     [Linux]     [Linux OMAP]     [Linux MIPS]     [ECOS]     [Asterisk Internet PBX]     [Linux API]     [Monitors]