发送i2c消息时,ioctl()调用中的valgrind错误

时间:2013-07-25 13:24:09

标签: c valgrind ioctl initialization

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

  

== 2764 ==错误摘要:来自1个上下文的1个错误(被抑制:0从0开始)
  == == 2764
  == 2764 ==上下文1中的错误1:
  == 2764 == Syscall param ioctl(I2C_RDWR).msgs指向未初始化的字节
  == 2764 ==在0x4E08EFEC:ioctl(syscall-template.S:81)
  == 2764 == by 0x871F:imxSend_i2cMsg(imx6qi2c_wrapper.c:54)
  == 2764 == by 0x87CB:imxSend_i2cByte(imx6qi2c_wrapper.c:84)
  == 2764 == by 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;
}

未初始化的字节在哪里?

1 个答案:

答案 0 :(得分:0)

好的只是为了完成我在这里复制了作为评论给出的答案(但从未被知道答案的用户发布为答案)。

memset to 0 on i2cmsg will solve your problem