Skip to content

Commit

Permalink
CodeQL: Should set width of input string stream. Also, fixed dvector:…
Browse files Browse the repository at this point in the history
…:allocate(filename). Unit tests added to confirm fixes worked.
  • Loading branch information
johnoel committed Feb 22, 2021
1 parent 7237a6a commit d3be66a
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/linad99/dvect9.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ dvector::dvector(const char* s)
ad_exit(1);
}
char* field = new char[MAX_FIELD_LENGTH + 1];
infile.width(MAX_FIELD_LENGTH + 1);
int count = 0;
do
{
Expand Down Expand Up @@ -192,6 +193,7 @@ dvector::dvector(const char* s)
cout << "Created dvector with address " << _farptr_tolong(v) <<"\n";
#endif
char* err_ptr;
infile.width(MAX_FIELD_LENGTH + 1);
for (int i = 1; i <= count; ++i)
{
infile >> field;
Expand Down Expand Up @@ -333,6 +335,7 @@ void dvector::allocate(const char* s)
}

char* field = new char[MAX_FIELD_LENGTH + 1];
infile.width(MAX_FIELD_LENGTH + 1);

int count = 0;
do
Expand All @@ -359,7 +362,7 @@ void dvector::allocate(const char* s)
infile.clear();
infile.seekg(0,ios::beg);

if ((v = new double[size()]) == 0)
if ((v = new double[static_cast<unsigned int>(count + 2)]) ==0)
{
cerr << " Error trying to allocate memory for dvector\n";
ad_exit(21);
Expand All @@ -378,8 +381,11 @@ void dvector::allocate(const char* s)
cout << "Created ncopies with address " << _farptr_tolong(ncopies) << "\n";
cout << "Created dvector with address " << _farptr_tolong(v) << "\n";
#endif
index_min = 1;
index_max = count;
v -= indexmin();
char* err_ptr;
infile.width(MAX_FIELD_LENGTH + 1);
for (int i = 1; i <= count; ++i)
{
infile >> field;
Expand Down
4 changes: 3 additions & 1 deletion src/linad99/fvar_io2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ dvar_vector::dvar_vector(const char * s)
int i=0;

// char * field = (char *) new[size_t(MAX_FIELD_LENGTH+1)];
char * field = new char[size_t(MAX_FIELD_LENGTH+1)];
char* field = new char[size_t(MAX_FIELD_LENGTH+1)];
infile.width(size_t(MAX_FIELD_LENGTH+1));
int count=0;
do
{
Expand Down Expand Up @@ -168,6 +169,7 @@ dvar_vector::dvar_vector(const char * s)
ad_exit(21);
}
char* err_ptr;
infile.width(size_t(MAX_FIELD_LENGTH+1));
for (i=1;i<=count;i++)
{
infile >> field;
Expand Down
16 changes: 16 additions & 0 deletions tests/gtests/test_dvar_vector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,22 @@ TEST_F(test_dvar_vector, fill)
ASSERT_DOUBLE_EQ(4, value(v(5)));
ASSERT_DOUBLE_EQ(5, value(v(6)));
}
TEST_F(test_dvar_vector, read_from_file)
{
gradient_structure gs;

ofstream ofs("test_dvar_vector_read_from_file.txt");
ofs << "3.5 -6.8 11.4 -77.5" << endl;
ofs.close();

dvar_vector v("test_dvar_vector_read_from_file.txt");

ASSERT_EQ(4, v.size());
ASSERT_DOUBLE_EQ(3.5, value(v(1)));
ASSERT_DOUBLE_EQ(-6.8, value(v(2)));
ASSERT_DOUBLE_EQ(11.4, value(v(3)));
ASSERT_DOUBLE_EQ(-77.5, value(v(4)));
}
TEST_F(test_dvar_vector, min)
{
gradient_structure gs;
Expand Down
28 changes: 28 additions & 0 deletions tests/gtests/test_dvector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,34 @@ TEST_F(test_dvector, sqr)
ASSERT_DOUBLE_EQ(16, results(5));
ASSERT_DOUBLE_EQ(std::pow(5.1, 2), results(6));
}
TEST_F(test_dvector, read_from_file2)
{
ofstream ofs("test_dvector_read_from_file2.txt");
ofs << "3.5 -6.8 11.4 -77.5" << endl;
ofs.close();

dvector v("test_dvector_read_from_file2.txt");

ASSERT_EQ(4, v.size());
ASSERT_DOUBLE_EQ(3.5, v(1));
ASSERT_DOUBLE_EQ(-6.8, v(2));
ASSERT_DOUBLE_EQ(11.4, v(3));
ASSERT_DOUBLE_EQ(-77.5, v(4));
}
TEST_F(test_dvector, allocate_read_from_file3)
{
ofstream ofs("test_dvector_read_from_file3.txt");
ofs << "3.5 -6.8 11.4 -77.5" << endl;
ofs.close();

dvector v;
v.allocate("test_dvector_read_from_file3.txt");
ASSERT_EQ(4, v.size());
ASSERT_DOUBLE_EQ(3.5, v(1));
ASSERT_DOUBLE_EQ(-6.8, v(2));
ASSERT_DOUBLE_EQ(11.4, v(3));
ASSERT_DOUBLE_EQ(-77.5, v(4));
}
TEST_F(test_dvector, log10)
{
char array[] = "{0.5, 1.5, 2, 3, 4, 5.1}";
Expand Down

0 comments on commit d3be66a

Please sign in to comment.