* src/SDCC.y: fixed bug #1744146: Crash when compiling array of
authorborutr <borutr@4a8a32a2-be11-0410-ad9d-d568d2c75423>
Wed, 27 Jun 2007 19:15:37 +0000 (19:15 +0000)
committerborutr <borutr@4a8a32a2-be11-0410-ad9d-d568d2c75423>
Wed, 27 Jun 2007 19:15:37 +0000 (19:15 +0000)
  negative size

git-svn-id: https://sdcc.svn.sourceforge.net/svnroot/sdcc/trunk/sdcc@4867 4a8a32a2-be11-0410-ad9d-d568d2c75423

ChangeLog
src/SDCC.y
support/Util/SDCCerr.c
support/Util/SDCCerr.h

index 77e199a38ef46c6b81104c97561151aee13bb91f..fcd83f0e6fb080217f9be22fc600fe5dc5846dcb 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -4,6 +4,8 @@
          in warning
        * configure.in, configure, sdccconf_in.h: find out the endianess of
          host machine
+       * src/SDCC.y: fixed bug #1744146: Crash when compiling array of
+         negative size
 
 2007-06-27 Gudjon I. Gudjonsson <gudjon AT gudjon.org>
 
index e54f49e32d7a4253e3ae12476881bc62ccae6a5d..1196d705c150e0ba8aa2d3ccccfaf5e34f132d2c 100644 (file)
@@ -520,14 +520,14 @@ declaration_specifiers
      else
        $$ = mergeSpec($1,$2, "type_specifier declaration_specifiers");
    }
-   | function_specifier                                    { $$ = $1; }
+   | function_specifier                             { $$ = $1; }
    | function_specifier declaration_specifiers          { 
      /* if the decl $2 is not a specifier */
      /* find the spec and replace it      */
      if ( !IS_SPEC($2)) {
        sym_link *lnk = $2 ;
        while (lnk && !IS_SPEC(lnk->next))
-        lnk = lnk->next;
+         lnk = lnk->next;
        lnk->next = mergeSpec($1,lnk->next, "function_specifier declaration_specifiers - skipped");
        $$ = $2 ;
      }
@@ -1115,23 +1115,32 @@ declarator2
          }
    | declarator3 '[' constant_expr ']'
          {
-            sym_link   *p ;
-                        value *tval;
+            sym_link *p;
+            value *tval;
+            int size;
 
-            tval = constExprValue($3,TRUE);
+            tval = constExprValue($3, TRUE);
             /* if it is not a constant then Error  */
             p = newLink (DECLARATOR);
-            DCL_TYPE(p) = ARRAY ;
-            if ( !tval || (SPEC_SCLS(tval->etype) != S_LITERAL)) {
-               werror(E_CONST_EXPECTED) ;
-               /* Assume a single item array to limit the cascade */
-               /* of additional errors. */
-               DCL_ELEM(p) = 1;
-            }
-            else {
-               DCL_ELEM(p) = (int) floatFromVal(tval) ;
-            }
-            addDecl($1,0,p);
+            DCL_TYPE(p) = ARRAY;
+
+            if (!tval || (SPEC_SCLS(tval->etype) != S_LITERAL))
+              {
+                werror(E_CONST_EXPECTED);
+                /* Assume a single item array to limit the cascade */
+                /* of additional errors. */
+                size = 1;
+              }
+            else
+              {
+                if ((size = (int)floatFromVal(tval)) < 0)
+                  {
+                    werror(E_NEGATIVE_ARRAY_SIZE, $1->name);
+                    size = 1;
+                  }
+              }
+            DCL_ELEM(p) = size;
+            addDecl($1, 0, p);
          }
    ;
 
@@ -1194,40 +1203,40 @@ pointer
          }
    | unqualified_pointer type_specifier_list pointer
          {
-            $$ = $1 ;               
-            if (IS_SPEC($2) && DCL_TYPE($3) == UPOINTER) {
-                DCL_PTR_CONST($1) = SPEC_CONST($2);
-                DCL_PTR_VOLATILE($1) = SPEC_VOLATILE($2);
-                DCL_PTR_RESTRICT($1) = SPEC_RESTRICT($2);
-                switch (SPEC_SCLS($2)) {
-                case S_XDATA:
-                    DCL_TYPE($3) = FPOINTER;
-                    break;
-                case S_IDATA:
-                    DCL_TYPE($3) = IPOINTER ;
-                    break;
-                case S_PDATA:
-                    DCL_TYPE($3) = PPOINTER ;
-                    break;
-                case S_DATA:
-                    DCL_TYPE($3) = POINTER ;
-                    break;
-                case S_CODE:
-                    DCL_TYPE($3) = CPOINTER ;
-                    break;
-                case S_EEPROM:
-                    DCL_TYPE($3) = EEPPOINTER;
-                    break;
-                default:
-                  // this could be just "constant" 
-                  // werror(W_PTR_TYPE_INVALID);
-                    ;
-                }
-            }
-            else 
-                werror (W_PTR_TYPE_INVALID);
-            $$->next = $3 ;
-        }
+             $$ = $1 ;               
+             if (IS_SPEC($2) && DCL_TYPE($3) == UPOINTER) {
+                 DCL_PTR_CONST($1) = SPEC_CONST($2);
+                 DCL_PTR_VOLATILE($1) = SPEC_VOLATILE($2);
+                 DCL_PTR_RESTRICT($1) = SPEC_RESTRICT($2);
+                 switch (SPEC_SCLS($2)) {
+                 case S_XDATA:
+                     DCL_TYPE($3) = FPOINTER;
+                     break;
+                 case S_IDATA:
+                     DCL_TYPE($3) = IPOINTER ;
+                     break;
+                 case S_PDATA:
+                     DCL_TYPE($3) = PPOINTER ;
+                     break;
+                 case S_DATA:
+                     DCL_TYPE($3) = POINTER ;
+                     break;
+                 case S_CODE:
+                     DCL_TYPE($3) = CPOINTER ;
+                     break;
+                 case S_EEPROM:
+                     DCL_TYPE($3) = EEPPOINTER;
+                     break;
+                 default:
+                   // this could be just "constant" 
+                   // werror(W_PTR_TYPE_INVALID);
+                     ;
+                 }
+             }
+             else 
+                 werror (W_PTR_TYPE_INVALID);
+             $$->next = $3 ;
+         }
    ;
 
 unqualified_pointer
@@ -1718,4 +1727,3 @@ identifier
    : IDENTIFIER   { $$ = newSymbol ($1,NestLevel) ; }
    ;
 %%
-
index eb69d38d4493f59e4d3b8aff1325b899e2a7829f..a20917f0b6c73e28e20e813274afdd582cf56a82 100644 (file)
 
 #include "SDCCerr.h"
 
-#define USE_STDOUT_FOR_ERRORS          0
+#define USE_STDOUT_FOR_ERRORS   0
 
 #if USE_STDOUT_FOR_ERRORS
-#define DEFAULT_ERROR_OUT      stdout
+#define DEFAULT_ERROR_OUT       stdout
 #else
-#define DEFAULT_ERROR_OUT      stderr
+#define DEFAULT_ERROR_OUT       stderr
 #endif
 
 struct SDCCERRG _SDCCERRG; 
@@ -39,11 +39,11 @@ extern int fatalError ;
  * entry in the array. It is only included in order to make 
  * human error lookup easier.
  */
-struct  
+struct
 {
-    int                        errIndex;
-    ERROR_LOG_LEVEL    errType;
-    const char         *errText;
+    int                 errIndex;
+    ERROR_LOG_LEVEL     errType;
+    const char          *errText;
 } ErrTab [] =
 {
 { E_DUPLICATE, ERROR_LEVEL_ERROR,
@@ -195,11 +195,11 @@ struct
 { E_INT_ARGS, ERROR_LEVEL_ERROR,
    "interrupt routine cannot have arguments, arguments ingored" },
 { E_INCLUDE_MISSING, ERROR_LEVEL_ERROR,
-   "critical compiler #include file missing.           " },
+   "critical compiler #include file missing.            " },
 { E_NO_MAIN, ERROR_LEVEL_ERROR,
    "function 'main' undefined" },
 { E_EXTERN_INIT, ERROR_LEVEL_ERROR,
-   "'extern' variable '%s' cannot be initialised       " },
+   "'extern' variable '%s' cannot be initialised        " },
 { E_PRE_PROC_FAILED, ERROR_LEVEL_ERROR,
    "Pre-Processor %s" },
 { E_DUP_FAILED, ERROR_LEVEL_ERROR,
@@ -366,7 +366,7 @@ struct
 { W_SYMBOL_NAME_TOO_LONG, ERROR_LEVEL_WARNING,
     "symbol name too long, truncated to %d chars" },
 { W_CAST_STRUCT_PTR,ERROR_LEVEL_WARNING,
-         "cast of struct %s * to struct %s * " },
+          "cast of struct %s * to struct %s * " },
 { W_LIT_OVERFLOW, ERROR_LEVEL_WARNING,
     "overflow in implicit constant conversion" },
 { E_PARAM_NAME_OMITTED, ERROR_LEVEL_ERROR,
@@ -441,7 +441,9 @@ struct
 { E_BAD_INLINE, ERROR_LEVEL_ERROR,
    "Only functions may be qualified with 'inline'" },
 { E_BAD_INT_ARGUMENT, ERROR_LEVEL_ERROR,
-   "Bad integer argument for option %s." },
+   "Bad integer argument for option %s" },
+{ E_NEGATIVE_ARRAY_SIZE, ERROR_LEVEL_ERROR,
+   "Size of array '%s' is negative" },
 };
 
 /*
@@ -499,17 +501,17 @@ void vwerror (int errNum, va_list marker)
         switch(ErrTab[errNum].errType)
         {
             case ERROR_LEVEL_ERROR:
-               fprintf(_SDCCERRG.out, "error %d: ", errNum);
-               break;
+                fprintf(_SDCCERRG.out, "error %d: ", errNum);
+                break;
             case ERROR_LEVEL_WARNING:
             case ERROR_LEVEL_PEDANTIC:
                 fprintf(_SDCCERRG.out, "warning %d: ", errNum);
-               break;
+                break;
             case ERROR_LEVEL_INFO:
-               fprintf(_SDCCERRG.out, "info %d: ", errNum);
-               break;
-           default:
-               break;                  
+                fprintf(_SDCCERRG.out, "info %d: ", errNum);
+                break;
+            default:
+                break;                  
         }
     
         vfprintf(_SDCCERRG.out, ErrTab[errNum].errText,marker);
index 3fcbdc8d1fb10d17b84f92495483968ba4b92e39..e523cc7a4c81db729e0bb9dd2d1b4b4db476a125 100644 (file)
@@ -209,7 +209,8 @@ SDCCERR - SDCC Standard error handler
 #define W_BAD_PRAGMA_ARGUMENTS        191 /* #pragma %s: bad argument(s); pragma ignored */
 #define E_BAD_RESTRICT                192 /* Only pointers may be qualified with 'restrict' */
 #define E_BAD_INLINE                  193 /* Only functions may be qualified with 'inline' */
-#define E_BAD_INT_ARGUMENT            194 /* Bad integer option argument. */
+#define E_BAD_INT_ARGUMENT            194 /* Bad integer option argument */
+#define E_NEGATIVE_ARRAY_SIZE         195 /* Size of array '%s' is negative */
 #define MAX_ERROR_WARNING             256 /* size of disable warnings array */
 
 /** Describes the maximum error level that will be logged.  Any level