Skip to content

Conversation

gtn1024
Copy link
Contributor

@gtn1024 gtn1024 commented Sep 20, 2025

support regex for objective problem

problem config:

type: objective
answers:
  '1':
  - .*(IntelliJ IDEA|Eclipse IDE|Electron:).*
  - 100
  - regex

matches

版本: 1.104.1 (Universal)
提交: 0f0d87fa9e96c856c5212fc86db137ac0d783365
日期: 2025-09-17T23:36:24.973Z
Electron: 37.3.1
ElectronBuildId: 12404162
Chromium: 138.0.7204.235
Node.js: 22.18.0
V8: 13.8.258.31-electron.0
OS: Darwin arm64 25.0.0

@Copilot Copilot AI review requested due to automatic review settings September 20, 2025 09:12
Copy link

@Copilot Copilot AI left a 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.

Comment on lines +72 to +75
} 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');
Copy link
Preview

Copilot AI Sep 20, 2025

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);
Copy link
Preview

Copilot AI Sep 20, 2025

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.

Suggested change
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]>;
Copy link
Preview

Copilot AI Sep 20, 2025

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.

Suggested change
answers?: Record<string, [string | string[], number, string]>;
answers?: Record<string, [string | string[], number, string?]>;

Copilot uses AI. Check for mistakes.

@gtn1024 gtn1024 deleted the support-regex-for-objective-problem branch September 29, 2025 01:05
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