Skip to content

check_config: validate INI datatypes (numeric / enum / duplicate) before comparison#4181

Open
tzuohann wants to merge 1 commit into
LinuxCNC:masterfrom
tzuohann:check_config-numeric-asserts
Open

check_config: validate INI datatypes (numeric / enum / duplicate) before comparison#4181
tzuohann wants to merge 1 commit into
LinuxCNC:masterfrom
tzuohann:check_config-numeric-asserts

Conversation

@tzuohann

Copy link
Copy Markdown

Problem

check_config.tcl compares [JOINT_n]/[AXIS_L] limits with Tcl >/<.
Tcl compares numerically only when both operands are valid numbers; otherwise
it silently does a string comparison. So a limit line carrying stray text —
e.g. an inline # comment — gets compared as a string.

With equal limits like:

[AXIS_A]
MAX_LIMIT = 3600000                # placeholder
[JOINT_4]
MAX_LIMIT = 3600000                  # placeholder

3600000 == 3600000, yet check_config reports
[JOINT_4]MAX_LIMIT < [AXIS_A]MAX_LIMIT — because the strings (comments +
differing whitespace) compare unequal. Pass/fail flips on trailing spaces, and
the error points at the wrong thing.

Fix

Add assert_ini_datatypes, run once right after parse_ini, before any
comparison. For each [JOINT_n]/[AXIS_L] item:

  • duplicate keys (multi-valued result) → error;
  • numeric items must satisfy string is double -strict (ints and reals);
  • enum items must match a fixed set, case-insensitively, mirroring
    LinuxCNC's caseless maps:
    • TYPE{LINEAR ANGULAR} (IniFile::mapJointType)
    • boolean home flags → {TRUE YES 1 ON FALSE NO 0 OFF} (IniFile::convertBool)
  • unknown items are left alone.

Errors name the [SECTION]ITEM and the offending value.

Behavior change

Duplicate [JOINT_]/[AXIS_] keys are now an error, replacing the warn-only
warn_for_multiple_ini_values (removed; same scope, now fatal).

Testing

Patched checker vs a working 5-joint INI and corrupted copies:

  • Accepted: valid int & real, lowercase TYPE=angular, all eight boolean spellings.
  • Rejected (clear message each): inline comment on a limit, abc, 1,000,
    1.2.3, TYPE=LINER, HOME_USE_INDEX=MAYBE, duplicate key.

Note

I'm not a software developer by trade, and this is my first day using LinuxCNC,
so I'd genuinely welcome feedback on the approach, the Tcl style, or the
numeric/enum item lists — happy to revise.

The [JOINT_n]/[AXIS_L] limit check compares values with Tcl > / <,
which silently falls back to *string* comparison when a value isn't a
valid number (e.g. an inline "# comment" left on the line). Equal
numeric limits could then spuriously fail, with a misleading message,
and the outcome depended on trailing whitespace rather than the numbers.

Add assert_ini_datatypes(), run once right after parse_ini, before any
comparison. For each [JOINT_n]/[AXIS_L] item it:
  - flags duplicate (multi-valued) keys as an error;
  - requires numeric items (limits, vels, accels, scales, PID terms,
    home vels, ...) to pass `string is double` (accepts ints and reals);
  - requires enum items to match a fixed set, case-insensitively, to
    mirror LinuxCNC's own caseless parsers:
      TYPE  -> {LINEAR ANGULAR}                 (IniFile::mapJointType)
      bools -> {TRUE YES 1 ON FALSE NO 0 OFF}   (IniFile::convertBool)
  - leaves unknown items untouched.

Each error names the [SECTION]ITEM and the offending value, so a stray
comment is reported as such instead of a phantom limit violation.

Duplicate [JOINT_]/[AXIS_] keys are now an error; this supersedes the
warn-only warn_for_multiple_ini_values, which is removed.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@grandixximo grandixximo requested a review from BsAtHome June 20, 2026 03:56
@grandixximo

grandixximo commented Jun 20, 2026

Copy link
Copy Markdown
Contributor

Thanks for digging into this, and welcome to LinuxCNC.

The inline # comment case is intentional, not a parser slip. Bertho's INI parser rewrite (April-May this year) makes # and ; comment markers only at the start of a line; after the = they are literal characters, by design. The reason is that values legitimately need them. For example the shipped qtdragon configs:

MDI_COMMAND_MACRO0 = G0 Z2;X0 Y0;Z0, Goto\nUser\nZero

The ; is a G-code command separator. If the parser stripped from #/; onward, that macro silently loses everything after Z2. So treating trailing text as a comment would break configurations that have to keep working. It is documented in docs/src/config/ini-config.adoc and pinned by tests/inifile/comments/.

Also worth knowing: the datatype, enum, and duplicate-key checks here already exist in src/emc/ini/linuxcnc_check_ini.py, the pre-run checker Bertho added in the same series. It validates TYPE, the boolean spellings, integer ranges, and duplicate keys, using the C++ parser's exposed enum functions so the accepted values stay in sync with the engine. That is probably the durable place for clearer errors, rather than reimplementing the checks in the hallib Tcl. Worth getting Bertho's read since this is his subsystem.

Given the above, I tend toward closing this, but happy to hear if I am missing something.

@BsAtHome

Copy link
Copy Markdown
Contributor

The lib/hallib/check_config.tcl script has been superseded by emc/ini/linuxcnc_check_ini.py.

Any changes to the Tcl script are in vain.

@grandixximo

Copy link
Copy Markdown
Contributor

@BsAtHome if check_config.tcl is fully superseded by linuxcnc_check_ini.py, why is it still in the tree? Should it be deleted, or is it kept around intentionally for now?

@BsAtHome

Copy link
Copy Markdown
Contributor

There were some doubts when I wrote the new checker so I left it hanging around unused. There have been no complaints, so better remove the old Tcl script.

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.

3 participants