I was trying to compile a kernel for my HTC phone when my compiler warned about this:
static ssize_t mipi_dsi_3d_barrier_read(struct device *dev,
struct device_attribute *attr,
char *buf)
{
return snprintf((char *)buf, sizeof(buf), "%u\n", barrier_mode);
}
with the message
warning: argument to 'sizeof' in 'snprintf' call is the same expression as the destination; did you mean to provide an explicit length? [-Wsizeof-pointer-memaccess] error, forbidden warning: mipi_novatek.c:524
I fixed a few like this already, as I understand that sizeof(buf) makes no sense, because buf is passed as an argument and therefore the compiler has no idea of the buffer size -even if you happen to pass a static buffer.
Thing is, after fixing a few like this, I am wondering if I am missing something. I downloaded the kernel from a github repository, with quite a few commits from the "original" htc kernel, and I could find these errors in both, so surely it compiled fine for others.
Am I missing or doing something wrong? I am using arm-none-eabi-gcc 4.8.2 from my ubuntu repository, instead of downloading from android.googlesource.com as advised. But regardless of the compiler, this is a bug, isn't it? Shouldn't you pass the buffer size as an extra argument?
EDIT Another such example in the same kernel...
struct msm_adsp_module *module;
...
if (!strncmp(module->name, "QCAMTASK", sizeof(module->name)))
module_irq_cnt[1]++;
else if(!strncmp(module->name, "VFETASK", sizeof(module->name)))
module_irq_cnt[2]++;
else if(!strncmp(module->name, "VIDEOENCTASK", sizeof(module->name)))
module_irq_cnt[3]++;
where
struct msm_adsp_module {
struct mutex lock;
const char *name;
...