Skip to content

Commit

Permalink
Fuzz meas fixes (#45)
Browse files Browse the repository at this point in the history
* add some fuzz failures from measurement and some fixes for some of the issues

* move the power operation to a separate function.

* update the measurement test fixes from fuzzing.

* update the measurement to do a validity check on the unit conversion
  • Loading branch information
phlptp authored Mar 15, 2020
1 parent 124daa6 commit b3cb621
Show file tree
Hide file tree
Showing 14 changed files with 201 additions and 125 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Add a converter command line application and fix a few slow conversion issues an
- A timeout issue from fuzzing having to do with not injecting multiplies after `[]` in some circumstances. The fix was to be a little more refined as to which point to not inject the `*` and to do it in multiple stages so as to not rely on the partitioner so much. [#35][]
- `fixed_measurement` and `fixed_precise_measurement` had incorrect subtraction operation in a few overloads. [#39][]
- fixed a few initial issues from fuzz_measurement [#42][]
- Some more fuzzing generated issues with cascading powers [#45][]

### Added
- added a [converter](https://units.readthedocs.io/en/latest/introduction/converter.html) command line application that can convert units on the command line [#35][]
Expand Down Expand Up @@ -95,6 +96,7 @@ Continued work on cleaning up the library and starting to add main documentation
[#42]: /~https://github.com/LLNL/units/pull/42
[#43]: /~https://github.com/LLNL/units/pull/43
[#44]: /~https://github.com/LLNL/units/pull/44
[#45]: /~https://github.com/LLNL/units/pull/45

[0.4.0]: /~https://github.com/LLNL/units/releases/tag/v0.4.0
[0.3.0]: /~https://github.com/LLNL/units/releases/tag/v0.3.0
Expand Down
5 changes: 5 additions & 0 deletions FuzzTargets/fuzz_target_measurement_from_string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* Data, size_t Size)
if (!match && isnormal(root(meas2, 3))) {
match = (root(mc2, 3) == root(mc1, 3));
}
if (!match && !(isnormal(mc1) && isnormal(mc2))) {
auto uc1 = unit_cast(meas1.as_unit());
auto uc2 = unit_cast(meas2.as_unit());
match = (uc1 == uc2);
}
}
if (!match) {
if (meas1.units() == meas2.units()) {
Expand Down
Binary file added test/files/fuzz_issues/meas_fail10
Binary file not shown.
Binary file added test/files/fuzz_issues/meas_fail11
Binary file not shown.
Binary file added test/files/fuzz_issues/meas_fail12
Binary file not shown.
Binary file added test/files/fuzz_issues/meas_fail13
Binary file not shown.
Binary file added test/files/fuzz_issues/meas_fail14
Binary file not shown.
1 change: 1 addition & 0 deletions test/files/fuzz_issues/meas_fail15
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
A1Z0-1+
Binary file added test/files/fuzz_issues/meas_fail16
Binary file not shown.
Binary file added test/files/fuzz_issues/meas_fail9
Binary file not shown.
28 changes: 15 additions & 13 deletions test/fuzz_issue_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -346,25 +346,27 @@ TEST_P(measProblems, measFiles)
auto str = to_string(m1);
auto m2 = measurement_from_string(str);
ASSERT_TRUE(m2.units().has_same_base(m1.units()) || isnormal(m2));
auto mc2 = measurement_cast(m2);
auto mc1 = measurement_cast(m1);
if (m2 == m1) {
EXPECT_EQ(m2, m1);
EXPECT_EQ(measurement_cast(m2), measurement_cast(m1));
EXPECT_FALSE(measurement_cast(m2) != measurement_cast(m1));
EXPECT_FALSE(m2 != m1);
} else if (isnormal(root(m2, 2))) {
EXPECT_EQ(
root(measurement_cast(m2), 2), root(measurement_cast(m1), 2));
EXPECT_FALSE(
root(measurement_cast(m2), 2) != root(measurement_cast(m1), 2));
EXPECT_EQ(root(mc2, 2), root(mc1, 2));
EXPECT_FALSE(root(mc2, 2) != root(mc1, 2));
} else if (isnormal(root(m2, 3))) {
EXPECT_EQ(
root(measurement_cast(m2), 3), root(measurement_cast(m1), 3));
EXPECT_FALSE(
root(measurement_cast(m2), 3) != root(measurement_cast(m1), 3));
EXPECT_EQ(root(mc2, 3), root(mc1, 3));
EXPECT_FALSE(root(mc2, 3) != root(mc1, 3));
} else if (isnormal(mc1) && isnormal(mc2)) {
EXPECT_TRUE(mc2 == mc1);
EXPECT_FALSE(mc2 != mc1);
} else {
EXPECT_TRUE(measurement_cast(m2) == measurement_cast(m1));
EXPECT_FALSE(measurement_cast(m2) != measurement_cast(m1));
auto uc1 = unit_cast(m1.as_unit());
auto uc2 = unit_cast(m2.as_unit());
EXPECT_TRUE(uc2 == uc1);
EXPECT_FALSE(uc2 != uc1);
}
}
}

INSTANTIATE_TEST_SUITE_P(measFiles, measProblems, ::testing::Range(6, 7));
INSTANTIATE_TEST_SUITE_P(measFiles, measProblems, ::testing::Range(1, 17));
21 changes: 21 additions & 0 deletions test/test_unit_strings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,21 @@ TEST(stringToUnits, Simple)
EXPECT_EQ(precise::m, unit_from_string("meter"));
}

TEST(stringToUnits, with_space)
{
EXPECT_EQ(precise::m.inv(), unit_from_string("1 /m"));
EXPECT_EQ(precise::m.inv(), unit_from_string("1 /m"));
EXPECT_EQ(precise::m.inv(), unit_from_string("1 /m"));
EXPECT_EQ(precise::m.inv(), unit_from_string("1 / m"));
EXPECT_EQ(precise::m.inv(), unit_from_string("1 / m"));
EXPECT_EQ(precise::m.inv(), unit_from_string("1 / m"));
EXPECT_EQ(precise::m.inv(), unit_from_string("1/ m"));
EXPECT_EQ(precise::m.inv(), unit_from_string("1\t/ m"));
EXPECT_EQ(precise::m.inv(), unit_from_string("1\t/\tm"));
EXPECT_EQ(precise::m.inv(), unit_from_string("1/\tm"));
EXPECT_EQ(precise::m.inv(), unit_from_string(" 1/\tm "));
}

TEST(stringToUnits, to_default_unit)
{
EXPECT_EQ(precise::defunit, unit_from_string("*"));
Expand Down Expand Up @@ -277,6 +292,12 @@ TEST(stringToUnits, Parenthesis)
unit_from_string("(N*yr)^-2/$"));
}

TEST(stringToUnits, multipower)
{
auto res = unit_from_string("(4.56^3)^3");
EXPECT_DOUBLE_EQ(res.multiplier(), std::pow(4.56, 9.0));
}

TEST(stringToUnits, dotNotation)
{
EXPECT_EQ(precise::m, unit_from_string("m.m.m/m.m", single_slash));
Expand Down
Loading

0 comments on commit b3cb621

Please sign in to comment.