Skip to content

Commit 558a17c

Browse files
committed
Reject malformed AGG/BAGG headers before construction
1 parent a809d96 commit 558a17c

2 files changed

Lines changed: 17 additions & 9 deletions

File tree

src/games/agg/agg.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,10 @@ std::shared_ptr<AGG> AGG::makeAGG(istream &in)
152152
if (!in.good()) {
153153
throw std::runtime_error("Error reading the number of function nodes");
154154
}
155+
if (n <= 0 || S < 0 || P < 0) {
156+
throw std::runtime_error("Error in game file: invalid AGG header (number of players, "
157+
"action nodes, or function nodes out of range)");
158+
}
155159
stripComment(in);
156160

157161
// enter sizes of action sets:

src/games/agg/bagg.cc

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,18 +73,22 @@ std::shared_ptr<BAGG> BAGG::makeBAGG(istream &in)
7373
in >> S;
7474
stripComment(in);
7575
in >> P;
76+
if (!in || N <= 0 || S < 0 || P < 0) {
77+
throw std::runtime_error("Error in game file: expected BAGG header with number of "
78+
"players, action nodes, and function nodes");
79+
}
7680

7781
stripComment(in);
7882
vector<int> numTypes(N);
7983

8084
// input number of types for each player
8185
for (int i = 0; i < N; ++i) {
82-
if (in.eof() || in.bad()) {
86+
in >> numTypes[i];
87+
if (!in) {
8388
throw std::runtime_error(
8489
"Error in game file: integer expected for the number of types for player " +
8590
std::to_string(i));
8691
}
87-
in >> numTypes[i];
8892
}
8993

9094
// input the type distributions
@@ -93,10 +97,10 @@ std::shared_ptr<BAGG> BAGG::makeBAGG(istream &in)
9397
for (int i = 0; i < N; ++i) {
9498
TDist.emplace_back(numTypes[i]);
9599
for (int j = 0; j < numTypes[i]; ++j) {
96-
if (in.eof() || in.bad()) {
100+
in >> TDist[i][j];
101+
if (!in) {
97102
throw std::runtime_error("Error in game file: number expected for type distribution");
98103
}
99-
in >> TDist[i][j];
100104
}
101105
}
102106

@@ -105,12 +109,12 @@ std::shared_ptr<BAGG> BAGG::makeBAGG(istream &in)
105109
vector<vector<vector<int>>> typeActionSets(N);
106110
for (int i = 0; i < N; ++i) {
107111
for (int j = 0; j < numTypes[i]; ++j) {
108-
if (in.eof() || in.bad()) {
112+
int temp;
113+
in >> temp;
114+
if (!in || temp < 0) {
109115
throw std::runtime_error(
110116
"Error in game file: integer expected for size of type action set");
111117
}
112-
int temp;
113-
in >> temp;
114118
typeActionSets[i].emplace_back(temp);
115119
}
116120
}
@@ -120,10 +124,10 @@ std::shared_ptr<BAGG> BAGG::makeBAGG(istream &in)
120124
for (int i = 0; i < N; ++i) {
121125
for (int j = 0; j < numTypes[i]; ++j) {
122126
for (int &el : typeActionSets[i][j]) {
123-
if (in.eof() || in.bad()) {
127+
in >> el;
128+
if (!in) {
124129
throw std::runtime_error("Error in game file: integer expected for type action set");
125130
}
126-
in >> el;
127131
}
128132
}
129133
}

0 commit comments

Comments
 (0)