Skip to content

Commit 0a79132

Browse files
committed
GH#79 : add more tests and get them to pass
handle NULL pointers
1 parent 40b8fe1 commit 0a79132

File tree

2 files changed

+52
-5
lines changed

2 files changed

+52
-5
lines changed

fortune-mod/fortune/fortune.c

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,9 @@ static FILEDESC *new_fp(void)
429429
fp->tbl.stuff[1] = 0;
430430
fp->tbl.stuff[2] = 0;
431431
fp->tbl.stuff[3] = 0;
432+
fp->name = NULL;
432433
fp->next = NULL;
434+
fp->path = NULL;
433435
fp->prev = NULL;
434436
fp->child = NULL;
435437
fp->parent = NULL;
@@ -1129,8 +1131,18 @@ static int form_file_list(char **files, int file_cnt)
11291131
if (system_dir_tail)
11301132
{
11311133
system_dir_tail->percent = percent;
1132-
File_tail = system_dir_tail;
1133-
File_list = system_dir_list;
1134+
if (File_tail)
1135+
{
1136+
FILEDESC *old = File_tail;
1137+
system_dir_list->prev = File_list;
1138+
old->next = system_dir_list;
1139+
File_tail = system_dir_tail;
1140+
}
1141+
else
1142+
{
1143+
File_tail = system_dir_tail;
1144+
File_list = system_dir_list;
1145+
}
11341146
}
11351147
else
11361148
{
@@ -1567,7 +1579,6 @@ static FILEDESC *pick_child(FILEDESC *parent)
15671579
continue;
15681580
}
15691581
DPRINTF(1, (stderr, " using %s\n", fp->name));
1570-
return fp;
15711582
}
15721583
else
15731584
{
@@ -1584,8 +1595,8 @@ static FILEDESC *pick_child(FILEDESC *parent)
15841595
}
15851596
DPRINTF(
15861597
1, (stderr, " using %s, %ld\n", fp->name, fp->tbl.str_numstr));
1587-
return fp;
15881598
}
1599+
return (fp->child ? (pick_child(fp)) : fp);
15891600
}
15901601

15911602
/*
@@ -1686,7 +1697,9 @@ static void get_fort(void)
16861697
{
16871698
DPRINTF(1, (stderr, "%s", "picking child\n"));
16881699
fp = pick_child(fp);
1700+
assert(!fp->child);
16891701
}
1702+
assert(!fp->child);
16901703
Fortfile = fp;
16911704
get_pos(fp);
16921705
open_dat(fp);

fortune-mod/tests/t/test-fortune-percent.t

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,40 @@ use lib "$FindBin::Bin/lib";
99
use FortTestInst ();
1010

1111
use Path::Tiny qw/ cwd path tempdir tempfile /;
12-
use Test::More tests => 13;
12+
use Test::More tests => 17;
1313
use Test::Trap
1414
qw( trap $trap :flow:stderr(systemsafe):stdout(systemsafe):warn );
1515

16+
# TEST:$_common_tests=2;
17+
sub _common_tests
18+
{
19+
my ( $blurb_base, $inst_dir ) = @_;
20+
21+
{
22+
my @cmd = (
23+
$inst_dir->child( 'games', 'fortune' ),
24+
qw/ 70% all 30% computers /,
25+
);
26+
27+
print "Running [@cmd]\n";
28+
trap
29+
{
30+
system(@cmd);
31+
};
32+
33+
# TEST*$_common_tests
34+
like( $trap->stdout(), qr/\S/ms,
35+
"$blurb_base : 70/30 percentages : stdout was used",
36+
);
37+
38+
# TEST*$_common_tests
39+
like( $trap->stderr(), qr/\A\r?\n?\z/ms,
40+
"$blurb_base : 70/30 percentages : stderr is empty. ",
41+
);
42+
}
43+
return;
44+
}
45+
1646
{
1747
my $inst_dir = FortTestInst::install("fortune-percent-overflow");
1848
my $IS_WIN = ( $^O eq "MSWin32" );
@@ -58,6 +88,8 @@ use Test::Trap
5888
}
5989
}
6090

91+
_common_tests( "local-dir == system-dir", $inst_dir, );
92+
6193
my @cmd = (
6294
$inst_dir->child( 'games', 'fortune' ),
6395
"999999999999999%", "songs-poems"
@@ -180,4 +212,6 @@ EOF
180212
"[ local+system paths have fortunes ] error message for No fortunes found",
181213
);
182214
}
215+
216+
_common_tests( "local-dir is populated and not system-dir", $inst_dir, );
183217
}

0 commit comments

Comments
 (0)