Menu

Incorrect 'Array accessed at index <n>, which is out of bounds'

2015-11-16
2015-11-17
  • Paul Aitken

    Paul Aitken - 2015-11-16

    The following code incorrectly gives two error messages:

    [test.c:11]: (error) Array 'cmd_str[8]' accessed at index 127, which is out of bounds.
    [test.c:15]: (error) Array 'cmd_str[8]' accessed at index 9998, which is out of bounds.

    However the output of the program is as expected (ie, "cmd1" through "cmd7").

    #include <stdio.h>
    
    const char *cmd_str[] = { "cmd1", "cmd2", "cmd3", "cmd4", "cmd5", "cmd6", "cmd7", "cmd8" };
    
    void
    cmd2str(void)
    {
        int bit, str;
    
        for (bit = 1, str = 0; bit < 128; str++, bit = bit << 1) {
             printf("%s\n", cmd_str[str]);
        }
    
        for (bit = 1, str = 0; bit < 128; bit = bit << 1, str++) {
             printf("%s\n", cmd_str[str]);
        }
    }
    
    main()
    {
        cmd2str();
    }
    
     

    Last edit: Paul Aitken 2015-11-16
  • Alexander Mai

    Alexander Mai - 2015-11-17

    Thanks for reporting it on http://trac.cppcheck.net/ticket/7139 as well..

     

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.