Another lesson on compare warnings please ...

Joel Sherrill joel at rtems.org
Wed Nov 25 14:28:38 UTC 2015


On Nov 25, 2015 2:54 AM, "Hal Murray" <hmurray at megapathdsl.net> wrote:
>
>
> In ntpd/refclock_palisade.h
>         short           rpt_cnt;        /* TSIP packet length so far */
>         char            rpt_buf[BMAX];  /* packet assembly buffer */
>
> From ntpd/refclock_palisade.c
>                 else if (up->rpt_cnt > BMAX)
>
> That code is correct, but I would have used sizeof(rpt_buf) rather than
BMAX.
>  With a little bit of poking around, you can figure out that rpt_buf is
the
> buffer.  With BMAX, you have to go look in another file to see that it's
the
> size of the buffer.
>
> But that gets:
> ../ntpd/refclock_palisade.c:1415:24: warning: comparison between signed
and unsigned integer expressions [-Wsign-compare]
>    else if (up->rpt_cnt > sizeof(up->rpt_buf)) {
>
> Both BMAX and sizeof work if I change the short to u_int.
>
> So what is the type of sizeof, and what is the type of a literal
constant?  I expected them to be the same.

Use size_t.

> What is the best way to do this sort of thing?
>
> Do I get a segfault if I botch something and get an underflow on a
unsigned int and then use it as an index with a 64 bit pointer?
>
>
> --
> These are my opinions.  I hate spam.
>
>
>
> _______________________________________________
> devel mailing list
> devel at ntpsec.org
> http://lists.ntpsec.org/mailman/listinfo/devel
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ntpsec.org/pipermail/devel/attachments/20151125/6ac007d4/attachment.html>


More information about the devel mailing list