From Bill Parker:
authorguy <guy@f5534014-38df-0310-8fa8-9805f1628bb7>
Sat, 29 Sep 2012 19:40:27 +0000 (19:40 +0000)
committerguy <guy@f5534014-38df-0310-8fa8-9805f1628bb7>
Sat, 29 Sep 2012 19:40:27 +0000 (19:40 +0000)
Add some additional memory-allocation failure checks in Lemon.

Use NULL rather than 0 as the null-pointer constant in those
checks.

From me:

Catch one more of the NULL-vs-0 cases.

Fix some failure messages to use fprintf(stderr, ...) -
ErrorMsg() requires a file name and line number, and is
generally used if you're going to continue rather than just give
up.

git-svn-id: http://anonsvn.wireshark.org/wireshark/trunk@45214 f5534014-38df-0310-8fa8-9805f1628bb7

AUTHORS
tools/lemon/lemon.c

diff --git a/AUTHORS b/AUTHORS
index d9a65cc8b4a2e7917161c888ce34ba1844309151..dbeca3bd5629cb425b73356b44d00717ab8bc16e 100644 (file)
--- a/AUTHORS
+++ b/AUTHORS
@@ -3677,6 +3677,7 @@ Martin Kupec              <martin.kupec[AT]kupson.cz>
 Litao Gao              <ltgao[AT]juniper.net>
 Niels Widger           <niels[AT]qacafe.com>
 Pontus Fuchs           <pontus.fuchs[AT]gmail.com>
+Bill Parker            <wp02855[AT]gmail.com>
 
 Dan Lasley <dlasley[AT]promus.com> gave permission for his
 dumpit() hex-dump routine to be used.
index 05830c391187cecb6a17671cc6fe2d22be3062d0..ca47c1f9d08cc9c1190d119a1370d4d91ecf153a 100644 (file)
@@ -377,14 +377,14 @@ void Configtable_clear(int(*)(struct config *));
 
 /* Allocate a new parser action */
 static struct action *Action_new(void){
-  static struct action *freelist = 0;
+  static struct action *freelist = NULL;
   struct action *new;
 
-  if( freelist==0 ){
+  if( freelist==NULL ){
     int i;
     int amt = 100;
     freelist = (struct action *)calloc(amt, sizeof(struct action));
-    if( freelist==0 ){
+    if( freelist==NULL ){
       fprintf(stderr,"Unable to allocate memory for a new parser action.");
       exit(1);
     }
@@ -1095,11 +1095,11 @@ static int resolve_conflict(
 ** in the LEMON parser generator.
 */
 
-static struct config *freelist = 0;      /* List of free configurations */
-static struct config *current = 0;       /* Top of list of configurations */
-static struct config **currentend = 0;   /* Last on list of configs */
-static struct config *basis = 0;         /* Top of list of basis configs */
-static struct config **basisend = 0;     /* End of list of basis configs */
+static struct config *freelist = NULL;      /* List of free configurations */
+static struct config *current = NULL;       /* Top of list of configurations */
+static struct config **currentend = NULL;   /* Last on list of configs */
+static struct config *basis = NULL;         /* Top of list of basis configs */
+static struct config **basisend = NULL;     /* End of list of basis configs */
 
 /* Return a pointer to a new configuration */
 PRIVATE struct config *newconfig(void){
@@ -1108,7 +1108,7 @@ PRIVATE struct config *newconfig(void){
     int i;
     int amt = 3;
     freelist = (struct config *)calloc( amt, sizeof(struct config) );
-    if( freelist==0 ){
+    if( freelist==NULL ){
       fprintf(stderr,"Unable to allocate memory for a new configuration.");
       exit(1);
     }
@@ -2149,7 +2149,7 @@ to follow the previous rule.");
         struct rule *rp;
         rp = (struct rule *)calloc( sizeof(struct rule) +
              sizeof(struct symbol*)*psp->nrhs + sizeof(char*)*psp->nrhs, 1);
-        if( rp==0 ){
+        if( rp==NULL ){
           ErrorMsg(psp->filename,psp->tokenlineno,
             "Can't allocate enough memory for this rule.");
           psp->errorcnt++;
@@ -2198,10 +2198,18 @@ to follow the previous rule.");
         if( msp->type!=MULTITERMINAL ){
           struct symbol *origsp = msp;
           msp = calloc(1,sizeof(*msp));
+         if (msp == NULL) {
+            fprintf(stderr, "Unable to allocate enough memory for MSP, exiting...\n");
+            exit(1);
+         }
           memset(msp, 0, sizeof(*msp));
           msp->type = MULTITERMINAL;
           msp->nsubsym = 1;
           msp->subsym = calloc(1,sizeof(struct symbol*));
+         if (msp->subsym == NULL) {
+            fprintf(stderr, "Unable to allocate enough memory for MSP->subsym, exiting...\n");
+            exit(1);
+         }
           msp->subsym[0] = origsp;
           msp->name = origsp->name;
           psp->rhs[psp->nrhs-1] = msp;
@@ -2712,7 +2720,7 @@ struct plink *Plink_new(void){
     int i;
     int amt = 100;
     plink_freelist = (struct plink *)calloc( amt, sizeof(struct plink) );
-    if( plink_freelist==0 ){
+    if( plink_freelist==NULL ){
       fprintf(stderr,
       "Unable to allocate memory for a new follow-set propagation link.\n");
       exit(1);
@@ -3455,6 +3463,10 @@ PRIVATE void print_stack_union(
   /* Allocate and initialize types[] and allocate stddt[] */
   arraysize = lemp->nsymbol * 2;
   types = (char**)calloc( arraysize, sizeof(char*) );
+  if (types == NULL) {
+    fprintf(stderr, "Unable to allocate enough memory for types\n");
+    exit(1);
+  }
   for(i=0; i<arraysize; i++) types[i] = 0;
   maxdtlength = 0;
   if( lemp->vartype ){
@@ -3737,7 +3749,7 @@ void ReportTable(
 
   /* Compute the actions on all states and count them up */
   ax = calloc(lemp->nstate*2, sizeof(ax[0]));
-  if( ax==0 ){
+  if( ax==NULL ){
     fprintf(stderr,"malloc failed\n");
     exit(1);
   }
@@ -4213,7 +4225,7 @@ void SetSize(int n)
 char *SetNew(void){
   char *s;
   s = (char*)calloc( size, 1);
-  if( s==0 ){
+  if( s==NULL ){
     memory_error();
   }
   return s;