Re: [PATCH v3] media: si2168: Refactor command setup code

From: Matthias Schwarzott
Date: Sun Jul 14 2019 - 14:31:31 EST


Am 13.07.19 um 12:02 schrieb Mauro Carvalho Chehab:
> Em Sat, 13 Jul 2019 00:11:12 +0200
> Marc Gonzalez <marc.w.gonzalez@xxxxxxx> escreveu:
>
>> On 12/07/2019 19:45, Mauro Carvalho Chehab wrote:
>>
>>> Brad Love <brad@xxxxxxxxxxxxxxxx> escreveu:
>>>
>>> IMHO, using sizeof() here is a very bad idea.
>>
>> You may have a point...
>> (Though I'm not proposing a kernel API function, merely code
>> refactoring for a single file that's unlikely to change going
>> forward.)
>
> Yes, I know, but we had already some bugs due to the usage of
> sizeof() on similar macros at drivers in the past.
>
>> It's also bad form to repeat the cmd size (twice) when the compiler
>> can figure it out automatically for string literals (which is 95%
>> of the use-cases).
>>
>> I can drop the macro, and just use the helper...
>
> The helper function sounds fine.
>
>>
>> Or maybe there's a GCC extension to test that an argument is a
>> string literal...
>
> If this could be evaluated by some advanced macro logic that
> would work not only with gcc but also with clang, then a
> macro that does what you proposed could be useful.
>
> There are some ways to check the type of a macro argument, but I'm
> not sure if are there any way for it to distinguish between a
> string constant from a char * array.
>
Maybe something like this will prevent compilation if the argument is no
string literal:

#define CMD_SETUP(cmd, args, rlen) \
cmd_setup(cmd, args "", sizeof(args) - 1, rlen)

Another idea is a check like:

#define CMD_SETUP(cmd, args, rlen) \
do { \
BUILD_BUG_ON(#args[0] != "\""); \
cmd_setup(cmd, args "", sizeof(args) - 1, rlen) \
} while(0)

Regards
Matthias