[llvm-bugs] [Bug 25599] New: Add warning if array initializer contains less elements than declared

via llvm-bugs llvm-bugs at lists.llvm.org
Sat Nov 21 22:55:20 PST 2015


https://llvm.org/bugs/show_bug.cgi?id=25599

            Bug ID: 25599
           Summary: Add warning if array initializer contains less
                    elements than declared
           Product: new-bugs
           Version: unspecified
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P
         Component: new bugs
          Assignee: unassignedbugs at nondot.org
          Reporter: samjnaa at gmail.com
                CC: llvm-bugs at lists.llvm.org
    Classification: Unclassified

Consider this small program arr.c:

#include <stdio.h>
const int len = 3;
const char * arr[len] = {"a", "b", "c"};
int main() { for (int i = 0; i < len; ++i) printf("%c ", arr[i][0]);
putchar('\n'); }

If I include excess elements in the array:

const char * arr[len] = {"a", "b", "c", "d"};

Clang warns me though it is harmless:

$ clang -o arr arr.c && ./arr
arr.c:3:41: warning: excess elements in array initializer
const char * arr[len] = {"a", "b", "c", "d"};
                                        ^~~
1 warning generated.
a b c

But if there are less elements:

const char * arr[len] = {"a", "b"};

Clang doesn't warn me and it segfaults:

$ clang -o arr arr.c && ./arr
Segmentation fault (core dumped)

I realize it is the fault of the programmer to deref the pointer without
checking its validity, but the point of a warning is to be helpful, and so I
request that Clang may produce a warning in such cases.

Thanks.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20151122/4da3771f/attachment-0001.html>


More information about the llvm-bugs mailing list