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

src: add args validation method #56487

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

ardinugrxha
Copy link
Contributor

Problem:

When fewer than three arguments are passed or if the third argument is not of type Uint32, the program attempts to access invalid memory or cast incorrect types. This leads to undefined behavior, including potential crashes [1] 2385420 IOT instruction (core dumped) node and not clear what does it mean

So I think we need to validate the args first before we do some assertions below. we can also do same thing in another place that doesn't have the validator

ref issue: #56367

@nodejs-github-bot
Copy link
Collaborator

Review requested:

  • @nodejs/net

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run. net Issues and PRs related to the net subsystem. labels Jan 6, 2025
src/tcp_wrap.cc Outdated Show resolved Hide resolved
Copy link

codecov bot commented Jan 6, 2025

Codecov Report

Attention: Patch coverage is 78.57143% with 12 lines in your changes missing coverage. Please review.

Project coverage is 89.12%. Comparing base (b0c65bb) to head (5db5ac6).
Report is 14 commits behind head on main.

Files with missing lines Patch % Lines
src/tcp_wrap.cc 78.57% 6 Missing and 6 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #56487      +/-   ##
==========================================
+ Coverage   88.52%   89.12%   +0.60%     
==========================================
  Files         660      662       +2     
  Lines      190900   191576     +676     
  Branches    36628    36871     +243     
==========================================
+ Hits       168995   170743    +1748     
+ Misses      15090    13701    -1389     
- Partials     6815     7132     +317     
Files with missing lines Coverage Δ
src/tcp_wrap.cc 80.40% <78.57%> (+0.08%) ⬆️

... and 76 files with indirect coverage changes

src/tcp_wrap.cc Outdated Show resolved Hide resolved
src/tcp_wrap.cc Outdated
CHECK(args[2]->IsUint32());
Environment* env = Environment::GetCurrent(args);
int backlog;
if (!args[2]->Int32Value(env->context()).To(&backlog)) return;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

i made an update here, so i think we should validate here instead of throwing args error

Copy link
Member

Choose a reason for hiding this comment

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

you have this setting to backlog as an int32 but we use it as port below, cast as a uint32 then converted to an int with port below. Where is the term backlog coming from here? either way, should make this consistent here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yep, i think the backlog should be a "port" here, and I think we don't need cast again since

 int port;
  if (!args[2]->Int32Value(env->context()).To(&port)) return;

already casted to int right (?)

and we don't need this anymore
int port = static_cast<int>(args[2].As<Uint32>()->Value());

@ardinugrxha ardinugrxha requested a review from jasnell January 7, 2025 02:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run. net Issues and PRs related to the net subsystem.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants