| @@ -142,8 +142,18 @@ | ||
| 142 | 142 | |
| 143 | 143 | if ( $serial_option ) { |
| 144 | 144 | $this->up_serialize( $index, $post_id, $sku ); |
| 145 | 145 | } else { |
| 146 | + if ( ! isset( $keys[ $index ] ) ) { | |
| 147 | + /* | |
| 148 | + * The cart in the session has fewer rows than the posted data | |
| 149 | + * ( session GC, deletion in another tab, etc. ). | |
| 150 | + * Skip it, because a null key is converted to "" and would | |
| 151 | + * write a broken row into the session. | |
| 152 | + */ | |
| 153 | + $index++; | |
| 154 | + continue; | |
| 155 | + } | |
| 146 | 156 | $this->serial = $keys[ $index ]; |
| 147 | 157 | } |
| 148 | 158 | |
| 149 | 159 | if ( ! WCUtils::is_blank( $_POST['quant'][ $index ][ $post_id ][ $sku ] ) ) { |
| @@ -279,9 +289,15 @@ | ||
| 279 | 289 | $rows = array(); |
| 280 | 290 | |
| 281 | 291 | $i = 0; |
| 282 | 292 | foreach ( (array) $_SESSION['usces_cart'] as $serial => $qua ) { |
| 283 | - $rows[ $i ] = $this->key_unserialize( $serial ); | |
| 293 | + $row = $this->key_unserialize( $serial ); | |
| 294 | + if ( false === $row ) { | |
| 295 | + /* A row that cannot be restored is unusable for display and ordering, so remove it from the session. */ | |
| 296 | + unset( $_SESSION['usces_cart'][ $serial ] ); | |
| 297 | + continue; | |
| 298 | + } | |
| 299 | + $rows[ $i ] = $row; | |
| 284 | 300 | $i++; |
| 285 | 301 | } |
| 286 | 302 | |
| 287 | 303 | return $rows; |
| @@ -384,14 +400,20 @@ | ||
| 384 | 400 | /** |
| 385 | 401 | * Serial key decompression |
| 386 | 402 | * |
| 387 | 403 | * @param string $serial Serial key. |
| 388 | - * @return array | |
| 404 | + * @return array|false False if the serial key cannot be restored ( unserialize failure or invalid format ). | |
| 389 | 405 | */ |
| 390 | 406 | public function key_unserialize( $serial ) { |
| 391 | 407 | $array = wel_safe_unserialize( $serial ); |
| 392 | - $ids = array_keys( $array ); | |
| 393 | - $skus = array_keys( $array[ $ids[0] ] ); | |
| 408 | + if ( ! is_array( $array ) || empty( $array ) ) { | |
| 409 | + return false; | |
| 410 | + } | |
| 411 | + $ids = array_keys( $array ); | |
| 412 | + if ( ! is_array( $array[ $ids[0] ] ) || empty( $array[ $ids[0] ] ) ) { | |
| 413 | + return false; | |
| 414 | + } | |
| 415 | + $skus = array_keys( $array[ $ids[0] ] ); | |
| 394 | 416 | |
| 395 | 417 | $row['serial'] = $serial; |
| 396 | 418 | $row['post_id'] = $ids[0]; |
| 397 | 419 | $row['sku'] = $skus[0]; |