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

Re: more improvements for mg



On Mon, May 16, 2005 at 01:40:40PM +0159, Han Boetes wrote:
> Han Boetes wrote:
> > Theo de Raadt wrote:
> > > snprintf does not return size_t
> >
> > sizeof returns size_t
> 
> 

hello,

imho, there is no elegant solution since size_t is unsigned 
and snprintf(3) returns a signed int. snprintf(3) is just unable 
to handle strings longer than INT_MAX (2^31 - 1 on 32bit arch). 
However, once we are sure that ret >= 0, it seems to me reasonable 
to compare 'ret' to sizeof(foo), example:

	int		 ret;
	char             foo[100];

	...
	ret = snprintf(foo, sizeof(foo), "%s", input);
	if (ret < 0 || (size_t)ret >= sizeof(foo))


-1 means that snprintf failed and negative number of chars is meaningless.
This works if sizeof(foo) > INT_MAX because res is converted to size_t. 
Have i missed something?

kind regards,

-- 
Alexandre



Visit your host, monkey.org