Skip to content

[Bugfix:System] Fix crash on numeric section names#12529

Merged
bmcutler merged 5 commits into
Submitty:mainfrom
srikaaviya:fix/numeric-section-name-crash
May 1, 2026
Merged

[Bugfix:System] Fix crash on numeric section names#12529
bmcutler merged 5 commits into
Submitty:mainfrom
srikaaviya:fix/numeric-section-name-crash

Conversation

@srikaaviya

@srikaaviya srikaaviya commented Mar 4, 2026

Copy link
Copy Markdown
Contributor

Why is this Change Important & Necessary?

Fixes #12484

When an instructor tries to add a registration section named 01 (or 02, etc.) and a section 1 (or 2) already exists, the application crashes with an unhandled 500 error instead of showing a friendly message.

What is the New Behavior?

Before: Adding section 01 when section 1 already exists crashes the page with an OutputException / Twig rendering error.
Screenshot 2026-03-04 at 7 08 58 AM

After: Adding section 01 when section 1 exists shows: "Registration Section 01 already present" (same friendly message as adding a normal exact duplicate).
Screenshot 2026-03-04 at 3 12 50 PM

What steps should a reviewer take to reproduce or test the bug or new feature?

  1. Log in as an instructor and navigate to any course's Manage Sections page
  2. Ensure section 1 exists (or add it)
  3. Try to add section 01 in the "Add Registration Section" input
    [ Before fix: 500 crash with Twig OutputException
    After fix: friendly error message "Registration Section 01 already present"]

Automated Testing & Documentation

Added test cases for '01' and '001' to User::validateUserData() in

UserTester.php
— confirms leading-zero section names pass format validation
— All 66 existing

UserTester
— tests continue to pass
— No documentation changes needed.

Other information

No breaking changes or migrations. The fix is backward-compatible — existing sections are unaffected.

Two changes are made:

  1. DatabaseQueries.php
    — Pre-check in
    insertNewRegistrationSection()
    uses a PostgreSQL regex to detect numerically-equivalent existing sections ('^0*1$' matches 1, 01, 001). Returns 0 to trigger the existing "already present" UI error.

  2. RotatingSectionsForm.twig
    — Replaced in reg_sections_count|keys with Twig's is defined (exact key lookup), which prevents a crash if numerically-equivalent sections somehow both exist in the database (PHP loose comparison caused '01' == 1 to evaluate as true, passing the check but failing on key access).

@JManion32 JManion32 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @srikaaviya, thank you for your contribution!

Please update your PR title to use our required format
[<TYPE>:<MODULE>] <SUBJECT>, where <SUBJECT> is 40 chars max.
See also: https://submitty.org/developer/getting_started/make_a_pull_request

Please also use our PR description template:
https://raw.githubusercontent.com/Submitty/Submitty/refs/heads/main/.github/PULL_REQUEST_TEMPLATE.md

Since your changes affect the user interface, please include relevant screenshots before and after your change to demonstrate that you tested and debugged your PR. This will help reviewers check your work.

Be sure to link the issue that you are addressing in this PR. If the issue is completely closed with this PR say “Fixes #1234”, if it only partially addresses the issue, “work related to issue #1234

Once this pull request adheres to all Submitty conventions, we will proceed with a full review.

Additionally, it seems there are linting changes unrelated to the content of your PR. Please revert these.

@github-project-automation github-project-automation Bot moved this from Seeking Reviewer to Work in Progress in Submitty Development Mar 4, 2026
@srikaaviya srikaaviya changed the title Fix crash when creating sections with leading zeros (e.g. '01') [Bug:System] Fix crash inserting numeric section names Mar 4, 2026
When a section like '01' is added while section '1' already exists,
the system crashed with an unhandled Twig error. This is caused by two
related issues:

1. The section insertion did not check for numerically-equivalent
   existing sections. Since registration_section_id is VARCHAR, '01'
   and '1' are distinct strings, so ON CONFLICT DO NOTHING did not
   prevent the insert. However, the sorting query for sections uses
   COALESCE(SUBSTRING(...)::NUMERIC, -1), treating '01' and '1' as
   the same integer, causing crashes downstream.

   Fix: Add a pre-check in insertNewRegistrationSection() that uses a
   PostgreSQL regex to detect existing sections with the same numeric
   value (e.g. '^0*1$' matches '1', '01', '001'). Returns 0 to
   trigger the existing 'already present' error message in the UI.

2. The RotatingSectionsForm.twig template used 'in reg_sections_count|keys'
   to check section presence. Twig's 'in' operator uses PHP loose
   comparison, so '01' == 1 (integer) evaluates to true, but then
   accessing reg_sections_count['01'] fails because only integer key
   1 exists in the mapping.

   Fix: Replace 'in reg_sections_count|keys' with Twig's 'is defined'
   test, which performs an exact key lookup without type coercion.

Also adds test cases for '01' and '001' to User::validateUserData()
to document that leading-zero section names are valid format.

Fixes Submitty#12484
@srikaaviya
srikaaviya force-pushed the fix/numeric-section-name-crash branch from 7fcd9fa to 7e05d9d Compare March 4, 2026 15:29

@JManion32 JManion32 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code looks reasonable and functionality is now as intended with no page errors.
A few small things:

  1. Just leave the top comment in DatabaseQueries.php, I don't think you need to elaborate further
  2. Change the title to [Bugfix:System]. Bug is not a valid prefix.

$semester = $this->core->getConfig()->getTerm();
$course = $this->core->getConfig()->getCourse();
// Prevent numerically-equivalent sections from both being inserted (e.g. '01' vs '1').
// registration_section_id is VARCHAR so '01' != '1' at the DB level, but the sort query

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just leave the first comment, no need to fully elaborate here.

@codecov

codecov Bot commented Mar 4, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 8 lines in your changes missing coverage. Please review.
✅ Project coverage is 21.67%. Comparing base (9b00998) to head (0cfd516).
⚠️ Report is 55 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff              @@
##               main   #12529      +/-   ##
============================================
+ Coverage     21.64%   21.67%   +0.02%     
- Complexity     9654     9809     +155     
============================================
  Files           268      268              
  Lines         36245    36730     +485     
  Branches        487      490       +3     
============================================
+ Hits           7846     7960     +114     
- Misses        27916    28284     +368     
- Partials        483      486       +3     
Flag Coverage Δ
autograder 21.32% <ø> (ø)
js 2.02% <ø> (-0.03%) ⬇️
migrator 100.00% <ø> (ø)
php 20.72% <0.00%> (+0.06%) ⬆️
python_submitty_utils 80.08% <ø> (ø)
submitty_daemon_jobs 91.13% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@srikaaviya srikaaviya changed the title [Bug:System] Fix crash inserting numeric section names [BugFix:System] Fix crash inserting numeric section names Mar 4, 2026
@srikaaviya srikaaviya changed the title [BugFix:System] Fix crash inserting numeric section names [Bugfix:System] Fix crash inserting numeric section names Mar 4, 2026
@srikaaviya

Copy link
Copy Markdown
Contributor Author

@JManion32 Thanks for the review. I've removed the explanatory lines.

@github-actions github-actions Bot added the Abandoned PR - Needs New Owner No activity on PR for more than 2 weeks -- seeking new owner to complete label Mar 19, 2026
@srikaaviya
srikaaviya requested a review from JManion32 March 19, 2026 11:42
@automateprojectmangement automateprojectmangement Bot moved this from Work in Progress to In Review in Submitty Development Mar 19, 2026

@aconfo aconfo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I performed a functionality review. During my testing I tried various different combinations with section numbers to see if there was an error, including many leading 0's, many trailing 0's, numbers between 0's, and couldn't find any combination that caused the page to crash. Overall, it looks pretty good.

@github-actions github-actions Bot removed the Abandoned PR - Needs New Owner No activity on PR for more than 2 weeks -- seeking new owner to complete label Mar 25, 2026
@srikaaviya

Copy link
Copy Markdown
Contributor Author

I performed a functionality review. During my testing I tried various different combinations with section numbers to see if there was an error, including many leading 0's, many trailing 0's, numbers between 0's, and couldn't find any combination that caused the page to crash. Overall, it looks pretty good.

Thanks for the review!

@bmcutler bmcutler moved this from In Review to Ready to Merge in Submitty Development Mar 28, 2026

@williamjallen williamjallen left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better to add a database-level check constraint for this.

@srikaaviya srikaaviya changed the title [Bugfix:System] Fix crash inserting numeric section names [Bugfix:System] Fix crash on numeric section names Mar 30, 2026
@srikaaviya

Copy link
Copy Markdown
Contributor Author

It would be better to add a database-level check constraint for this.

Sure. I'm planning to add a migration file in (migration/migrator/migrations/master/) to add the database level unique index. Should I keep the PHP pre-check alongside the DB constraint or replace the PHP pre-check entirely and rely only on the DB constraint?

@bmcutler
bmcutler requested a review from williamjallen April 3, 2026 17:06
@bmcutler bmcutler moved this from Ready to Merge to Awaiting Maintainer Review in Submitty Development Apr 3, 2026
@automateprojectmangement automateprojectmangement Bot moved this from Awaiting Maintainer Review to In Review in Submitty Development Apr 3, 2026

@Rkoester47 Rkoester47 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved, the feature works properly and all tests are passing.

@bmcutler
bmcutler merged commit a78cedf into Submitty:main May 1, 2026
46 of 48 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

Creating certain sections crashes the database

6 participants