Skip to content

fix(thold): substitution loop, empty THOLD_DOWNTIME, time-based graph tags and other small defects - #901

Merged
TheWitness merged 11 commits into
Cacti:developfrom
somethingwithproof:fix/low-hanging-fruit
Dec 3, 2026
Merged

TheWitness merged 11 commits into
Cacti:developfrom
somethingwithproof:fix/low-hanging-fruit

Conversation

@somethingwithproof

Copy link
Copy Markdown
Member

Closes #900.

Small, local fixes found with PHPStan level 8. The threshold evaluator (thold_check_threshold) and the functions changed by #892 and #898 are untouched, and so is CHANGELOG.md (both open PRs edit it; the entry can be added at merge).

Fixes

  • thold_substitute_custom_data(): stop the scan at an unterminated |custom_ token instead of restarting it forever.
  • Device Down Command: pass the previous up-time in THOLD_DOWNTIME instead of an unset variable.
  • |thold:...| graph tags: resolve the time_* bounds for time-based thresholds.
  • Edit pages: list no data sources instead of failing with a TypeError when the data source has no items.
  • Assign variables that only some branches set (device format file, baseline CF reference, thold_mail sender name, template import name), and return the text unchanged when there is no data source id.
  • Redirect an empty selection to thold.php instead of the missing tholds.php.
  • Show time_warning_* bounds for time-based templates in the template list.
  • Treat an imported template without a hash as new instead of reading an undefined $save['id'].

Cleanup (no behaviour change)

  • Fix malformed and wrong PHPDoc blocks; remove unreachable code.

Tests

  • New Pest tests for each behavioural fix except the edit-page and template-list changes, which live in web pages the unit harness cannot load. Each new test fails without its fix.
  • Pest: 631 passed, 1 skipped on PHP 8.3. Patch coverage 100%. PHPStan on plugin sources: 529 to 447 errors at level 6, 577 to 493 at level 8.

thold_substitute_custom_data() looks for the closing escape after each
|custom_ token. When there is none, strpos() returns false, the next scan
starts again at offset 0 and finds the same token, so the loop never ends
and the process runs out of memory. A trigger command or notification
text with a stray "|custom_x" therefore hung the poller or daemon.

Stop scanning at an unterminated token and leave it as typed.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
The down branch of thold_update_host_status() exported THOLD_DOWNTIME
from $last_time_duration, a variable that is never assigned. The command
always received an empty value and the poller logged an undefined
variable warning for every device that went down. The email for the
same event already uses $last_state_duration; export that.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
thold_rrd_graph_graph_options() reads a tagged bound from the time_*
columns for time-based thresholds, but the branch tested thold_type == 1,
which the preceding Hi/Low and Baseline branch already handles. Time-based
thresholds (type 2) never reached it, $value was left undefined, and the
graph item carrying the tag was dropped from the graph. Test for type 2.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
Four functions read a variable that only some branches assign:

- thold_get_device_notification_format_file() read $notify_format_file
  when the device event had no notification list.
- thold_check_baseline() read $ref_value for the CF deviation types when
  bl_cf held none of AVG/MIN/MAX/LAST.
- thold_mail() read $from_name when the caller passed its own sender.
- thold_template_import() kept $tname across templates, so a template
  without a name was reported under the previous template's name, or
  raised a warning when it came first.

Each now starts from an empty value. The test bootstrap gains xml2array()
and db_column_exists() shims, and the mailer() shim records the sender.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
thold_substitute_custom_data() returned null when $local_data_id was
empty, so thold_expand_string() went on with a null string instead of
the text it passed in. With no data source there is nothing to look up;
return the input as given.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
Several docblocks used "@PARAM array - text" and "@return - text", which
PHPDoc parsers reject, so PHPStan ignored them. Give each tag a type and
a variable name, and correct the ones that disagreed with the code:
thold_set_environ() returns the queued environment, the delay functions
return nothing, and thold_cli_autocreate() receives the id lists already
split into arrays. No code changes.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
- Drop the break statements that follow a return or exit.
- Drop the $banner block on the threshold edit page; nothing has set
  $banner since messages moved to raise_message().
- Stop printing the result of html_host_filter(), which prints its own
  markup and returns nothing.
- Return nothing from thold_wizard(), which is declared void and whose
  callers ignore the result.
- Pass html_header_sort() its default colspan of 1 instead of false.
- Return an empty label from get_bl_type() for an unknown type instead of
  falling off the end of the function.

No behaviour change.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
thold_edit() and template_edit() build the "Data Sources" hint from
$dsname[], which is only assigned inside the loop over the data source's
rows. With no rows, implode() received an undefined variable and PHP 8
threw a TypeError, so the edit page for a threshold or template whose
data source has no items failed to render. Start from an empty list.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
thold.php sent an action submitted with no threshold selected to
tholds.php, which does not exist, so the user got a missing-page error
instead of the list and the 'select at least one' message. Redirect to
thold.php like the page's other redirects, and test that every Location
redirect in the plugin names an existing page.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
The template list showed thold_warning_hi/low as the warning bounds of a
time-based template. Time-based templates keep them in time_warning_hi
and time_warning_low, as the alert bounds in the same branch already do.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
thold_template_import() sets $save['id'] only when the template has a
hash, then read it to choose between 'Imported' and 'Updated', raising an
undefined-key warning. Without a hash there is nothing to match, and
sql_save() inserts a new row, so report it as an import.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
@sonarqubecloud

sonarqubecloud Bot commented Dec 3, 2026

Copy link
Copy Markdown

@TheWitness
TheWitness merged commit c49a020 into Cacti:develop Dec 3, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fix substitution loop, empty THOLD_DOWNTIME, dropped time-based graph tags and other small defects

2 participants