Skip to content

Commit 75bc1df

Browse files
authored
fixing read_file issue with invalid input (#528)
Signed-off-by: Andrey Parfenov <a1994ndrey@gmail.com>
1 parent bfbcaa5 commit 75bc1df

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

src/data_handler/data_handler.cpp

+16-1
Original file line numberDiff line numberDiff line change
@@ -998,10 +998,25 @@ int read_file (double *data, int *num_rows, int *num_cols, const char *file_name
998998
splitted.push_back (tmp);
999999
}
10001000
}
1001+
if ((total_cols != 0) && (total_cols != (int)splitted.size ()))
1002+
{
1003+
data_logger->error ("some rows have more cols than others, invalid input file");
1004+
fclose (fp);
1005+
return (int)BrainFlowExitCodes::INVALID_ARGUMENTS_ERROR;
1006+
}
10011007
total_cols = (int)splitted.size ();
10021008
for (int i = 0; i < total_cols; i++)
10031009
{
1004-
data[i * total_rows + current_row] = std::stod (splitted[i]);
1010+
try
1011+
{
1012+
data[i * total_rows + current_row] = std::stod (splitted[i]);
1013+
}
1014+
catch (const std::invalid_argument &)
1015+
{
1016+
fclose (fp);
1017+
data_logger->error ("found not a number in data file");
1018+
return (int)BrainFlowExitCodes::INVALID_ARGUMENTS_ERROR;
1019+
}
10051020
cur_pos++;
10061021
if (cur_pos == (num_elements - 1))
10071022
{

0 commit comments

Comments
 (0)