Question

valgrind is giving me the following error on the ioctl() line: I am not sure how I could avert such an error.

==2764== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
==2764==
==2764== 1 errors in context 1 of 1:
==2764== Syscall param ioctl(I2C_RDWR).msgs points to uninitialised byte(s)
==2764== at 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== Address 0x7db99b82 is on thread 1's stack
==2764== Uninitialised value was created by a stack allocation
==2764== at 0x875C: imxSend_i2cByte (imx6qi2c_wrapper.c:66)

the code in imx6qi2c_wrapper.c is the following:

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;
}

and "i2cMsgArray" is declared in annother function locally.

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;
}

where is the uninitialized byte?

Was it helpful?

Solution

ok just for completion I copy here the answer given as a comment (but was never posted as an answer by the user who knew the answer).

memset to 0 on i2cmsg will solve your problem
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top