Menu

a case of CWE: 788

AlexHsieh
2024-08-09
2024-08-23
  • AlexHsieh

    AlexHsieh - 2024-08-09

    Hi,
    I use cppcheck-2.14.1 to check the following code

    #pragma pack(1)
    typedef struct
    {
       long  a1;
       char  a2;
    } SA;  
    #pragma pack()
    
    void f()
    {
        SA a;
        char b[5] = {};
        memmove(&a, b, sizeof(a));
    }
    

    , and get
    CWE: 788
    Buffer is accessed out of bounds: b

    Is this a false positive?

     

    Last edit: AlexHsieh 2024-08-09
  • Andrew C Aitchison

    long is 8 bytes on most 64bit systems. 8+1 > 5 ... but I still get the warning with
    char b[15] = {};

    But

    void f()
    {
    SA a[2];
    char b[31] = {};
    memmove(&a, b, sizeof(a));
    }
    also fails, suggesting that the long in a[1] will be aligned to b[16].
    So, I think cppcheck is ignoring #pragma pack(1)

     
    • AlexHsieh

      AlexHsieh - 2024-08-23

      thank you for sharing

       
  • CHR

    CHR - 2024-08-23
     

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.