valgrind在ioctl()行上给了我以下错误:
我不确定如何避免这种错误。


  == 2764 ==错误摘要:来自1个上下文的1个错误(禁止显示:0至0)
  == 2764 ==
  == 2764 ==上下文1中的1个错误:
  == 2764 ==系统调用参数ioctl(I2C_RDWR).msgs指向未初始化的字节
  == 2764 == at 0x4E08EFEC:ioctl(syscall-template.S:81)
  == 2764 ==通过0x871F:imxSend_i2cMsg(imx6qi2c_wrapper.c:54)
  == 2764 ==通过0x87CB:imxSend_i2cByte(imx6qi2c_wrapper.c:84)
  == 2764 ==通过0x86D3:main(imx6qi2c_test.c:30)
  == 2764 ==地址0x7db99b82在线程1的堆栈上
  == 2764 ==未初始化的值是由堆栈分配创建的
  == 2764 ==在0x875C:imxSend_i2cByte(imx6qi2c_wrapper.c:66)


imx6qi2c_wrapper.c中的代码如下:

int imxSend_i2cMsg(const int i2c_fd,
                   struct i2c_msg *i2cMsgArray,
                   const unsigned int arraySize){
    int ret=0;
    struct i2c_rdwr_ioctl_data ioctl_pack;

    ioctl_pack.nmsgs=arraySize;
    ioctl_pack.msgs=i2cMsgArray;

    ret=ioctl(i2c_fd,I2C_RDWR,&ioctl_pack);

    return ret;
}


和“ i2cMsgArray”在另一个函数中本地声明。

int imxSend_i2cByte(const int i2c_fd,
                      const unsigned char i2cAddress,
                      const unsigned char devRegister,
                      const unsigned char value){
    struct i2c_msg i2cmsg[I2CMSGS_TOTAL_WR];
    int ret=0,i=0;

    for (i=0;i<I2CMSGS_TOTAL_WR;i++){
        i2cmsg[i].buf=malloc(2*sizeof(char));
    }

    i2cmsg[0].addr=i2cAddress;
    i2cmsg[0].flags=I2C_M_WR;
    i2cmsg[0].len=2;
    i2cmsg[0].buf[0]=devRegister;
    i2cmsg[0].buf[1]=value;

    ret=imxSend_i2cMsg(i2c_fd,&i2cmsg[0],I2CMSGS_TOTAL_WR);
    if (ret<0){
        printf("i2cmsg failed.errno is %d, %s\n",errno,strerror(errno));
    }

    for (i=0;i<I2CMSGS_TOTAL_WR;i++){
        free(i2cmsg[i].buf);
    }
    return ret;
}


未初始化的字节在哪里?

最佳答案

好的,只是为了完成,我在此处复制作为注释给出的答案(但从未被知道答案的用户发布为答案)。

memset to 0 on i2cmsg will solve your problem

关于c - 发送i2c消息时,ioctl()调用中的valgrind错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17859320/

10-12 18:05