Skip to content

src: add args validation method#56487

Open
arxngr wants to merge 1 commit into
nodejs:mainfrom
arxngr:validation-args
Open

src: add args validation method#56487
arxngr wants to merge 1 commit into
nodejs:mainfrom
arxngr:validation-args

Conversation

@arxngr

@arxngr arxngr commented Jan 6, 2025

Copy link
Copy Markdown
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
Copy Markdown
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
Comment thread src/tcp_wrap.cc Outdated
@codecov

codecov Bot commented Jan 6, 2025

Copy link
Copy Markdown

Codecov Report

❌ 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 4264 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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Comment thread src/tcp_wrap.cc Outdated
Comment thread 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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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());

@arxngr arxngr requested a review from jasnell January 7, 2025 02:09
Comment thread src/tcp_wrap.cc
Environment* env = Environment::GetCurrent(args);
int port;
if (!args[2]->Int32Value(env->context()).To(&port)) return;

Copy link
Copy Markdown
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

@jazelly jazelly left a comment

Copy link
Copy Markdown
Member

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?

Comment thread src/tcp_wrap.cc

#include <cstdlib>


Copy link
Copy Markdown
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
Copy Markdown
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.

@arxngr arxngr requested a review from jazelly January 9, 2025 18:09
@arxngr

arxngr commented Jan 15, 2025

Copy link
Copy Markdown
Contributor Author

Hi guys, I need help for re-review my PR here :)

@github-actions

Copy link
Copy Markdown
Contributor

This pull request has been marked as stale due to 210 days of inactivity.
It will be automatically closed in 30 days if no further activity occurs. If this is still relevant, please leave a comment or update it to keep it open.

@github-actions github-actions Bot added stale and removed stale labels Apr 24, 2026
@Ethan-Arrowood

Copy link
Copy Markdown
Contributor

Hello, I'm doing some backlog hygiene on all PRs related to networking and streaming modules. There hasn't been any activity here for quite a while so I'm going to mark it stalled. It will auto-close in ~30 days. This is not a rejection; if you've like to pick it back up, rebase onto latest main and give me a ping.

Thank you!

@Ethan-Arrowood Ethan-Arrowood added the stalled Issues and PRs that are stalled. label Jul 15, 2026
@github-actions

Copy link
Copy Markdown
Contributor

This issue/PR was marked as stalled, it will be automatically closed in 30 days. If it should remain open, please leave a comment explaining why it should remain open.

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. stalled Issues and PRs that are stalled.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants