Re: [PATCH v3 2/2] lib/test-string_helpers.c: add string_get_size() tests

From: Andy Shevchenko
Date: Tue Sep 15 2015 - 08:19:27 EST


On Tue, 2015-09-15 at 14:10 +0200, Vitaly Kuznetsov wrote:
> Andy Shevchenko <andriy.shevchenko@xxxxxxxxxxxxxxx> writes:
>
> > I prefer to put '\0' at the position after we expected have an
> > actual
> > '\0'. In this case we always be NULL terminated. I did this for
> > hexdump
> > test cases.
>
> Just to check I got your suggestions right:
>
> ...
> + if (!memcmp(buf, exp_result, strnlen(exp_result, sizeof(buf)
> - 1) + 1))
> + return;
> +
> + /* NULL terminate buf right after the expected '\0' */
> + buf[strnlen(exp_result, sizeof(buf) - 2) + 1] = '\0';
> ...
>
> Alternatively, we could have avoided strnlen() by asserting
> strlen(exp_result) < sizeof(buf) - 1 at the very beginning.
>

Just

buf[sizeof(buf) - 1] = '\0';

should be enough after you called the string_get_size().

And minimize the buffer to something like 16 (whatever is the biggest
possible length + '\0' + some space for the wrong algorithm aligned to
let's say 4.

--
Andy Shevchenko <andriy.shevchenko@xxxxxxxxxxxxxxx>
Intel Finland Oy
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/