Re: [PATCH wayland] server: fix conditions for fds in wl_socket_destroy

2014-08-07 Thread Pekka Paalanen
On Thu, 7 Aug 2014 09:56:59 -0400
"Jasper St. Pierre"  wrote:

> Reviewed-by: Jasper St. Pierre 

Thanks, pushed.
- pq


> On Thu, Aug 7, 2014 at 9:51 AM, Pekka Paalanen  wrote:
> 
> > From: Pekka Paalanen 
> >
> > 0 is also a valid fd, and needs to be closed.
> >
> > On error we set fd to -1. We need to also initialize fds to -1, so we do
> > not accidentally close stdout on error.
> >
> > While fixing this, also remove one use-before-NULL-check.
> >
> > Based on the patch by Marek.
> >
> > Cc: Marek Chalupa 
> > Signed-off-by: Pekka Paalanen 
> > ---
> >  src/wayland-server.c | 27 ---
> >  1 file changed, 20 insertions(+), 7 deletions(-)
> >
> > diff --git a/src/wayland-server.c b/src/wayland-server.c
> > index 3c162d4..3bd8e68 100644
> > --- a/src/wayland-server.c
> > +++ b/src/wayland-server.c
> > @@ -848,16 +848,32 @@ wl_socket_destroy(struct wl_socket *s)
> > wl_event_source_remove(s->source);
> > if (s->addr.sun_path[0])
> > unlink(s->addr.sun_path);
> > -   if (s->fd)
> > +   if (s->fd >= 0)
> > close(s->fd);
> > if (s->lock_addr[0])
> > unlink(s->lock_addr);
> > -   if (s->fd_lock)
> > +   if (s->fd_lock >= 0)
> > close(s->fd_lock);
> >
> > free(s);
> >  }
> >
> > +static struct wl_socket *
> > +wl_socket_alloc(void)
> > +{
> > +   struct wl_socket *s;
> > +
> > +   s = malloc(sizeof *s);
> > +   if (!s)
> > +   return NULL;
> > +
> > +   memset(s, 0, sizeof *s);
> > +   s->fd = -1;
> > +   s->fd_lock = -1;
> > +
> > +   return s;
> > +}
> > +
> >  WL_EXPORT void
> >  wl_display_destroy(struct wl_display *display)
> >  {
> > @@ -1149,12 +1165,10 @@ wl_display_add_socket_auto(struct wl_display
> > *display)
> >  * you need more than this, use the explicit add_socket API. */
> > const int MAX_DISPLAYNO = 32;
> >
> > -   s = malloc(sizeof *s);
> > +   s = wl_socket_alloc();
> > if (s == NULL)
> > return NULL;
> >
> > -   memset(s, 0, sizeof *s);
> > -
> > do {
> > snprintf(display_name, sizeof display_name, "wayland-%d",
> > displayno);
> > if (wl_socket_init_for_display_name(s, display_name) < 0) {
> > @@ -1184,8 +1198,7 @@ wl_display_add_socket(struct wl_display *display,
> > const char *name)
> >  {
> > struct wl_socket *s;
> >
> > -   s = malloc(sizeof *s);
> > -   memset(s, 0, sizeof *s);
> > +   s = wl_socket_alloc();
> > if (s == NULL)
> > return -1;
> >
> > --
> > 1.8.5.5
> >
> > ___
> > wayland-devel mailing list
> > wayland-devel@lists.freedesktop.org
> > http://lists.freedesktop.org/mailman/listinfo/wayland-devel
> >
> 
> 
> 

___
wayland-devel mailing list
wayland-devel@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/wayland-devel


Re: [PATCH wayland] server: fix conditions for fds in wl_socket_destroy

2014-08-07 Thread Jasper St. Pierre
Reviewed-by: Jasper St. Pierre 


On Thu, Aug 7, 2014 at 9:51 AM, Pekka Paalanen  wrote:

> From: Pekka Paalanen 
>
> 0 is also a valid fd, and needs to be closed.
>
> On error we set fd to -1. We need to also initialize fds to -1, so we do
> not accidentally close stdout on error.
>
> While fixing this, also remove one use-before-NULL-check.
>
> Based on the patch by Marek.
>
> Cc: Marek Chalupa 
> Signed-off-by: Pekka Paalanen 
> ---
>  src/wayland-server.c | 27 ---
>  1 file changed, 20 insertions(+), 7 deletions(-)
>
> diff --git a/src/wayland-server.c b/src/wayland-server.c
> index 3c162d4..3bd8e68 100644
> --- a/src/wayland-server.c
> +++ b/src/wayland-server.c
> @@ -848,16 +848,32 @@ wl_socket_destroy(struct wl_socket *s)
> wl_event_source_remove(s->source);
> if (s->addr.sun_path[0])
> unlink(s->addr.sun_path);
> -   if (s->fd)
> +   if (s->fd >= 0)
> close(s->fd);
> if (s->lock_addr[0])
> unlink(s->lock_addr);
> -   if (s->fd_lock)
> +   if (s->fd_lock >= 0)
> close(s->fd_lock);
>
> free(s);
>  }
>
> +static struct wl_socket *
> +wl_socket_alloc(void)
> +{
> +   struct wl_socket *s;
> +
> +   s = malloc(sizeof *s);
> +   if (!s)
> +   return NULL;
> +
> +   memset(s, 0, sizeof *s);
> +   s->fd = -1;
> +   s->fd_lock = -1;
> +
> +   return s;
> +}
> +
>  WL_EXPORT void
>  wl_display_destroy(struct wl_display *display)
>  {
> @@ -1149,12 +1165,10 @@ wl_display_add_socket_auto(struct wl_display
> *display)
>  * you need more than this, use the explicit add_socket API. */
> const int MAX_DISPLAYNO = 32;
>
> -   s = malloc(sizeof *s);
> +   s = wl_socket_alloc();
> if (s == NULL)
> return NULL;
>
> -   memset(s, 0, sizeof *s);
> -
> do {
> snprintf(display_name, sizeof display_name, "wayland-%d",
> displayno);
> if (wl_socket_init_for_display_name(s, display_name) < 0) {
> @@ -1184,8 +1198,7 @@ wl_display_add_socket(struct wl_display *display,
> const char *name)
>  {
> struct wl_socket *s;
>
> -   s = malloc(sizeof *s);
> -   memset(s, 0, sizeof *s);
> +   s = wl_socket_alloc();
> if (s == NULL)
> return -1;
>
> --
> 1.8.5.5
>
> ___
> wayland-devel mailing list
> wayland-devel@lists.freedesktop.org
> http://lists.freedesktop.org/mailman/listinfo/wayland-devel
>



-- 
  Jasper
___
wayland-devel mailing list
wayland-devel@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/wayland-devel