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.17%. Comparing base (b0c65bb) to head (707c925).
Report is 26 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.17%   +0.65%     
==========================================
  Files         660      662       +2     
  Lines      190900   191683     +783     
  Branches    36628    36890     +262     
==========================================
+ Hits       168995   170940    +1945     
+ Misses      15090    13612    -1478     
- Partials     6815     7131     +316     
Files with missing lines Coverage Δ
src/tcp_wrap.cc 80.40% <78.57%> (+0.08%) ⬆️

... and 98 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());

Environment* env = Environment::GetCurrent(args);
int port;
if (!args[2]->Int32Value(env->context()).To(&port)) 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.

new update here

Copy link
Member

@jazelly jazelly left a comment

Choose a reason for hiding this comment

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

can you please also add a test?

@@ -34,7 +34,6 @@

#include <cstdlib>


Copy link
Member

Choose a reason for hiding this comment

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

can you please remove these unnecessary changes

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 have added a unit test, but the unnecessary changes are clearly a result of the formatter overzealous behavior. as @jasnell said above, I restored the changes to their original state, yet every time I save the file, it reverts to the modified version.

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.

4 participants