Skip to content

Prevent comma abuse by implementing new rule#581

Open
jgengo wants to merge 2 commits into42school:masterfrom
jgengo:jg/add_comma
Open

Prevent comma abuse by implementing new rule#581
jgengo wants to merge 2 commits into42school:masterfrom
jgengo:jg/add_comma

Conversation

@jgengo
Copy link

@jgengo jgengo commented Dec 8, 2025

Fix: Detect comma operator abuse to bypass line count rules

Issues Fixed

  • Fixes comma operator loophole allowing students to bypass 25-line function limit
  • Prevents chaining multiple function calls/assignments on a single line using comma operators

Problem

Students were exploiting comma operators wrapped in parentheses to bypass the 25-line function limit and "too many instructions" rules:

(free(a), free(b), free(c), free(d), free(e));
return (free(a), free(b), free(c), 0);

These expressions passed validation despite chaining multiple function calls on a single line.

Solution

  • Added new rule CheckCommaOperatorAbuse that detects multiple function calls or assignments chained with comma operators within parentheses
  • Introduced COMMA_OP_ABUSE error type with message: "Comma operator used to chain multiple function calls or assignments"
  • Rule correctly distinguishes between abuse and legitimate comma usage (function parameters, for-loop expressions)

Verification Steps

Please ensure the following steps have been completed:

  • Added new tests to cover the changes.
    • Added tests/rules/samples/ko_comma_operator_abuse.c (negative test)
    • Added tests/rules/samples/ok_comma_in_params.c (positive test)
  • Fixed all broken tests.
    • All 517 tests passing
  • Ran poetry run flake8 to check for linting issues.
    • No linting issues found
  • Verified that all unit tests are passing:
    • Ran poetry run pytest to ensure unit tests pass (517/517 passed).
    • Ran poetry run tox to validate compatibility across Python versions.

Testing Results

  • ✅ Original abuse cases now caught and flagged with COMMA_OP_ABUSE error
  • ✅ Legitimate comma usage still allowed:
    • Function parameters: printf("%d %d", a, b) → OK
    • For-loop expressions → OK
  • ✅ All 517 existing tests pass with no regressions
  • ✅ New test cases validate both positive and negative scenarios

Files Changed

  • norminette/rules/check_comma_operator_abuse.py (new, 155 lines)
  • norminette/norm_error.py (modified, +1 error definition)
  • tests/rules/samples/ko_comma_operator_abuse.c (new)
  • tests/rules/samples/ko_comma_operator_abuse.out (new)
  • tests/rules/samples/ok_comma_in_params.c (new)
  • tests/rules/samples/ok_comma_in_params.out (new)

Additional Notes

The new rule scans for opening parentheses and counts:

  1. Top-level comma operators within the parentheses
  2. Function calls (IDENTIFIER followed by LPARENTHESIS pattern)
  3. Assignment operators

If 2+ function calls or 2+ assignments are chained with commas, the rule flags a COMMA_OP_ABUSE error.

The implementation properly handles nested parentheses to avoid false positives on legitimate patterns like func(a, func2(b, c)).

@jgengo jgengo changed the title Jg/add comma Prevent comma abuse by implementing new rule Dec 8, 2025
@NiumXp
Copy link
Contributor

NiumXp commented Dec 8, 2025

Hello, @jgengo!

Can you add tests for (ab(a(), b())) and ((f)(), f())?

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.

2 participants

Comments