Skip to content

Commit

Permalink
Fix integer overflow on 32 bit architectures
Browse files Browse the repository at this point in the history
  • Loading branch information
wernerwa authored and wernerwank committed Feb 17, 2021
1 parent d8800ae commit 7b10c4c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
15 changes: 13 additions & 2 deletions src/Vies/Validator/ValidatorFR.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,15 @@ public function validate(string $vatNumber): bool
private function validateOld(string $vatNumber): string
{
$checkVal = substr($vatNumber, 2);
if (! ctype_digit($checkVal)) {
return "";
}
$checkVal .= "12";
$checkVal = intval($checkVal) % 97;
if (PHP_INT_SIZE === 4 && function_exists("bcmod")) {
$checkVal = (int) bcmod($checkVal, "97");
} else {
$checkVal = intval($checkVal) % 97;
}

return $checkVal == 0 ? "00" : (string) $checkVal;
}
Expand All @@ -112,6 +119,10 @@ private function validateNew(string $vatNumber): bool
$checkCharacter = array_flip(str_split($this->alphabet));
$checkVal = ($checkCharacter[$vatNumber[0]] * $multiplier) + $checkCharacter[$vatNumber[1]] - $subStractor;

return (((intval(substr($vatNumber, 2)) + ($checkVal / 11) + 1) % 11) == $checkVal % 11);
if (PHP_INT_SIZE === 4 && function_exists("bcmod")) {
return (int) bcmod(bcadd(substr($vatNumber, 2), strval(($checkVal / 11) + 1)), "11") === $checkVal % 11;
} else {
return ((intval(substr($vatNumber, 2)) + ($checkVal / 11) + 1) % 11) == $checkVal % 11;
}
}
}
8 changes: 6 additions & 2 deletions src/Vies/Validator/ValidatorNL.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,18 @@ protected function validateSoleProprietor(string $vatNumber): bool
return false;
}

$sumBase = (int)array_reduce(str_split($vatNumber), function ($acc, $e) {
$sumBase = array_reduce(str_split($vatNumber), function ($acc, $e) {
if (ctype_digit($e)) {
return $acc.$e;
}

return $acc.$this->checkCharacter[$e];
}, '2321');

return ($sumBase % 97) === 1;
if (PHP_INT_SIZE === 4 && function_exists('bcmod')) {
return bcmod($sumBase, '97') === '1';
} else {
return ((int) $sumBase % 97) === 1;
}
}
}
10 changes: 8 additions & 2 deletions src/Vies/Validator/ValidatorSK.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,13 @@ public function validate(string $vatNumber): bool
return false;
}

return in_array((int) $vatNumber[2], [2, 3, 4, 7, 8, 9])
&& $vatNumber % 11 == 0;
if (in_array((int) $vatNumber[2], [2, 3, 4, 7, 8, 9])) {
if (PHP_INT_SIZE === 4 && function_exists('bcmod')) {
return bcmod($vatNumber, '11') === '0';
} else {
return $vatNumber % 11 == 0;
}
}
return false;
}
}

0 comments on commit 7b10c4c

Please sign in to comment.