Skip to content

Commit

Permalink
Fixed a bug where using randommin without randommax attribute wou…
Browse files Browse the repository at this point in the history
…ld generates values bigger than the actual pattern length.
  • Loading branch information
end2endzone committed Feb 2, 2024
1 parent eb35390 commit 5329c5f
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 4 deletions.
8 changes: 7 additions & 1 deletion src/core/ActionProperty.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,6 @@ namespace shellanything
if (!min_max_mode)
{
// Generate from the pattern

std::string random_string;
bool success = RandomHelper::GetRandomFromPattern(random, random_string);

Expand All @@ -629,10 +628,17 @@ namespace shellanything
return false;
}

// Limit maximum value based on pattern length if not specified.
if (random_max.empty())
{
max_value = RandomHelper::GetMaxValue(random.size());
}

// Validate pattern length against
size_t min_length = RandomHelper::GetNumericPatternLength(&random[0]);
std::string random_string = RandomHelper::GetRandomMinMaxValue(min_value, max_value + 1); // +1 to include max_value as a possible outcome.

// Add leading zeroes if pattern is made of '0' characters and generated value is smaller than pattern.
bool must_have_leading_zeroes = (random[0] == RandomHelper::NUMERIC_DIGIT_WITH_LEADING_ZEROS_PATTERN);
if (must_have_leading_zeroes)
{
Expand Down
2 changes: 1 addition & 1 deletion src/core/RandomHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ namespace shellanything
static const std::string ANY_CHARACTERS = "!#$()+,-.0123456789;=@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]_abcdefghijklmnopqrstuvwxyz{}~";

static const uint32_t MAX_UINT32 = (uint32_t)-1;
static const uint32_t MAX_DIGITS_32 = 10; // maximum number of digits in an unsigned 32 bit value (4,294,967,295)
static const uint32_t MAX_DIGITS_32 = 10; // maximum number of digits in an unsigned 32 bit value (4,294,967,295 / 4294967295)

bool RandomHelper::IsNumericPattern(const std::string& value)
{
Expand Down
4 changes: 3 additions & 1 deletion src/tests/TestActionProperty.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ namespace shellanything

//ASSERT a multiple menus are available
Menu::MenuPtrList menus = cmgr.GetConfigFiles()[0]->GetMenus();
ASSERT_EQ(menus.size(), 7);
ASSERT_EQ(menus.size(), 8);

//Clear properties
static const char* properties[] = {
Expand All @@ -620,6 +620,7 @@ namespace shellanything
"test5",
"test6",
"test7",
"test8",
};
static const size_t properties_count = sizeof(properties) / sizeof(properties[0]);
for (size_t i = 0; i < properties_count; i++)
Expand Down Expand Up @@ -658,6 +659,7 @@ namespace shellanything
"KXXlVh6Z",
"Pjl_(kF7",
"0056",
"753",
};
static const size_t expected_values_count = sizeof(expected_values) / sizeof(expected_values[0]);
ASSERT_EQ(expected_values_count, properties_count);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,11 @@
</actions>
</menu>

<menu name="menu8">
<actions>
<property name="test8" random="###" randommin="100" />
</actions>
</menu>

</shell>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
<property name="my_coin_flip.index" random="#" randommin="0" randommax="1" />
<property name="my_card.index" random="00" randommin="1" randommax="52" />

<!-- Convert indice ro actual names -->
<!-- Convert numeric card index to a card name -->
<property name="my_coin_flip" value="${coins.names.${my_coin_flip.index}}" />
<property name="my_card" value="${cards.names.${my_card.index}}" />
</actions>
Expand Down

0 comments on commit 5329c5f

Please sign in to comment.