[prev in list] [next in list] [prev in thread] [next in thread] 

List:       gpsd-dev
Subject:    Re: [gpsd-dev] [PATCH] Improved resilience in gps_shm_close() in case it is called after an unsucces
From:       Fred Wright <fw () fwright ! net>
Date:       2016-07-31 18:45:37
Message-ID: Pine.LNX.4.58L1X.1607311141230.11879 () bolt ! sonic ! net
[Download RAW message or body]


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

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic