Mailing List Archive

String length (mis)calculations in mime.c
So I noticed that mime.c reports 3 errors when being compiled:

mime.c:309: warning: comparison between signed and unsigned
mime.c:314: warning: comparison between signed and unsigned
mime.c:363: warning: comparison between signed and unsigned

Each of those three lines has some variant of this code:

vallen = snprintf(mr->value, MIME_VALUE_MAX, "%s", startptr);


if (vallen == -1 || vallen >= MIME_VALUE_MAX)

Note that vallen (and elsewhere fieldlen) are defined thusly:

size_t fieldlen, vallen;

On most systems, size_t is unsigned and snprintf() returns int. So... there's
two options to fix this. Either a) we change those to int, or b) we check only
if the return value is *greater* than the requested size to copy. In that
case, we know that there was some error because the sign bit will be flipped,
and in an unsigned interpretation, this means that some huge value will be
represented and it will be some value lager than the requested copy size.

Aaron
Re: String length (mis)calculations in mime.c [ In reply to ]
Hi,

The usage of vallen (in this case, fieldlen in others) is just wrong. We
should not assign a signed integer value to an unsigned variable.
I don't like hacking around this by checking if the return value is
greater, just because it will work because of the representation of
signed and unsigned integers.

If anybody wants to fix this for real, please do so, otherwise I'll try
to find some time to do it (not this week).

Ilja

Aaron Stone wrote:

> So I noticed that mime.c reports 3 errors when being compiled:
>
> mime.c:309: warning: comparison between signed and unsigned
> mime.c:314: warning: comparison between signed and unsigned
> mime.c:363: warning: comparison between signed and unsigned
>
> Each of those three lines has some variant of this code:
>
> vallen = snprintf(mr->value, MIME_VALUE_MAX, "%s", startptr);
>
>
> if (vallen == -1 || vallen >= MIME_VALUE_MAX)
>
> Note that vallen (and elsewhere fieldlen) are defined thusly:
>
> size_t fieldlen, vallen;
>
> On most systems, size_t is unsigned and snprintf() returns int. So... there's
> two options to fix this. Either a) we change those to int, or b) we check only
> if the return value is *greater* than the requested size to copy. In that
> case, we know that there was some error because the sign bit will be flipped,
> and in an unsigned interpretation, this means that some huge value will be
> represented and it will be some value lager than the requested copy size.
>
> Aaron
>
> _______________________________________________
> Dbmail-dev mailing list
> Dbmail-dev@dbmail.org
> http://twister.fastxs.net/mailman/listinfo/dbmail-dev

--
IC&S
Stadhouderslaan 57
3583 JD Utrecht

PGP-key:
http://www.ic-s.nl/keys/ilja.txt