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

util: remove instance check on empty object #35754

Closed
wants to merge 3 commits into from

Conversation

baylesa-dev
Copy link

@baylesa-dev baylesa-dev commented Oct 22, 2020

Remove a useless check on Object descriptor. It seems like js browsers engine dont do this check.

Fixes: #35730

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • commit message follows commit guidelines

Remove a useless check on Object descriptor. It seems like js browsers engine dont do this check.

Fixes: nodejs#35730
@nodejs-github-bot nodejs-github-bot added the util Issues and PRs related to the built-in util module. label Oct 22, 2020
Remove a useless check on Object descriptor. It seems like js browsers engine dont do this check.

Fixes: nodejs#35730
@aduh95
Copy link
Contributor

aduh95 commented Oct 22, 2020

Hi @baylesa-dev 👋 Thanks for opening this! Could you add a test to ensure the related issue doesn't happen again if we make other changes to this code in the future?

Write test to prevent this bug in future
@aduh95 aduh95 added the request-ci Add this label to start a Jenkins CI on a PR. label Oct 22, 2020
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Oct 22, 2020
@nodejs-github-bot
Copy link
Collaborator

@codecov-io
Copy link

Codecov Report

Merging #35754 into master will decrease coverage by 0.06%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master   #35754      +/-   ##
==========================================
- Coverage   87.92%   87.85%   -0.07%     
==========================================
  Files         477      477              
  Lines      113090   113088       -2     
  Branches    24632    24606      -26     
==========================================
- Hits        99431    99356      -75     
- Misses       7948     8020      +72     
- Partials     5711     5712       +1     
Impacted Files Coverage Δ
lib/internal/util/inspect.js 93.41% <100.00%> (-2.26%) ⬇️
src/connect_wrap.h 25.00% <0.00%> (-75.00%) ⬇️
lib/internal/repl/history.js 88.16% <0.00%> (-4.15%) ⬇️
src/inspector_agent.cc 83.88% <0.00%> (-0.86%) ⬇️
lib/internal/encoding.js 84.06% <0.00%> (-0.53%) ⬇️
src/node_http_parser.cc 80.75% <0.00%> (-0.45%) ⬇️
lib/internal/url.js 96.12% <0.00%> (-0.41%) ⬇️
lib/_http_server.js 97.83% <0.00%> (-0.11%) ⬇️
src/module_wrap.cc 70.84% <0.00%> (+0.24%) ⬆️

@baylesa-dev
Copy link
Author

@aduh95 Do you know why some checks weren't successful ? What could I do to resolve them?

@BridgeAR
Copy link
Member

BridgeAR commented Oct 23, 2020

@baylesa-dev the check is not useless. It's a safeguard to find the correct prototype. To fix the issue, we'll have to wrap the instanceof check in a try / catch and return false in the error case.

This should be a full fix:

diff --git a/lib/internal/util/inspect.js b/lib/internal/util/inspect.js
index e6787760fe..c281d36630 100644
--- a/lib/internal/util/inspect.js
+++ b/lib/internal/util/inspect.js
@@ -534,6 +534,14 @@ function getEmptyFormatArray() {
   return [];
 }
 
+function isInstanceof(object, proto) {
+  try {
+    return object instanceof proto;
+  } catch {
+    return false;
+  }
+}
+
 function getConstructorName(obj, ctx, recurseTimes, protoProps) {
   let firstProto;
   const tmp = obj;
@@ -542,7 +550,7 @@ function getConstructorName(obj, ctx, recurseTimes, protoProps) {
     if (descriptor !== undefined &&
         typeof descriptor.value === 'function' &&
         descriptor.value.name !== '' &&
-        tmp instanceof descriptor.value) {
+        isInstanceof(tmp, descriptor.value)) {
       if (protoProps !== undefined &&
          (firstProto !== obj ||
          !builtInObjects.has(descriptor.value.name))) {
diff --git a/test/parallel/test-util-inspect.js b/test/parallel/test-util-inspect.js
index 70f2ff6432..a8caf6f891 100644
--- a/test/parallel/test-util-inspect.js
+++ b/test/parallel/test-util-inspect.js
@@ -2866,6 +2866,17 @@ assert.strictEqual(
   );
 }
 
+// Regression test for https://github.com/nodejs/node/issues/35730
+{
+  function Func() {}
+  Func.prototype = null;
+  const object = {};
+  object.constructor = Func;
+
+  assert.strictEqual(util.inspect(object), '{ constructor: [Function: Func] }');
+}
+
 // Test changing util.inspect.colors colors and aliases.
 {
   const colors = util.inspect.colors;

descriptor.value.name !== '' &&
tmp instanceof descriptor.value) {
descriptor.value.name !== '') {
Copy link
Contributor

Choose a reason for hiding this comment

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

This change broke tests inspecting the prototype object, which is not an instance of its constructor.

@Trott
Copy link
Member

Trott commented Nov 8, 2020

Should this PR be closed or is work ongoing?

nodejs-github-bot pushed a commit that referenced this pull request Dec 12, 2020
Signed-off-by: Ruben Bridgewater <ruben@bridgewater.de>

Fixes: #36151

PR-URL: #36178
Fixes: #35730
Refs: #35754
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Guy Bedford <guybedford@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
nodejs-github-bot pushed a commit that referenced this pull request Dec 12, 2020
Signed-off-by: Ruben Bridgewater <ruben@bridgewater.de>

Fixes: #35730

PR-URL: #36178
Fixes: #36151
Refs: #35754
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Guy Bedford <guybedford@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
@ExE-Boss
Copy link
Contributor

ExE-Boss commented Dec 12, 2020

This PR has been superseded by #36178, which has now been merged.

BridgeAR
BridgeAR previously approved these changes Dec 13, 2020
@BridgeAR BridgeAR dismissed their stale review December 13, 2020 00:38

Wrong button

@BridgeAR BridgeAR closed this Dec 13, 2020
targos pushed a commit that referenced this pull request Dec 21, 2020
Signed-off-by: Ruben Bridgewater <ruben@bridgewater.de>

Fixes: #36151

PR-URL: #36178
Fixes: #35730
Refs: #35754
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Guy Bedford <guybedford@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
targos pushed a commit that referenced this pull request Dec 21, 2020
Signed-off-by: Ruben Bridgewater <ruben@bridgewater.de>

Fixes: #35730

PR-URL: #36178
Fixes: #36151
Refs: #35754
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Guy Bedford <guybedford@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Pezmc pushed a commit to Pezmc/node that referenced this pull request Jan 27, 2021
Signed-off-by: Ruben Bridgewater <ruben@bridgewater.de>

Fixes: nodejs#36151

PR-URL: nodejs#36178
Fixes: nodejs#35730
Refs: nodejs#35754
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Guy Bedford <guybedford@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Pezmc pushed a commit to Pezmc/node that referenced this pull request Jan 27, 2021
Signed-off-by: Ruben Bridgewater <ruben@bridgewater.de>

Fixes: nodejs#35730

PR-URL: nodejs#36178
Fixes: nodejs#36151
Refs: nodejs#35754
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Guy Bedford <guybedford@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Pezmc pushed a commit to Pezmc/node that referenced this pull request Jan 27, 2021
Backport-PR-URL: nodejs#37100

Signed-off-by: Ruben Bridgewater <ruben@bridgewater.de>

Fixes: nodejs#36151

PR-URL: nodejs#36178
Fixes: nodejs#35730
Refs: nodejs#35754
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Guy Bedford <guybedford@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Pezmc pushed a commit to Pezmc/node that referenced this pull request Jan 27, 2021
Backport-PR-URL: nodejs#37100

Signed-off-by: Ruben Bridgewater <ruben@bridgewater.de>

Fixes: nodejs#35730

PR-URL: nodejs#36178
Fixes: nodejs#36151
Refs: nodejs#35754
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Guy Bedford <guybedford@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Pezmc pushed a commit to Pezmc/node that referenced this pull request Jan 29, 2021
Backport-PR-URL: nodejs#37100

Signed-off-by: Ruben Bridgewater <ruben@bridgewater.de>

Fixes: nodejs#36151

PR-URL: nodejs#36178
Fixes: nodejs#35730
Refs: nodejs#35754
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Guy Bedford <guybedford@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Pezmc pushed a commit to Pezmc/node that referenced this pull request Jan 29, 2021
Backport-PR-URL: nodejs#37100

Signed-off-by: Ruben Bridgewater <ruben@bridgewater.de>

Fixes: nodejs#35730

PR-URL: nodejs#36178
Fixes: nodejs#36151
Refs: nodejs#35754
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Guy Bedford <guybedford@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Pezmc pushed a commit to Pezmc/node that referenced this pull request Jan 29, 2021
Backport-PR-URL: nodejs#37100

Signed-off-by: Ruben Bridgewater <ruben@bridgewater.de>

Fixes: nodejs#36151

PR-URL: nodejs#36178
Fixes: nodejs#35730
Refs: nodejs#35754
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Guy Bedford <guybedford@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Pezmc pushed a commit to Pezmc/node that referenced this pull request Jan 29, 2021
Backport-PR-URL: nodejs#37100

Signed-off-by: Ruben Bridgewater <ruben@bridgewater.de>

Fixes: nodejs#35730

PR-URL: nodejs#36178
Fixes: nodejs#36151
Refs: nodejs#35754
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Guy Bedford <guybedford@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Pezmc pushed a commit to Pezmc/node that referenced this pull request Jan 29, 2021
Backport-PR-URL: nodejs#37100

Signed-off-by: Ruben Bridgewater <ruben@bridgewater.de>

Fixes: nodejs#36151

PR-URL: nodejs#36178
Fixes: nodejs#35730
Refs: nodejs#35754
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Guy Bedford <guybedford@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Pezmc pushed a commit to Pezmc/node that referenced this pull request Jan 29, 2021
Backport-PR-URL: nodejs#37100

Signed-off-by: Ruben Bridgewater <ruben@bridgewater.de>

Fixes: nodejs#35730

PR-URL: nodejs#36178
Fixes: nodejs#36151
Refs: nodejs#35754
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Guy Bedford <guybedford@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
BethGriggs pushed a commit that referenced this pull request Feb 5, 2021
Signed-off-by: Ruben Bridgewater <ruben@bridgewater.de>

Backport-PR-URL: #37100
PR-URL: #36178
Fixes: #35730
Fixes: #36151
Refs: #35754
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Guy Bedford <guybedford@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
BethGriggs pushed a commit that referenced this pull request Feb 5, 2021
Signed-off-by: Ruben Bridgewater <ruben@bridgewater.de>

Backport-PR-URL: #37100
PR-URL: #36178
Fixes: #35730
Fixes: #36151
Refs: #35754
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Guy Bedford <guybedford@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
BethGriggs pushed a commit that referenced this pull request Feb 5, 2021
Signed-off-by: Ruben Bridgewater <ruben@bridgewater.de>

Backport-PR-URL: #37100
PR-URL: #36178
Fixes: #35730
Fixes: #36151
Refs: #35754
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Guy Bedford <guybedford@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
util Issues and PRs related to the built-in util module.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Function has non-object prototype 'null' in instanceof check, when I use node but it is fine in Browser
7 participants