Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lint: Call to function 'memcpy(void *, const void *, std::size_t)' violates semantic '(3n>4)'

Tags:

c++

lint

This is a part of my code base . I am not getting the meaning of warning and so am not able to resolve this ... code :

struct ParamsTube{
    uint8                Colours01[4];
    uint8                Colours02[4];
    uint8                Colours03[4];
};

void sample_fun(const uint8 *diagData){
    ParamsTube Record;
    memcpy(&Record.Colours01[0], &diagData[0], 4); //Line 1
    memcpy(&Record.Colours02[0], &diagData[4], 4); //Line 2
    memcpy(&Record.Colours03[0], &diagData[8], 4); //Line 3
}

and LINT warning 426 for this logic at line 1,2 and 3 is

Call to function 'memcpy(void *, const void *, std::size_t)' violates semantic '(3n>4)'

Can you tell me what exactly it means .....

like image 661
Ashwin Avatar asked Dec 05 '25 16:12

Ashwin


1 Answers

(3n > 4) means the third argument used to call memcpy() should be larger than 4, and your calls violate this semantic. The semantic seems to state that memcpy() shouldn't be used to copy data smaller than a machine word (usually 4). That's why lint is warning you. Whether the semantic is appropriate or not is another question.


Below is the explanation of lint warning 426:

426 Call to function 'Symbol' violates semantic 'String' -- This Warning message is issued when a user semantic (as defined by -sem) is violated. 'String' is the subportion of the semantic that was violated. For example:

//lint -sem( f, 1n > 10 && 2n > 10 )  
        void f( int, int );  
        ...  
        f( 2, 20 );  

results in the message:

Call to function 'f(int, int)' violates semantic '(1n>10)'

So the memcpy() in your environment probably has a leading lint semantic like this:

// lint -sem(memcpy, 3n > 4)
void* memcpy(void* s1, const void* s2, std::size_t n);

For your case, if what you want to achieve is actually:

memcpy(&Record.Colours01[0], &diagData[0], 4); //Line 1
memcpy(&Record.Colours02[1], &diagData[4], 4); //Line 2
memcpy(&Record.Colours03[2], &diagData[8], 4); //Line 3

then simply:

memcpy(&Record, diagData, sizeof(Record));  

will do all the work without triggering the lint warning.

like image 199
neverhoodboy Avatar answered Dec 07 '25 17:12

neverhoodboy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!