Re: [PATCH] Fix previously adjusted segfault patch

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

 



On Thu, Jul 14, 2016 at 09:28:34PM +0200, Tobias Stoeckmann wrote:
> Casting the value to be checked to size_t renders the check useless.
> If st_size is SIZE_MAX+1, it will be truncated to 0 and the check
> succeeds. In fact, this check can never be false because every value
> stored in a size_t is smaller or equal to SIZE_MAX.
> 
> I think this adjustment was meant to fix a compiler warning for 64 bit

yes

> systems for which sizeof(off_t) is sizeof(size_t), but the signedness
> differs. One possibility is to do a "binary and" operation with the
> value SIZE_MAX. If the original value and the and-operated value differ,
> it means that a higher bit was set and therefore the file was too large.
> 
> Signed-off-by: Tobias Stoeckmann <tobias@xxxxxxxxxxxxxx>
> ---
>  text-utils/tailf.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/text-utils/tailf.c b/text-utils/tailf.c
> index 6219aa2..5ddbee9 100644
> --- a/text-utils/tailf.c
> +++ b/text-utils/tailf.c
> @@ -284,7 +284,7 @@ int main(int argc, char **argv)
>  		errx(EXIT_FAILURE, _("%s: is not a file"), filename);
>  
>  	/* mmap is based on size_t */
> -	if (st.st_size && (size_t) st.st_size <= SIZE_MAX)
> +	if (st.st_size && st.st_size == (st.st_size & SIZE_MAX))

but compiler still don't like it, because "& SIZE_MAX" result is
unsigned number.

text-utils/tailf.c: In function ‘main’:
text-utils/tailf.c:287:31: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]

I guess you need to cast:

    if (st.st_size > 0 && st.st_size == (off_t) (st.st_size & SIZE_MAX))


Maybe it would be nice to have a macros for this purpose in include/c.h,
something like:

#define is_between(x, mi, ma)  (x >= mi && x == (__typeof__(x)) (x & ma))

#define is_sizet_convertible(x)     is_between(x, 0, SIZE_MAX)
#define is_uint32_convertible(x)    is_between(x, 0, UINT32_MAX)
...

    if (st.st_size && is_sizet_convertible(st.st_size))
        tailf(filename, lines, &st);


Correct? or maybe I need a coffee? ;-)

    Karel

-- 
 Karel Zak  <kzak@xxxxxxxxxx>
 http://karelzak.blogspot.com
--
To unsubscribe from this list: send the line "unsubscribe util-linux" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html



[Index of Archives]     [Netdev]     [Ethernet Bridging]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Admin]     [Samba]

  Powered by Linux