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: Fred Wright
Subject: Re: [gpsd-dev] [PATCH] Improved resilience in gps_shm_close() in case it is called after an unsuccessful gps_shm_open()
Date: Sun, 31 Jul 2016 11:45:37 -0700 (PDT)

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]