Permalink
Browse files

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

… robot id. Avoid EVERY CRASH EVER.
  • Loading branch information...
jdm committed Apr 25, 2011
1 parent 6831a3a commit 1d96ceec7a294eed9a65331cb77336175e311756
Showing with 50 additions and 34 deletions.
  1. +1 −1 src/debugger/main.c
  2. +20 −11 src/debugger/ui.c
  3. +1 −1 src/debugger_host.c
  4. +4 −1 src/editor/robo_ed.c
  5. +1 −1 src/game.c
  6. +21 −17 src/run_robot.c
  7. +1 −1 src/world.c
  8. +1 −1 src/world_struct.h
View
@@ -131,7 +131,7 @@ static bool process_message(char *message, struct world *mzx_world,
_info("setting current line to %d\n", line_number);
clear_breakpoints(mzx_world);
- mzx_world->debug_watch.watch = robot;
+ //mzx_world->debug_watch.watch = robot; //XXX not needed if we hardcode global robot
break;
}
View
@@ -33,7 +33,7 @@
void watch_remote_robot(struct world *mzx_world)
{
struct breakpoint *bp = &mzx_world->debug_watch.breakpoints;
- struct robot *cur_robot = mzx_world->debug_watch.watch;
+ struct robot *cur_robot = mzx_world->current_board->robot_list[mzx_world->debug_watch.watch_id];
FILE *bc_file = fsafeopen(DEBUGGER_BYTECODE, "wb");
if(bc_file)
{
@@ -62,21 +62,29 @@ bool watch_robot(struct world *mzx_world)
struct dialog di;
struct element *elements[1];
bool robot_selected = false;
+ struct robot *watched = NULL;
+ struct board *src_board = mzx_world->current_board;
+ if (mzx_world->debug_watch.watch_id >= 0)
+ watched = src_board->robot_list[mzx_world->debug_watch.watch_id];
m_show();
for(i = 0; i < num_robots; i++)
{
robot_list[i] = calloc(16, 1);
- cp_len = strlen(mzx_world->current_board->robot_list[i]->robot_name);
- if(mzx_world->debug_watch.watch)
+ if (!src_board->robot_list[i]) {
+ memset(robot_list[i], ' ', 15);
+ continue;
+ }
+ cp_len = strlen(src_board->robot_list[i]->robot_name);
+ if(watched)
{
- if(!strcmp(mzx_world->current_board->robot_list[i]->robot_name,
- mzx_world->debug_watch.watch->robot_name))
+ if(!strcmp(src_board->robot_list[i]->robot_name,
+ watched->robot_name))
selected = i;
}
memset(robot_list[i], ' ', 15);
- memcpy(robot_list[i], mzx_world->current_board->robot_list[i]->robot_name, cp_len);
+ memcpy(robot_list[i], src_board->robot_list[i]->robot_name, cp_len);
}
do
@@ -90,12 +98,13 @@ bool watch_robot(struct world *mzx_world)
dialog_result = run_dialog(mzx_world, &di);
if(dialog_result == 0)
{
- struct robot *cur_robot = mzx_world->current_board->robot_list[selected];
- mzx_world->debug_watch.watch = cur_robot;
- watch_remote_robot(mzx_world);
+ if (mzx_world->current_board->robot_list[selected]) {
+ mzx_world->debug_watch.watch_id = selected;
+ watch_remote_robot(mzx_world);
- dialog_result = -1;
- robot_selected = true;
+ dialog_result = -1;
+ robot_selected = true;
+ }
}
destruct_dialog(&di);
} while(dialog_result != -1);
View
@@ -87,7 +87,7 @@ static bool process_message(char *message, struct world *mzx_world)
break;
case TOGGLE_BREAKPOINT:
{
- struct robot *watch = mzx_world->debug_watch.watch;
+ struct robot *watch = mzx_world->current_board->robot_list[mzx_world->debug_watch.watch_id];
int line = get_message_arg(message, 0);
int offset = line_number_to_offset(watch, line);
info("instructed to toggle breakpoint at line %d (offset %d)\n", line, offset);
View
@@ -2799,13 +2799,15 @@ static void display_robot_line(struct robot_state *rstate,
}
else
{
+#ifdef CONFIG_DEBUGGER
if(rstate->mzx_world->debugging)
{
//FIXME this linear search is inefficient for large programs
int line_number = 0;
int offset = 2; // FIXME Why is this 2?
struct robot_line *temp = rstate->base;
struct breakpoint *bp = &rstate->mzx_world->debug_watch.breakpoints;
+ struct robot *watched = &rstate->mzx_world->global_robot; //FIXME this is hardcoded in too many locations
while(temp != current_rline)
{
offset += temp->line_bytecode_length;
@@ -2829,7 +2831,7 @@ static void display_robot_line(struct robot_state *rstate,
y, current_color, 0);
return;
}
- else if(rstate->mzx_world->debug_watch.watch->cur_prog_line == offset)
+ else if(watched->cur_prog_line == offset)
{
//FIXME magic number 18
current_color = combine_colors(color_codes[18], bg_color);
@@ -2839,6 +2841,7 @@ static void display_robot_line(struct robot_state *rstate,
return;
}
}
+#endif
use_mask = 0;
arg_length = 0;
View
@@ -1958,7 +1958,7 @@ __editor_maybe_static void play_game(struct world *mzx_world)
mzx_world->debugging = NOT_DEBUGGING;
else mzx_world->debugging = STOPPED;
- mzx_world->debug_watch.watch = NULL;
+ mzx_world->debug_watch.watch_id = -1;
if(mzx_world->debugging)
{
if(!watch_robot(mzx_world))
View
@@ -307,10 +307,11 @@ int place_player_xy(struct world *mzx_world, int x, int y)
return 0;
}
-static void set_robot_coords(struct world *mzx_world, struct robot *robot, int x, int y)
+static void set_robot_coords(struct world *mzx_world, int id, int x, int y)
{
+ struct robot *robot = mzx_world->current_board->robot_list[id];
#ifdef CONFIG_DEBUGGER
- if (mzx_world->debugging && mzx_world->debug_watch.watch == robot)
+ if (mzx_world->debugging && mzx_world->debug_watch.watch_id == id)
debugger_send(UPDATE_COORDS, x, y);
#endif
robot->xpos = x;
@@ -903,11 +904,11 @@ void run_robot(struct world *mzx_world, int id, int x, int y)
{
if(mzx_world->debugging == STOPPED)
return;
- else if(mzx_world->debugging == STEPPING_OTHERS
- && mzx_world->debug_watch.watch == src_board->robot_list[id < 0 ? -id : id])
+ else if(mzx_world->debugging == STEPPING_OTHERS &&
+ mzx_world->debug_watch.watch_id == (id < 0 ? -id : id))
mzx_world->debugging = STEPPING;
- else if(mzx_world->debugging == STEPPING
- && mzx_world->debug_watch.watch != src_board->robot_list[id < 0 ? -id : id])
+ else if(mzx_world->debugging == STEPPING &&
+ mzx_world->debug_watch.watch_id != (id < 0 ? -id : id))
return;
}
#endif
@@ -921,10 +922,10 @@ void run_robot(struct world *mzx_world, int id, int x, int y)
{
id = -id;
cur_robot = src_board->robot_list[id];
- set_robot_coords(mzx_world, cur_robot, x, y);
+ set_robot_coords(mzx_world, id, x, y);
cur_robot->cycle_count = 0;
- src_board->robot_list[id]->status = 0;
+ cur_robot->status = 0;
}
else
{
@@ -934,7 +935,7 @@ void run_robot(struct world *mzx_world, int id, int x, int y)
walk_dir = cur_robot->walk_dir;
// Reset x/y
- set_robot_coords(mzx_world, cur_robot, x, y);
+ set_robot_coords(mzx_world, id, x, y);
// Update cycle count
cur_robot->cycle_count++;
@@ -1018,7 +1019,8 @@ void run_robot(struct world *mzx_world, int id, int x, int y)
find_player(mzx_world);
#ifdef CONFIG_DEBUGGER
- if(mzx_world->debugging && cur_robot == mzx_world->debug_watch.watch)
+ if(mzx_world->debugging &&
+ cur_robot == src_board->robot_list[mzx_world->debug_watch.watch_id])
{
if(mzx_world->debug_watch.commands_executed == -1)
mzx_world->debug_watch.commands_executed = 0;
@@ -2473,7 +2475,7 @@ void run_robot(struct world *mzx_world, int id, int x, int y)
cur_robot->cur_prog_line = 0;
cur_robot->cycle_count = 0;
- set_robot_coords(mzx_world, cur_robot, x, y);
+ set_robot_coords(mzx_world, id, x, y);
// Move player
move_player(mzx_world, dir_to_int(direction));
@@ -5892,15 +5894,15 @@ void run_robot(struct world *mzx_world, int id, int x, int y)
{
struct breakpoint *bp;
- if(cur_robot == mzx_world->debug_watch.watch)
+ if(cur_robot == src_board->robot_list[mzx_world->debug_watch.watch_id])
debugger_send(CURRENT_LINE, cur_robot->cur_prog_line);
for(bp = &mzx_world->debug_watch.breakpoints; bp; bp = bp->next)
{
if(bp->target != cur_robot || bp->pos != cur_robot->cur_prog_line)
continue;
- mzx_world->debug_watch.watch = cur_robot;
+ mzx_world->debug_watch.watch_id = id;
if(mzx_world->debugging == RUNNING
|| mzx_world->debugging == STEPPING_OTHERS)
{
@@ -5911,7 +5913,8 @@ void run_robot(struct world *mzx_world, int id, int x, int y)
}
}
- if(mzx_world->debugging == STEPPING && cur_robot == mzx_world->debug_watch.watch)
+ if(mzx_world->debugging == STEPPING &&
+ cur_robot == src_board->robot_list[mzx_world->debug_watch.watch_id])
{
mzx_world->debugging = STOPPED;
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)
#ifdef CONFIG_DEBUGGER
// Ended a cycle early
- if(mzx_world->debugging && cur_robot == mzx_world->debug_watch.watch
- && (mzx_world->debug_watch.commands_executed < mzx_world->commands || done))
+ if(mzx_world->debugging &&
+ cur_robot == src_board->robot_list[mzx_world->debug_watch.watch_id] &&
+ (mzx_world->debug_watch.commands_executed < mzx_world->commands || done))
{
mzx_world->debug_watch.commands_executed = -1;
if(mzx_world->debugging == STEPPING)
@@ -5943,6 +5947,6 @@ void run_robot(struct world *mzx_world, int id, int x, int y)
cur_robot->cycle_count = 0; // In case a label changed it
// Reset x/y (from movements)
- set_robot_coords(mzx_world, cur_robot, x, y);
+ set_robot_coords(mzx_world, id, x, y);
}
View
@@ -1635,7 +1635,7 @@ __editor_maybe_static void default_global_data(struct world *mzx_world)
#ifdef CONFIG_DEBUGGER
mzx_world->debugging = false;
- mzx_world->debug_watch.watch = NULL;
+ mzx_world->debug_watch.watch_id = -1;
mzx_world->debug_watch.breakpoints.target = NULL;
mzx_world->debug_watch.breakpoints.next = NULL;
#endif
View
@@ -207,7 +207,7 @@ struct world
} debugging;
struct
{
- struct robot *watch;
+ int watch_id; // -1 is no watch
struct breakpoint breakpoints;
int commands_executed;
} debug_watch;

0 comments on commit 1d96cee

Please sign in to comment.