[CODE] db.c get_line fixes

From: George (greerga@DRAGON.HAM.MUOHIO.EDU)
Date: 10/02/97


Here's the fixes for db.c to get_line(), this also includes Andrew's
addition to get_line() so we crash immediately if we wrongly use the
buffer.

diff -upPr -x *.o ../stk/db.c ./db.c
--- ../stk/db.c Tue Sep  9 23:09:57 1997
+++ ./db.c      Thu Oct  2 10:58:56 1997
@@ -845,7 +845,10 @@ void parse_simple_mob(FILE *mob_f, int i
     mob_proto[i].real_abils.con = 11;
     mob_proto[i].real_abils.cha = 11;

-    get_line(mob_f, line);
+    if (!get_line(mob_f, line)) {
+      fprintf(stderr, "Format error in mob #%d, no first line.\n", nr);
+      exit(1);
+    }
     if (sscanf(line, " %d %d %d %dd%d+%d %dd%d+%d ",
          t, t + 1, t + 2, t + 3, t + 4, t + 5, t + 6, t + 7, t + 8) != 9) {
       fprintf(stderr, "Format error in mob #%d, first line after S flag\n"
@@ -869,15 +872,24 @@ void parse_simple_mob(FILE *mob_f, int i
     mob_proto[i].mob_specials.damsizedice = t[7];
     mob_proto[i].points.damroll = t[8];

-    get_line(mob_f, line);
+    if (!get_line(mob_f, line)) {
+      fprintf(stderr, "Format error in mob #%d, gold and experience line\n"
+               "...expecting line of form '# #'\n", nr);
+      exit(1);
+    }
     sscanf(line, " %d %d ", t, t + 1);
     GET_GOLD(mob_proto + i) = t[0];
     GET_EXP(mob_proto + i) = t[1];

-    get_line(mob_f, line);
+    if (!get_line(mob_f, line)) {
+      fprintf(stderr, "Format error in mob #%d, second line after S flag\n"
+               "..unexpected end of file.\n", nr);
+      exit(1);
+    }
     if (sscanf(line, " %d %d %d %d ", t, t + 1, t + 2, t + 3) != 3) {
       fprintf(stderr, "Format error in mob #%d, second line after S flag\n"
              "...expecting line of form '# # #'\n", nr);
+      exit(1);
     }

     mob_proto[i].char_specials.position = t[0];
@@ -1029,7 +1041,11 @@ void parse_mobile(FILE * mob_f, int nr)
   mob_proto[i].player.title = NULL;

   /* *** Numeric data *** */
-  get_line(mob_f, line);
+  if (!get_line(mob_f, line)) {
+    fprintf(stderr, "Format error in mob #%d, first line after descriptions\n"
+               "...expecting line of form 'abc abc # x'\n", nr);
+    exit(1);
+  }
   sscanf(line, "%s %s %d %c", f1, f2, t + 2, &letter);
   MOB_FLAGS(mob_proto + i) = asciiflag_conv(f1);
   SET_BIT(MOB_FLAGS(mob_proto + i), MOB_ISNPC);
@@ -1164,7 +1180,11 @@ char *parse_object(FILE * obj_f, int nr)
        fprintf(stderr, "Too many A fields (%d max), %s\n", MAX_OBJ_AFFECT, buf2);
        exit(1);
       }
-      get_line(obj_f, line);
+      if (!get_line(obj_f, line)) {
+       fprintf(stderr, "Format error in mob #%d, line after affection #%d\n"
+               "...expecting line of form '# #'\n", nr, j);
+        exit(1);
+      }
       sscanf(line, " %d %d ", t, t + 1);
       obj_proto[i].affected[j].location = t[0];
       obj_proto[i].affected[j].modifier = t[1];
@@ -1201,34 +1221,48 @@ void load_zones(FILE * fl, char *zonenam

   if (num_of_cmds == 0) {
     fprintf(stderr, "%s is empty!\n", zname);
-    exit(0);
+    exit(1);
   } else
     CREATE(Z.cmd, struct reset_com, num_of_cmds);

-  line_num += get_line(fl, buf);
+  if ((tmp = get_line(fl, buf)) == 0) {
+    fprintf(stderr, "Format error in %s - premature end of file\n", zname);
+    exit(1);
+  }
+  line_num += tmp;

   if (sscanf(buf, "#%d", &Z.number) != 1) {
     fprintf(stderr, "Format error in %s, line %d\n", zname, line_num);
-    exit(0);
+    exit(1);
   }
   sprintf(buf2, "beginning of zone #%d", Z.number);

-  line_num += get_line(fl, buf);
+  if ((tmp = get_line(fl, buf)) == 0) {
+    fprintf(stderr, "Format error in %s - premature end of file\n", zname);
+    exit(1);
+  }
+  line_num += tmp;
+
   if ((ptr = strchr(buf, '~')) != NULL)        /* take off the '~' if it's there */
     *ptr = '\0';
   Z.name = str_dup(buf);

-  line_num += get_line(fl, buf);
+  if ((tmp = get_line(fl, buf)) == 0) {
+    fprintf(stderr, "Format error in %s - premature end of file\n", zname);
+    exit(1);
+  }
+  line_num += tmp;
+
   if (sscanf(buf, " %d %d %d ", &Z.top, &Z.lifespan, &Z.reset_mode) != 3) {
     fprintf(stderr, "Format error in 3-constant line of %s", zname);
-    exit(0);
+    exit(1);
   }
   cmd_no = 0;

   for (;;) {
     if ((tmp = get_line(fl, buf)) == 0) {
       fprintf(stderr, "Format error in %s - premature end of file\n", zname);
-      exit(0);
+      exit(1);
     }
     line_num += tmp;
     ptr = buf;
@@ -1257,7 +1291,7 @@ void load_zones(FILE * fl, char *zonenam

     if (error) {
       fprintf(stderr, "Format error in %s, line %d: '%s'\n", zname, line_num, buf);
-      exit(0);
+      exit(1);
     }
     ZCMD.line = line_num;
     cmd_no++;
diff -upPr -x *.o ../stk/utils.c ./utils.c
--- ../stk/utils.c      Thu Jun 19 00:58:39 1997
+++ ./utils.c   Thu Oct  2 10:54:58 1997
@@ -401,9 +401,10 @@ int get_line(FILE * fl, char *buf)
       temp[strlen(temp) - 1] = '\0';
   } while (!feof(fl) && (*temp == '*' || !*temp));

-  if (feof(fl))
+  if (feof(fl)) {
+    *buf = '\0';       /* Make it NULL so we crash if wrongly used. */
     return 0;
-  else {
+  } else {
     strcpy(buf, temp);
     return lines;
   }


     +------------------------------------------------------------+
     | Ensure that you have read the CircleMUD Mailing List FAQ:  |
     | http://democracy.queensu.ca/~fletcher/Circle/list-faq.html |
     +------------------------------------------------------------+



This archive was generated by hypermail 2b30 : 12/08/00 PST