Skip to content

Commit 4478c14

Browse files
committed
Fix segfault when argc -1 > MAX_TANKS
forftanks now reads up to MAX_TANKS valid tank directories in the order provided on the command line. The 'order' index LUT has been re-purposed to randomise the order they are placed on the game board.
1 parent 1f3ea57 commit 4478c14

File tree

1 file changed

+19
-19
lines changed

1 file changed

+19
-19
lines changed

forftanks.c

+19-19
Original file line numberDiff line numberDiff line change
@@ -501,24 +501,12 @@ main(int argc, char *argv[])
501501
fprintf(stdout, "// SEED=%d\n", seed);
502502
}
503503

504-
/* Shuffle the order we read things in */
505-
for (i = 0; i < MAX_TANKS; i += 1) {
506-
order[i] = i;
507-
}
508-
for (i = 0; i < argc - 1; i += 1) {
509-
int j = rand() % (argc - 1);
510-
int n = order[j];
511-
512-
order[j] = order[i];
513-
order[i] = n;
514-
}
515-
516504
/* Every argument is a tank directory */
517-
for (i = 0; i < argc - 1; i += 1) {
505+
for (i = 1; ntanks < MAX_TANKS && i < argc; i += 1) {
518506
if (ft_read_tank(&myftanks[ntanks],
519507
&mytanks[ntanks],
520508
lenv,
521-
argv[order[i] + 1])) {
509+
argv[i])) {
522510
ntanks += 1;
523511
}
524512
}
@@ -542,17 +530,29 @@ main(int argc, char *argv[])
542530
game.size[1] = y * SPACING;
543531
}
544532

533+
/* Shuffle the order we place things on the game board */
534+
for (i = 0; i < ntanks; i += 1) {
535+
order[i] = i;
536+
}
537+
for (i = 0; i < ntanks; i += 1) {
538+
int j = rand() % ntanks;
539+
int n = order[j];
540+
541+
order[j] = order[i];
542+
order[i] = n;
543+
}
544+
545545
/* Position tanks */
546546
{
547547
int x = SPACING/2;
548548
int y = SPACING/2;
549549

550550
for (i = 0; i < ntanks; i += 1) {
551-
mytanks[i].position[0] = (float)x;
552-
mytanks[i].position[1] = (float)y;
553-
mytanks[i].angle = deg2rad(rand() % 360);
554-
mytanks[i].turret.current = deg2rad(rand() % 360);
555-
mytanks[i].turret.desired = mytanks[i].turret.current;
551+
mytanks[order[i]].position[0] = (float)x;
552+
mytanks[order[i]].position[1] = (float)y;
553+
mytanks[order[i]].angle = deg2rad(rand() % 360);
554+
mytanks[order[i]].turret.current = deg2rad(rand() % 360);
555+
mytanks[order[i]].turret.desired = mytanks[order[i]].turret.current;
556556

557557
x += SPACING;
558558
if (x > game.size[0]) {

0 commit comments

Comments
 (0)