From e64e57716cc6e51103ed7d28a0dd9102fdaef572 Mon Sep 17 00:00:00 2001 From: MDW Date: Tue, 2 Apr 2024 22:49:22 +0200 Subject: [PATCH 1/2] Fix fatal (PHP8.X) type issue with abs, and amend dol_eval return type # Fix fatal (PHP8.X) type issue with abs, and amend dol_eval return type I got the following message: `Fatal error: Uncaught TypeError: abs(): Argument #1 ($num) must be of type int|float, string given in D:\mdeweerd\workspace\dolibarr\htdocs\projet\list.php on line 233` I examined the phan report and there was no mention of this because dol_eval was said to return mixed. In order to detect such cases, I amended the dol_eval return type to ensure to find most of the locations where a cast is needed. --- htdocs/core/lib/functions.lib.php | 2 +- htdocs/projet/list.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/htdocs/core/lib/functions.lib.php b/htdocs/core/lib/functions.lib.php index 0e4f002dfe5..0a4e1235afd 100644 --- a/htdocs/core/lib/functions.lib.php +++ b/htdocs/core/lib/functions.lib.php @@ -9925,7 +9925,7 @@ function verifCond($strToEvaluate, $onlysimplestring = '1') * @param string $onlysimplestring '0' (deprecated, do not use it anymore)=Accept all chars, * '1' (most common use)=Accept only simple string with char 'a-z0-9\s^$_+-.*>&|=!?():"\',/@';', * '2' (used for example for the compute property of extrafields)=Accept also '[]' - * @return mixed Nothing or return result of eval + * @return void|string Nothing or return result of eval (even if type can be int, it is safer to assume string and find all potential typing issues as abs(dol_eval(...)). * @see verifCond() * @phan-suppress PhanPluginUnsafeEval */ diff --git a/htdocs/projet/list.php b/htdocs/projet/list.php index fadb09ca3af..c2834b649df 100644 --- a/htdocs/projet/list.php +++ b/htdocs/projet/list.php @@ -226,7 +226,7 @@ $arrayfields = array(); foreach ($object->fields as $key => $val) { // If $val['visible']==0, then we never show the field if (!empty($val['visible'])) { - $visible = dol_eval($val['visible'], 1, 1, '1'); + $visible = (int) dol_eval($val['visible'], 1, 1, '1'); $arrayfields['p.'.$key] = array( 'label' => $val['label'], 'checked' => (($visible < 0) ? 0 : 1), From 44f4261cf90fe387379647be208ebcc4cea0a044 Mon Sep 17 00:00:00 2001 From: MDW Date: Tue, 2 Apr 2024 23:02:05 +0200 Subject: [PATCH 2/2] Fix phan notice by verifying value is array --- htdocs/core/class/commonobject.class.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php index d74538f288d..4dbd8918c7e 100644 --- a/htdocs/core/class/commonobject.class.php +++ b/htdocs/core/class/commonobject.class.php @@ -8300,7 +8300,12 @@ abstract class CommonObject $value = ''.$langs->trans("Encrypted").''; //$value = preg_replace('/./i', '*', $value); } elseif ($type == 'array') { - $value = implode('
', $value); + if (is_array($value)) { + $value = implode('
', $value); + } else { + dol_syslog(__METHOD__.' Expected array from dol_eval, but got '.gettype($value), LOG_ERR); + return 'Error unexpected result from code evaluation'; + } } else { // text|html|varchar $value = dol_htmlentitiesbr($value); }