about summary refs log tree commit diff
path: root/sourcecodes/network_layout_inv_2.php
diff options
context:
space:
mode:
authorClaude Sonnet 52026-08-17 19:32:03 +0000
committerFrederick Muriuki Muriithi2026-08-17 14:53:21 -0500
commita1f64bda77d74026d663eca91b975a6ff869d9a3 (patch)
tree59936fff9d795fbe76ac1924c34741c83d2808ae /sourcecodes/network_layout_inv_2.php
parent9cae7ea19c7c744a1ed2bd997571ec1b516a0d32 (diff)
downloadBNW-a1f64bda77d74026d663eca91b975a6ff869d9a3.tar.gz
Guard arithmetic on file/shell-output-derived values against non-numeric strings
PHP 8 throws a fatal TypeError for arithmetic (+ - * /) on a
non-numeric string ("Unsupported operand types"); PHP 7.4 only warned
and treated it as 0. Several files perform arithmetic directly on
values parsed from intermediate state files (nnode.txt, nrows.txt,
structure_input*.txt) or from `dot -Tplain` output, with no
is_numeric() check — safe as long as those files are well-formed, but
a crash risk if a file is missing/empty/truncated or a `dot` output
line doesn't parse as expected.

Affected: runtime_check.php ($node, $nrows), remove_variables.php and
create_tiers_gom_part1.php ($maxplist=$node-1), mat_structure.php
(matrix cell multiplication), and the four network_layout_{evd,inv}[,_2].php
files ($r_index=$node+N and the dot-output-derived $cell arithmetic in
the _2 variants).

Fix pattern: coalesce to 0 via `is_numeric($x) ? $x : 0` before the
arithmetic, matching PHP 7.4's original fallback-to-0 behavior instead
of crashing.

Verified empirically against PHP 8.3.28 with runtime_check.php and
mat_structure.php: previously, a missing/empty source file caused
  TypeError: Unsupported operand types: string - int (or string * int)
after the fix, the same missing-file scenario produces only the
pre-existing "undefined array key" warnings (same as PHP 7.4) and the
function completes and returns a value instead of crashing.

Reviewed by: Frederick M Muriithi <fredmanglis@gmail.com>
Diffstat (limited to 'sourcecodes/network_layout_inv_2.php')
-rw-r--r--sourcecodes/network_layout_inv_2.php11
1 files changed, 7 insertions, 4 deletions
diff --git a/sourcecodes/network_layout_inv_2.php b/sourcecodes/network_layout_inv_2.php
index e6e9b128..cedb3782 100644
--- a/sourcecodes/network_layout_inv_2.php
+++ b/sourcecodes/network_layout_inv_2.php
@@ -191,6 +191,7 @@ exit;
 }
 
 $node=trim($str_arrmat[1]);
+$node=is_numeric($node) ? $node : 0;
 
 
 
@@ -314,6 +315,7 @@ $str_arrmat_old=explode("\n",$matrix1_old);
 $datamat_old=array();
 $data_read_old=array();
 $node_old=trim($str_arrmat_old[0]);
+$node_old=is_numeric($node_old) ? $node_old : 0;
 $r_index_old=$node_old+2;
 
 for($i=0;$i<$node_old;$i++)
@@ -410,7 +412,7 @@ foreach($str_arrname as $row)
                 if($j==2)
                    $ID_data[$ii][$k]=$grviz_name_list[$cell];
                 else
-                   $ID_data[$ii][$k]=round($cell/10*900);
+                   $ID_data[$ii][$k]=round((is_numeric($cell) ? $cell : 0)/10*900);
                // echo $ID_data[$ii][$k];
                 //echo '&nbsp';
 
@@ -491,15 +493,16 @@ foreach($str_arrname as $row)
              else if($j==4 && $flag==1)
              {
                   $edge_data[$ii][4]=$cell;
-                  $number_of_point=$cell*2;
+                  $number_of_point=(is_numeric($cell) ? $cell : 0)*2;
                   $index=5;
              }
              else if($j>4 && $number_of_point>0 && $flag==1)
              {
+                $cell_num=is_numeric($cell) ? $cell : 0;
                 if(($j%2)!=0)
-                  $edge_data[$ii][$index]=round($cell/10*900)+60;//+30+30;
+                  $edge_data[$ii][$index]=round($cell_num/10*900)+60;//+30+30;
                 else
-                  $edge_data[$ii][$index]=round($cell/10*900)+48;//+18+30;
+                  $edge_data[$ii][$index]=round($cell_num/10*900)+48;//+18+30;
 
                 $number_of_point--;
                 $index++;