[sheepdog] [PATCH] lib: insert assert() to sd_strerror() for detecting invalid index

MORITA Kazutaka morita.kazutaka at lab.ntt.co.jp
Thu Aug 22 08:21:02 CEST 2013


At Thu, 22 Aug 2013 15:06:35 +0900,
Hitoshi Mitake wrote:
> 
> Commit a4d01de did wrong treatment of request data structure. And
> it caused segfault in sd_strerror() because of invalid index of the desc
> array. This patch insert assert() for detecting such a bug.
> 
> Signed-off-by: Hitoshi Mitake <mitake.hitoshi at lab.ntt.co.jp>
> ---
>  include/sheep.h |    2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/include/sheep.h b/include/sheep.h
> index 1d146bd..d708122 100644
> --- a/include/sheep.h
> +++ b/include/sheep.h
> @@ -242,6 +242,8 @@ static inline const char *sd_strerror(int err)
>  		[SD_RES_CLUSTER_ERROR] = "Cluster driver error",
>  	};
>  
> +	assert(0 <= err && err < ARRAY_SIZE(descs));

Returning "Invalid error code" looks better?

> +
>  	if (descs[err] == NULL) {
>  		static __thread char msg[32];
>  		snprintf(msg, sizeof(msg), "Invalid error code %x", err);

E.g.

	if(err < 0 || ARRAY_SIZE(descs) <= err || descs[err] == NULL)
		static __thread char msg[32];
		snprintf(msg, sizeof(msg), "Invalid error code %x", err);

Thanks,

Kazutaka



More information about the sheepdog mailing list