Skip to content
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

libphoenx/scanf: Fix SEGFAULT caused by invalid processing of %n #357

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

astalke
Copy link
Contributor

@astalke astalke commented Apr 22, 2024

Description

  • Fix SEGFAULT caused by invalid processing of %n
  • MISRA

Fixes: phoenix-rtos/phoenix-rtos-project#1059

JIRA: RTOS-825

Motivation and Context

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

How Has This Been Tested?

  • Already covered by automatic testing.
  • New test added: (add PR link here).
  • Tested by hand on: (list targets here).

Checklist:

  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
  • All new and existing linter checks and tests passed.
  • My changes generate no new compilation warnings for any of the targets.

Special treatment

  • This PR needs additional PRs to work (list the PRs, preferably in merge-order).
  • I will merge this PR by myself when appropriate.

stdio/scanf.c Outdated Show resolved Hide resolved
@astalke astalke force-pushed the astalke/RTOS-825 branch 2 times, most recently from 1ea9e11 to 364b04b Compare April 22, 2024 10:52
Copy link

github-actions bot commented Apr 22, 2024

Unit Test Results

7 460 tests  ±0   6 745 ✅ ±0   39m 10s ⏱️ + 1m 59s
  420 suites ±0     715 💤 ±0 
    1 files   ±0       0 ❌ ±0 

Results for commit c7cf842. ± Comparison against base commit 81b5a09.

♻️ This comment has been updated with latest results.

@astalke astalke marked this pull request as ready for review April 22, 2024 11:40
@agkaminski
Copy link
Member

Hmm, tbh I don't get this change. Block handling %n has been moved later in the code, but I can't see any functional change in doing so. Could you please briefly explain this change?

@astalke
Copy link
Contributor Author

astalke commented Apr 29, 2024

Hmm, tbh I don't get this change. Block handling %n has been moved later in the code, but I can't see any functional change in doing so. Could you please briefly explain this change?

After reviewing my changes, I've noticed that there is an easier fix, so I will upload it soon and let CI check if it really is enough. Bug is caused by a use of continue instead of break, lack of default label in this switch and reusing c variable in the next switch statement. Basically, continue causes loop to execute one too many times, so c == 0, there is no default label to catch that and due to reusing of c in the next switch statement, we execute case CT_CHAR.

@astalke astalke marked this pull request as draft April 30, 2024 10:56
@astalke astalke marked this pull request as ready for review April 30, 2024 10:58
agkaminski
agkaminski previously approved these changes May 15, 2024
Copy link
Member

@agkaminski agkaminski left a comment

Choose a reason for hiding this comment

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

LGTM

@@ -318,7 +320,10 @@ static int scanf_parse(char *ccltab, const char *inp, int *inr, char const *fmt0
else {
*va_arg(ap, int *) = nread;
}
continue;
break;
Copy link
Member

Choose a reason for hiding this comment

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

This works because c == 'n' so it won't match any of CT_* values in the conversion switch below. Whitespace will still be skipped anyway. Maybe there should be CT_NONE value which performs no conversion is used for 'n' and default cases, together with NOSKIP?

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.

sscanf produces segfault
3 participants