Menu

False Overlapping read/write in memcpy()

2024-04-21
2024-04-22
  • Martin Poupě

    Martin Poupě - 2024-04-21

    Have code:

    int K[2];
    void test(void)
    {
        memcpy(&K[0],&K[1],sizeof(K[0]));
    }
    

    Cppcheck 2.14 generates warning:
    tmp.c:4:5: error: Overlapping read/write in memcpy() is undefined behavior [overlappingWriteFunction]
    memcpy(&K[0],&K[1],sizeof(K[0]));
    ^
    However there is no overlap.

     
  • Andrew C Aitchison

    However this is OK:

    int K[5];
    void test(void)
    {
        memcpy(&K[0],&K[4],sizeof(K[0]));
    
    }
    

    but destination &K[3]is not OK.

     
  • Andrew C Aitchison

    long long K[9];
    void test(void)
    {
    memcpy(&K[0],&K[8],sizeof(K[0]));
    memcpy(&K[0],&K[7],sizeof(K[0]));
    }
    Complains about 8 but not 7.

     
  • CHR

    CHR - 2024-04-22
     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.