-
-
Notifications
You must be signed in to change notification settings - Fork 403
feat: support regex for objective problem #1049
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: support regex for objective problem #1049
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR adds support for regex pattern matching in objective problem types by introducing an optional third parameter to the answer configuration that specifies the matching mode as 'regex'.
- Extends the answer configuration type to include an optional third string parameter for matching mode
- Implements regex matching logic when the mode is set to 'regex'
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
File | Description |
---|---|
packages/hydrojudge/src/judge/objective.ts | Adds regex matching logic and updates type annotation for answer configuration |
packages/common/types.ts | Updates the answer configuration type to include optional third string parameter |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
} else if (ansInfo[2] === 'regex') { | ||
const regex = new RegExp(stdAns); | ||
if (regex.test(usrAns)) report(STATUS.STATUS_ACCEPTED, fullScore, 'Correct'); | ||
else report(STATUS.STATUS_WRONG_ANSWER, 0, 'Incorrect'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Creating a RegExp from user-controlled data without validation can lead to ReDoS (Regular Expression Denial of Service) attacks. Consider validating the regex pattern or adding a timeout mechanism to prevent malicious patterns from causing performance issues.
Copilot uses AI. Check for mistakes.
else if (ans.size && Set.isSuperset(stdSet, ans)) report(STATUS.STATUS_WRONG_ANSWER, Math.floor(fullScore / 2), 'Partially Correct'); | ||
else report(STATUS.STATUS_WRONG_ANSWER, 0, 'Incorrect'); | ||
} else if (ansInfo[2] === 'regex') { | ||
const regex = new RegExp(stdAns); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The RegExp constructor can throw a SyntaxError if the pattern is invalid. This should be wrapped in a try-catch block to handle malformed regex patterns gracefully.
const regex = new RegExp(stdAns); | |
let regex: RegExp; | |
try { | |
regex = new RegExp(stdAns); | |
} catch (e) { | |
report(STATUS.STATUS_WRONG_ANSWER, 0, 'Invalid regex pattern'); | |
continue; | |
} |
Copilot uses AI. Check for mistakes.
judge_extra_files?: string[]; | ||
detail?: DetailType | boolean; | ||
answers?: Record<string, [string | string[], number]>; | ||
answers?: Record<string, [string | string[], number, string]>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The third parameter should be optional since existing configurations may not include it. Consider using [string | string[], number, string?]
to maintain backward compatibility.
answers?: Record<string, [string | string[], number, string]>; | |
answers?: Record<string, [string | string[], number, string?]>; |
Copilot uses AI. Check for mistakes.
support regex for objective problem
problem config:
matches