Re: [PATCH v4 1/9] lib/string: introduce match_string() helper

From: Rasmus Villemoes
Date: Tue Feb 02 2016 - 14:50:14 EST


On Tue, Feb 02 2016, Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx> wrote:

> On Thu, 28 Jan 2016 15:14:17 +0200 Andy Shevchenko <andriy.shevchenko@xxxxxxxxxxxxxxx> wrote:
>
>> + * @array: array of strings
>> + * @n: number of strings in the array or -1 for NULL terminated arrays
>> + * @string: string to match with
>> + *
>> + * Return:
>> + * index of a @string in the @array if matches, or %-ENODATA otherwise.
>> + */
>> +int match_string(const char * const *array, size_t n, const char *string)
>> +{
>> + int index;
>> + const char *item;
>> +
>> + for (index = 0; index < n; index++) {
>
> So we're taking an int and comparing that with (size_t)-1, relying upon
> the compiler promoting the int to an unsigned type because size_t is
> unsigned. It works, but it isn't pretty - there wasn't really much
> point in making size have type size_t.

n has unsigned type to make it easy to pass 'infinity'; *that's* where
we rely on integer promotion.

One could make index be unsigned int (or size_t), but it won't matter,
because it will never have a value where the type promotion (nor the
implicit cast back to int if/when it's used as a return value) changes
its value.

Rasmus