gpsd-dev
[Top][All Lists]
Advanced

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

Re: [gpsd-dev] [PATCH] Improved resilience in gps_shm_close() in case it


From: Robert Norris
Subject: Re: [gpsd-dev] [PATCH] Improved resilience in gps_shm_close() in case it is called after an unsuccessful gps_shm_open()
Date: Mon, 1 Aug 2016 17:48:22 +0000


Yes, that's how I normally would write it.


I can't remember why I choose the longer form for this patch.


I'll redo it in the compact form.


--
Be Seeing You - Rob.
If at first you don't succeed,
then skydiving isn't for you.

From: gpsd-dev <gpsd-dev-bounces+address@hidden> on behalf of Fred Wright <address@hidden>
Sent: 31 July 2016 19:45:37
To: address@hidden
Subject: Re: [gpsd-dev] [PATCH] Improved resilience in gps_shm_close() in case it is called after an unsuccessful gps_shm_open()
 

On Sat, 30 Jul 2016, Robert Norris wrote:

> Check that the structure exists before trying to use a component of it.
> ---
>  libgps_shm.c | 2 ++
>  1 file changed, 2 insertions(+)
>
> diff --git a/libgps_shm.c b/libgps_shm.c
> index a0dfdde..872a089 100644
> --- a/libgps_shm.c
> +++ b/libgps_shm.c
> @@ -154,6 +154,8 @@ int gps_shm_read(struct gps_data_t *gpsdata)
>
>  void gps_shm_close(struct gps_data_t *gpsdata)
>  {
> +    if (PRIVATE(gpsdata) == NULL)
> +     return;
>      if (PRIVATE(gpsdata)->shmseg != NULL)
>        (void)shmdt((const void *)PRIVATE(gpsdata)->shmseg);
>  }
> --
> 2.8.1

That could be written more compactly as:

        if (PRIVATE(gpsdata) != NULL && PRIVATE(gpsdata)->shmseg != NULL)

or even:

        if (PRIVATE(gpsdata) && PRIVATE(gpsdata)->shmseg)

That && construct is a fairly common idiom for dereferencing potentially
NULL pointers.

Fred Wright


reply via email to

[Prev in Thread] Current Thread [Next in Thread]