Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fix stale pointer to robots that no longer exist by only referring to…

… robot id. Avoid EVERY CRASH EVER.
  • Loading branch information...
commit 1d96ceec7a294eed9a65331cb77336175e311756 1 parent 6831a3a
Josh Matthews authored
2  src/debugger/main.c
@@ -131,7 +131,7 @@ static bool process_message(char *message, struct world *mzx_world,
131 131
             _info("setting current line to %d\n", line_number);
132 132
 
133 133
             clear_breakpoints(mzx_world);
134  
-            mzx_world->debug_watch.watch = robot;
  134
+            //mzx_world->debug_watch.watch = robot; //XXX not needed if we hardcode global robot
135 135
             break;
136 136
         }
137 137
 
31  src/debugger/ui.c
@@ -33,7 +33,7 @@
33 33
 void watch_remote_robot(struct world *mzx_world)
34 34
 {
35 35
   struct breakpoint *bp = &mzx_world->debug_watch.breakpoints;
36  
-  struct robot *cur_robot = mzx_world->debug_watch.watch;
  36
+  struct robot *cur_robot = mzx_world->current_board->robot_list[mzx_world->debug_watch.watch_id];
37 37
   FILE *bc_file = fsafeopen(DEBUGGER_BYTECODE, "wb");
38 38
   if(bc_file)
39 39
   {
@@ -62,21 +62,29 @@ bool watch_robot(struct world *mzx_world)
62 62
   struct dialog di;
63 63
   struct element *elements[1];
64 64
   bool robot_selected = false;
  65
+  struct robot *watched = NULL;
  66
+  struct board *src_board = mzx_world->current_board;
  67
+  if (mzx_world->debug_watch.watch_id >= 0)
  68
+      watched = src_board->robot_list[mzx_world->debug_watch.watch_id];
65 69
   
66 70
   m_show();
67 71
   
68 72
   for(i = 0; i < num_robots; i++)
69 73
   {
70 74
     robot_list[i] = calloc(16, 1);
71  
-    cp_len = strlen(mzx_world->current_board->robot_list[i]->robot_name);
72  
-    if(mzx_world->debug_watch.watch)
  75
+    if (!src_board->robot_list[i]) {
  76
+        memset(robot_list[i], ' ', 15);
  77
+        continue;
  78
+    }
  79
+    cp_len = strlen(src_board->robot_list[i]->robot_name);
  80
+    if(watched)
73 81
     {
74  
-      if(!strcmp(mzx_world->current_board->robot_list[i]->robot_name,
75  
-                 mzx_world->debug_watch.watch->robot_name))
  82
+      if(!strcmp(src_board->robot_list[i]->robot_name,
  83
+                 watched->robot_name))
76 84
         selected = i;
77 85
     }
78 86
     memset(robot_list[i], ' ', 15);
79  
-    memcpy(robot_list[i], mzx_world->current_board->robot_list[i]->robot_name, cp_len);
  87
+    memcpy(robot_list[i], src_board->robot_list[i]->robot_name, cp_len);
80 88
   }
81 89
   
82 90
   do
@@ -90,12 +98,13 @@ bool watch_robot(struct world *mzx_world)
90 98
     dialog_result = run_dialog(mzx_world, &di);
91 99
     if(dialog_result == 0)
92 100
     {
93  
-      struct robot *cur_robot = mzx_world->current_board->robot_list[selected];
94  
-      mzx_world->debug_watch.watch = cur_robot;
95  
-      watch_remote_robot(mzx_world);
  101
+      if (mzx_world->current_board->robot_list[selected]) {
  102
+        mzx_world->debug_watch.watch_id = selected;
  103
+        watch_remote_robot(mzx_world);
96 104
 
97  
-      dialog_result = -1;
98  
-      robot_selected = true;
  105
+        dialog_result = -1;
  106
+        robot_selected = true;
  107
+      }
99 108
     }
100 109
     destruct_dialog(&di);
101 110
   } while(dialog_result != -1);
2  src/debugger_host.c
@@ -87,7 +87,7 @@ static bool process_message(char *message, struct world *mzx_world)
87 87
       break;
88 88
     case TOGGLE_BREAKPOINT:
89 89
     {
90  
-      struct robot *watch = mzx_world->debug_watch.watch;
  90
+      struct robot *watch = mzx_world->current_board->robot_list[mzx_world->debug_watch.watch_id];
91 91
       int line = get_message_arg(message, 0);
92 92
       int offset = line_number_to_offset(watch, line);
93 93
       info("instructed to toggle breakpoint at line %d (offset %d)\n", line, offset);
5  src/editor/robo_ed.c
@@ -2799,6 +2799,7 @@ static void display_robot_line(struct robot_state *rstate,
2799 2799
     }
2800 2800
     else
2801 2801
     {
  2802
+#ifdef CONFIG_DEBUGGER
2802 2803
       if(rstate->mzx_world->debugging)
2803 2804
       {
2804 2805
         //FIXME this linear search is inefficient for large programs
@@ -2806,6 +2807,7 @@ static void display_robot_line(struct robot_state *rstate,
2806 2807
         int offset = 2; // FIXME Why is this 2?
2807 2808
         struct robot_line *temp = rstate->base;
2808 2809
         struct breakpoint *bp = &rstate->mzx_world->debug_watch.breakpoints;
  2810
+        struct robot *watched = &rstate->mzx_world->global_robot; //FIXME this is hardcoded in too many locations
2809 2811
         while(temp != current_rline)
2810 2812
         {
2811 2813
           offset += temp->line_bytecode_length;
@@ -2829,7 +2831,7 @@ static void display_robot_line(struct robot_state *rstate,
2829 2831
                             y, current_color, 0);
2830 2832
           return;
2831 2833
         }
2832  
-        else if(rstate->mzx_world->debug_watch.watch->cur_prog_line == offset)
  2834
+        else if(watched->cur_prog_line == offset)
2833 2835
         {
2834 2836
           //FIXME magic number 18
2835 2837
           current_color = combine_colors(color_codes[18], bg_color);
@@ -2839,6 +2841,7 @@ static void display_robot_line(struct robot_state *rstate,
2839 2841
           return;
2840 2842
         }
2841 2843
       }
  2844
+#endif
2842 2845
 
2843 2846
       use_mask = 0;
2844 2847
       arg_length = 0;
2  src/game.c
@@ -1958,7 +1958,7 @@ __editor_maybe_static void play_game(struct world *mzx_world)
1958 1958
               mzx_world->debugging = NOT_DEBUGGING;
1959 1959
             else mzx_world->debugging = STOPPED;
1960 1960
 
1961  
-            mzx_world->debug_watch.watch = NULL;
  1961
+            mzx_world->debug_watch.watch_id = -1;
1962 1962
             if(mzx_world->debugging)
1963 1963
             {
1964 1964
               if(!watch_robot(mzx_world))
38  src/run_robot.c
@@ -307,10 +307,11 @@ int place_player_xy(struct world *mzx_world, int x, int y)
307 307
   return 0;
308 308
 }
309 309
 
310  
-static void set_robot_coords(struct world *mzx_world, struct robot *robot, int x, int y)
  310
+static void set_robot_coords(struct world *mzx_world, int id, int x, int y)
311 311
 {
  312
+    struct robot *robot = mzx_world->current_board->robot_list[id];
312 313
 #ifdef CONFIG_DEBUGGER
313  
-    if (mzx_world->debugging && mzx_world->debug_watch.watch == robot)
  314
+    if (mzx_world->debugging && mzx_world->debug_watch.watch_id == id)
314 315
         debugger_send(UPDATE_COORDS, x, y);
315 316
 #endif
316 317
     robot->xpos = x;
@@ -903,11 +904,11 @@ void run_robot(struct world *mzx_world, int id, int x, int y)
903 904
   {
904 905
     if(mzx_world->debugging == STOPPED)
905 906
       return;
906  
-    else if(mzx_world->debugging == STEPPING_OTHERS
907  
-         && mzx_world->debug_watch.watch == src_board->robot_list[id < 0 ? -id : id])
  907
+    else if(mzx_world->debugging == STEPPING_OTHERS &&
  908
+            mzx_world->debug_watch.watch_id == (id < 0 ? -id : id))
908 909
       mzx_world->debugging = STEPPING;
909  
-    else if(mzx_world->debugging == STEPPING
910  
-         && mzx_world->debug_watch.watch != src_board->robot_list[id < 0 ? -id : id])
  910
+    else if(mzx_world->debugging == STEPPING &&
  911
+            mzx_world->debug_watch.watch_id != (id < 0 ? -id : id))
911 912
       return;
912 913
   }
913 914
 #endif
@@ -921,10 +922,10 @@ void run_robot(struct world *mzx_world, int id, int x, int y)
921 922
   {
922 923
     id = -id;
923 924
     cur_robot = src_board->robot_list[id];
924  
-    set_robot_coords(mzx_world, cur_robot, x, y);
  925
+    set_robot_coords(mzx_world, id, x, y);
925 926
     cur_robot->cycle_count = 0;
926 927
 
927  
-    src_board->robot_list[id]->status = 0;
  928
+    cur_robot->status = 0;
928 929
   }
929 930
   else
930 931
   {
@@ -934,7 +935,7 @@ void run_robot(struct world *mzx_world, int id, int x, int y)
934 935
     walk_dir = cur_robot->walk_dir;
935 936
 
936 937
     // Reset x/y
937  
-    set_robot_coords(mzx_world, cur_robot, x, y);
  938
+    set_robot_coords(mzx_world, id, x, y);
938 939
     // Update cycle count
939 940
 
940 941
     cur_robot->cycle_count++;
@@ -1018,7 +1019,8 @@ void run_robot(struct world *mzx_world, int id, int x, int y)
1018 1019
   find_player(mzx_world);
1019 1020
 
1020 1021
 #ifdef CONFIG_DEBUGGER
1021  
-  if(mzx_world->debugging && cur_robot == mzx_world->debug_watch.watch)
  1022
+  if(mzx_world->debugging &&
  1023
+     cur_robot == src_board->robot_list[mzx_world->debug_watch.watch_id])
1022 1024
   {
1023 1025
     if(mzx_world->debug_watch.commands_executed == -1)
1024 1026
       mzx_world->debug_watch.commands_executed = 0;
@@ -2473,7 +2475,7 @@ void run_robot(struct world *mzx_world, int id, int x, int y)
2473 2475
             cur_robot->cur_prog_line = 0;
2474 2476
 
2475 2477
           cur_robot->cycle_count = 0;
2476  
-          set_robot_coords(mzx_world, cur_robot, x, y);
  2478
+          set_robot_coords(mzx_world, id, x, y);
2477 2479
 
2478 2480
           // Move player
2479 2481
           move_player(mzx_world, dir_to_int(direction));
@@ -5892,7 +5894,7 @@ void run_robot(struct world *mzx_world, int id, int x, int y)
5892 5894
     {
5893 5895
       struct breakpoint *bp;
5894 5896
 
5895  
-      if(cur_robot == mzx_world->debug_watch.watch)
  5897
+      if(cur_robot == src_board->robot_list[mzx_world->debug_watch.watch_id])
5896 5898
         debugger_send(CURRENT_LINE, cur_robot->cur_prog_line);
5897 5899
 
5898 5900
       for(bp = &mzx_world->debug_watch.breakpoints; bp; bp = bp->next)
@@ -5900,7 +5902,7 @@ void run_robot(struct world *mzx_world, int id, int x, int y)
5900 5902
         if(bp->target != cur_robot || bp->pos != cur_robot->cur_prog_line)
5901 5903
           continue;
5902 5904
         
5903  
-        mzx_world->debug_watch.watch = cur_robot;
  5905
+        mzx_world->debug_watch.watch_id = id;
5904 5906
         if(mzx_world->debugging == RUNNING
5905 5907
         || mzx_world->debugging == STEPPING_OTHERS)
5906 5908
         {
@@ -5911,7 +5913,8 @@ void run_robot(struct world *mzx_world, int id, int x, int y)
5911 5913
       }
5912 5914
     }
5913 5915
 
5914  
-    if(mzx_world->debugging == STEPPING && cur_robot == mzx_world->debug_watch.watch)
  5916
+    if(mzx_world->debugging == STEPPING &&
  5917
+       cur_robot == src_board->robot_list[mzx_world->debug_watch.watch_id])
5915 5918
     {
5916 5919
       mzx_world->debugging = STOPPED;
5917 5920
       mzx_world->debug_watch.commands_executed += lines_run + 1;
@@ -5930,8 +5933,9 @@ void run_robot(struct world *mzx_world, int id, int x, int y)
5930 5933
 
5931 5934
 #ifdef CONFIG_DEBUGGER
5932 5935
   // Ended a cycle early
5933  
-  if(mzx_world->debugging && cur_robot == mzx_world->debug_watch.watch
5934  
-  && (mzx_world->debug_watch.commands_executed < mzx_world->commands || done))
  5936
+  if(mzx_world->debugging &&
  5937
+     cur_robot == src_board->robot_list[mzx_world->debug_watch.watch_id] &&
  5938
+     (mzx_world->debug_watch.commands_executed < mzx_world->commands || done))
5935 5939
   {
5936 5940
     mzx_world->debug_watch.commands_executed = -1;
5937 5941
     if(mzx_world->debugging == STEPPING)
@@ -5943,6 +5947,6 @@ void run_robot(struct world *mzx_world, int id, int x, int y)
5943 5947
 
5944 5948
   cur_robot->cycle_count = 0; // In case a label changed it
5945 5949
   // Reset x/y (from movements)
5946  
-  set_robot_coords(mzx_world, cur_robot, x, y);
  5950
+  set_robot_coords(mzx_world, id, x, y);
5947 5951
 }
5948 5952
 
2  src/world.c
@@ -1635,7 +1635,7 @@ __editor_maybe_static void default_global_data(struct world *mzx_world)
1635 1635
 
1636 1636
 #ifdef CONFIG_DEBUGGER
1637 1637
   mzx_world->debugging = false;
1638  
-  mzx_world->debug_watch.watch = NULL;
  1638
+  mzx_world->debug_watch.watch_id = -1;
1639 1639
   mzx_world->debug_watch.breakpoints.target = NULL;
1640 1640
   mzx_world->debug_watch.breakpoints.next = NULL;
1641 1641
 #endif
2  src/world_struct.h
@@ -207,7 +207,7 @@ struct world
207 207
   } debugging;
208 208
   struct
209 209
   {
210  
-    struct robot *watch;
  210
+    int watch_id; // -1 is no watch
211 211
     struct breakpoint breakpoints;
212 212
     int commands_executed;
213 213
   } debug_watch;

0 notes on commit 1d96cee

Please sign in to comment.
Something went wrong with that request. Please try again.