Skip to content

Commit

Permalink
fix: illegal division by zero in percent estimation (#8783)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephanegigandet committed Aug 7, 2023
1 parent b2e684a commit 799f250
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/ProductOpener/Ingredients.pm
Original file line number Diff line number Diff line change
Expand Up @@ -2663,7 +2663,7 @@ sub init_percent_values ($total_min, $total_max, $ingredients_ref) {
# Go through each ingredient to set percent_min, percent_max, and if we can an absolute percent

foreach my $ingredient_ref (@{$ingredients_ref}) {
if (defined $ingredient_ref->{percent}) {
if ((defined $ingredient_ref->{percent}) and ($ingredient_ref->{percent} > 0)) {
# There is a specified percent for the ingredient.

if ($percent_mode eq "grams") {
Expand Down Expand Up @@ -4719,6 +4719,9 @@ sub preparse_ingredients_text ($product_lc, $text) {
}

$text =~ s/\"/"/g;
$text =~ s/\&lt;/</g;
$text =~ s/\&gt;/>/g;
$text =~ s/\&apos;/'/g;
$text =~ s//'/g;

# turn special chars to spaces
Expand Down
42 changes: 42 additions & 0 deletions tests/unit/ingredients_percent.t
Original file line number Diff line number Diff line change
Expand Up @@ -1064,6 +1064,48 @@ my @tests = (

],

# Trigger illegal division by zero - https://github.com/openfoodfacts/openfoodfacts-server/issues/8782
[
{
lc => "fr",
ingredients_text => 'légumes 0% (épices et aromates (contient oignon et safran &lt;0.1%), sel)',
},
[
{
'id' => 'en:vegetable',
'ingredients' => [
{
'id' => 'en:herbs-and-spices',
'ingredients' => [
{
'id' => 'en:onion',
'percent_estimate' => 25,
'text' => 'contient oignon'
},
{
'id' => 'en:e164',
'percent' => '0.1',
'percent_estimate' => 25,
'text' => 'safran'
}
],
'percent_estimate' => 50,
'text' => "\x{e9}pices et aromates"
},
{
'id' => 'en:salt',
'percent_estimate' => 50,
'text' => 'sel'
}
],
'percent' => 0,
'percent_estimate' => 100,
'text' => "l\x{e9}gumes"
}
]

]

);

foreach my $test_ref (@tests) {
Expand Down

0 comments on commit 799f250

Please sign in to comment.