cURL / Mailing Lists / curl-library / Single Mail

curl-library

Array Index size_t vs int

From: Marc Hoersken <info_at_marc-hoersken.de>
Date: Mon, 17 Sep 2012 20:54:53 +0200

Hello everyone,

I noticed that quite some warnings in the curl source code are related
to size_t/ssite_t to int conversions.
This is caused by the fact that we are mostly using int-typed indexes
to access array fields there the actual array holds a string- or
data-buffer.
Many of the functions operating on these buffers return size_t/ssize_t
values, for example strlen:
http://linux.die.net/man/3/strlen

In order to calculate array indexes on the fly we add those return
values to an int-typed index counter and then access the array with
that.
This strategy is commonly used to calculate the total packet length or
access packet fields.

Curl uses multiple ways to deal with some of those warnings. One of
them is to do type-conversion or use the helper functions provided
within warnless.h.
I think this might be the wrong way to deal with this specific
problem. As you can see in the discussion of the following
StackOverflow question, size_t/ssize_t can actually be used for array
access:
http://stackoverflow.com/questions/6004415/why-size-t-when-int-would-suffice

This means that using size_t/ssize_t instead of int would remove the
actual warning and it would make sure that the value is actually big
enough to hold the array length or index on a 64bit system. I know,
it's unlikely that most of the arrays ever grow that large, but that
way we would be safe regarding this.

What do you think about this? Is this a good alternative to the
existing strategy that should/could be used to remove the remaining
warnings and may also be used to replace the existing type
conversions?

Since I am not sure if size_t/ssite_t would be a cross-platform way to
solve this, I would like to ask for your feedback on this, too. Thanks
in advance!

Unfortunately I cannot show some example warnings or link to the
autobuilds page right now. At the moment it just shows up as an empty
page to me.

Best regards,
Marc
-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2012-09-17