Skip to content

Commit

Permalink
fix Issue 22413 - importC: Error: array index 0 is out of bounds
Browse files Browse the repository at this point in the history
  • Loading branch information
ibuclaw committed Oct 18, 2021
1 parent a326b78 commit 6a2bc71
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 16 deletions.
7 changes: 7 additions & 0 deletions src/dmd/dsymbolsem.d
Expand Up @@ -983,6 +983,13 @@ private extern(C++) final class DsymbolSemanticVisitor : Visitor
ei = new ExpInitializer(dsym._init.loc, e);
dsym._init = ei;
}
else if (sc.flags & SCOPE.Cfile && dsym.type.isTypeSArray() &&
dsym.type.isTypeSArray().isIncomplete())
{
// C11 6.7.9-22 determine the size of the incomplete array,
// or issue an error that the initializer is invalid.
dsym._init = dsym._init.initializerSemantic(sc, dsym.type, INITinterpret);
}

Expression exp = ei.exp;
Expression e1 = new VarExp(dsym.loc, dsym);
Expand Down
41 changes: 25 additions & 16 deletions test/compilable/testcstuff2.c
Expand Up @@ -377,7 +377,7 @@ typedef struct S22182 { int x; } S22182;

int test22182b(S22182* b)
{
return ((S22182*)(b))->x;
return ((S22182*)(b))->x;
}

/***************************************************/
Expand All @@ -404,14 +404,14 @@ int test22245()

void test22262(unsigned char *buf)
{
if (buf == 0)
return;
if (0 == buf)
return;
if (buf == 1)
return;
if (2 == buf)
return;
if (buf == 0)
return;
if (0 == buf)
return;
if (buf == 1)
return;
if (2 == buf)
return;
}

/***************************************************/
Expand Down Expand Up @@ -495,13 +495,13 @@ const struct S22400b C22400b = {C22400};

int test22406(int a)
{
switch (a)
{
case 1: return -1;
case 2: return -2;
case 3: return -3;
}
return 0;
switch (a)
{
case 1: return -1;
case 2: return -2;
case 3: return -3;
}
return 0;
}

/***************************************************/
Expand All @@ -523,6 +523,15 @@ typedef struct S22409
int f1;
} S22409_t;

/***************************************************/
// https://issues.dlang.org/show_bug.cgi?id=22413

int test22413(void)
{
char msg[] = "ok";
return msg[0] | msg[1];
}

/***************************************************/

int test(char *dest)
Expand Down
18 changes: 18 additions & 0 deletions test/fail_compilation/failcstuff2.c
Expand Up @@ -30,6 +30,8 @@ fail_compilation/failcstuff2.c(205): Error: variable `var` is used as a type
fail_compilation/failcstuff2.c(203): variable `var` is declared here
fail_compilation/failcstuff2.c(254): Error: identifier or `(` expected before `)`
fail_compilation/failcstuff2.c(255): Error: identifier or `(` expected
fail_compilation/failcstuff2.c(354): Error: variable `arr` cannot be read at compile time
fail_compilation/failcstuff2.c(360): Error: variable `str` cannot be read at compile time
---
*/

Expand Down Expand Up @@ -112,3 +114,19 @@ void test22102()
}

/***************************************************/
// https://issues.dlang.org/show_bug.cgi?id=22413
#line 350

void test22413a()
{
int arr[6] = {1,2,3,4,5,6};
int arr2[] = arr;
}

void test22413b()
{
const char *str = "hello";
char msg[] = str;
}

/***************************************************/

0 comments on commit 6a2bc71

Please sign in to comment.