【问题标题】:valgrind error in ioctl() call, while sending an i2c messageioctl() 调用中的 valgrind 错误,同时发送 i2c 消息
【发布时间】:2015-07-23 01:23:23
【问题描述】:

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

==2764== 错误摘要:来自 1 个上下文的 1 个错误(已抑制:来自 0 的 0 个)
==2764==
==2764== 1 个上下文中的 1 个错误:
==2764== 系统调用参数 ioctl(I2C_RDWR).msgs 指向未初始化的字节
==2764== 在 0x4E08EFEC: ioctl (syscall-template.S:81)
==2764== 由 0x871F:imxSend_i2cMsg (imx6qi2c_wrapper.c:54)
==2764== 0x87CB:imxSend_i2cByte (imx6qi2c_wrapper.c:84)
==2764== 由 0x86D3:主要 (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;
}

未初始化的字节在哪里?

【问题讨论】:

  • i2cmsg[1]..i2cmsg[I2CMSGS_TOTAL_WR-1] 的所有内存不是都未初始化吗?你初始化了i2cmsg[0],但没有初始化任何后续元素。
  • memset to 0 on i2cmsg 将解决您的问题。
  • 是的,确实如此。谢谢
  • @Dayalrai 你应该从你的评论中做出回答。

标签: c valgrind ioctl initialization


【解决方案1】:

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

memset to 0 on i2cmsg will solve your problem

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-16
    • 1970-01-01
    相关资源
    最近更新 更多