Menu

False positive: Array 'src[8]' accessed at index 127, which is out of bounds.

2023-02-23
2023-02-26
  • Dotan Barak

    Dotan Barak - 2023-02-23

    Cppcheck 2.10 emits the following (false?) errors on the following code:
    [test.cpp:11]: (error) Array 'src[8]' accessed at index 127, which is out of bounds.
    [test.cpp:12]: (error) Array 'src[8]' accessed at index 127, which is out of bounds.

    It is worth mentioning, that when i moved the loop from the function to main, everything was ok..

    #include <stdarg.h>
    
    #define BUFFER_SIZE 128
    
    static void
    my_strncpy(char *dst, const char *src, size_t n)
    {
            size_t i;
    
            for (i = 0; i < n; i++) {
                    dst[i] = src[i];
                    if (src[i] == '\0')
                            break;
            }
    }
    
    int main()
    {
            char buffer[BUFFER_SIZE + 1];
    
            my_strncpy(buffer, "Example", BUFFER_SIZE);
    
            return 0;
    }
    

    Thanks!

     

    Last edit: Dotan Barak 2023-02-23
  • CHR

    CHR - 2023-02-23

    I can't reproduce this with the provided code.

     
  • Dotan Barak

    Dotan Barak - 2023-02-23

    Hi.

    Thank you very much for the fast response, i forgot to write the first 2 lines.
    I updated the original message and verified with the online client of cppcheck that it complains about the problem.

    Sorry
    Dotan

     

    Last edit: Dotan Barak 2023-02-23
  • CHR

    CHR - 2023-02-23

    Thanks for the update, ticket is here: https://trac.cppcheck.net/ticket/11580

     
  • Dotan Barak

    Dotan Barak - 2023-02-26

    Thanks for the fast response.
    :)

    BTW, is this issue fixed now?
    I cloned the cppcheck repository and i still see this issue...

     

    Last edit: Dotan Barak 2023-02-26

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.