Skip to content

Potential fix for code scanning alert no. 6: Incorrect return-value check for a 'scanf'-like function#1120

Merged
zhblue merged 1 commit intomasterfrom
alert-autofix-6
Jan 21, 2026
Merged

Potential fix for code scanning alert no. 6: Incorrect return-value check for a 'scanf'-like function#1120
zhblue merged 1 commit intomasterfrom
alert-autofix-6

Conversation

@zhblue
Copy link
Copy Markdown
Owner

@zhblue zhblue commented Jan 21, 2026

Potential fix for https://github.com/zhblue/hustoj/security/code-scanning/6

In general, calls to scanf-like functions should compare their return value to the exact number of expected input items, not just test for nonzero. Here, fscanf(fpname,"%s",noip_file_name) is expected to read exactly one string, so the correct condition is if (fscanf(...) == 1) (or equivalently, store the result in a variable and check r == 1 or r < 1). This ensures that neither partial reads nor EOF are treated as success.

To fix the specific problem without changing functionality, update the if condition on line 2134 to explicitly compare the return value of fscanf against 1. The surrounding logic (debug prints, basename usage, execute_cmd, and fclose) should remain unchanged. No new methods, imports, or definitions are required, since we are only tightening the return-value check semantics on an existing standard-library call.

Concretely, in trunk/core/judge_client/judge_client.cc, locate the block around the second use of fpname (the one for output.name) and change:

if(fscanf(fpname,"%s",noip_file_name)){

to:

if (fscanf(fpname, "%s", noip_file_name) == 1) {

leaving indentation and the rest of the function intact.

Suggested fixes powered by Copilot Autofix. Review carefully before merging.

…heck for a 'scanf'-like function

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
@zhblue zhblue marked this pull request as ready for review January 21, 2026 01:10
Copilot AI review requested due to automatic review settings January 21, 2026 01:10
@zhblue zhblue merged commit ea6e234 into master Jan 21, 2026
10 checks passed
Copy link
Copy Markdown
Contributor

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 addresses a code scanning security alert by fixing an incorrect return-value check for a fscanf function call. The change ensures that the return value is explicitly compared to the expected number of items (1) rather than just testing for nonzero, which could incorrectly treat EOF or partial reads as success.

Changes:

  • Updated the fscanf return value check on line 2134 from an implicit nonzero test to an explicit == 1 comparison
  • Added proper code formatting with consistent spacing

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

zhblue added a commit that referenced this pull request Jan 21, 2026
* Refactor contest ranking logic in contestrank.xls.php

* Sanitize nicknames with additional prefix check

* Remove '../' from file names in problem import

Sanitize file names by removing '../' to prevent directory traversal vulnerabilities.

* Add exit to restrict access to proxy.php

Added exit statement to prevent unauthorized access.

* Update problem_import_hoj.php

* Update problem_import_hydro.php

* Update problem_import_md.php

* Update problem_import_qduoj.php

* Update problem_import_syzoj.php

* Update problem_import_tyvj.php

* Update problem_import_unkownoj.php

* Update problem_import_hoj.php

* Update my_func.inc.php

* Update problem_import.php

* Update problem_import_qduoj.php

* Update problem_import_hoj.php

* Update problem_import_qduoj.php

* Update difficulty control standards in common.php

* Update mail.php

* Fix HTML form attributes in mail.php

* Update mail.php

* Potential fix for code scanning alert no. 4: Time-of-check time-of-use filesystem race condition (#1118)

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>

* Potential fix for code scanning alert no. 6: Incorrect return-value check for a 'scanf'-like function (#1120)

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>

---------

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
zhblue added a commit that referenced this pull request Jan 21, 2026
* Refactor contest ranking logic in contestrank.xls.php

* Sanitize nicknames with additional prefix check

* Remove '../' from file names in problem import

Sanitize file names by removing '../' to prevent directory traversal vulnerabilities.

* Add exit to restrict access to proxy.php

Added exit statement to prevent unauthorized access.

* Update problem_import_hoj.php

* Update problem_import_hydro.php

* Update problem_import_md.php

* Update problem_import_qduoj.php

* Update problem_import_syzoj.php

* Update problem_import_tyvj.php

* Update problem_import_unkownoj.php

* Update problem_import_hoj.php

* Update my_func.inc.php

* Update problem_import.php

* Update problem_import_qduoj.php

* Update problem_import_hoj.php

* Update problem_import_qduoj.php

* Update difficulty control standards in common.php

* Update mail.php

* Fix HTML form attributes in mail.php

* Update mail.php

* Potential fix for code scanning alert no. 4: Time-of-check time-of-use filesystem race condition (#1118)

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>

* Potential fix for code scanning alert no. 6: Incorrect return-value check for a 'scanf'-like function (#1120)

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>

* Potential fix for code scanning alert no. 5: Incorrect return-value check for a 'scanf'-like function (#1119)

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>

* Update date formatting in submit.php

Replaced strftime with date for better date formatting.

* Update submit.php

---------

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
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