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: fix UB in InternalModuleReadFile() #16871

Merged
merged 0 commits into from
Nov 9, 2017

Conversation

bnoordhuis
Copy link
Member

@bnoordhuis bnoordhuis commented Nov 7, 2017

&vec[0] is undefined behavior when vec.size() == 0.

It is mostly academic because package.json files are not usually empty
and because with most STL implementations it decays to something that
is legal C++ as long as the result is not dereferenced, but better safe
than sorry.

Note that the tests don't actually fail because of that, I added them
as sanity checks.

Split off from #15767 where it already had a couple of LGTMs.

CI: https://ci.nodejs.org/job/node-test-pull-request/11283/

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. fs Issues and PRs related to the fs subsystem / file system. labels Nov 7, 2017

strictEqual(internalModuleReadFile('nosuchfile'), undefined);
strictEqual(internalModuleReadFile(fixturesDir + '/empty.txt'), '');
strictEqual(internalModuleReadFile(fixturesDir + '/empty-with-bom.txt'), '');
Copy link
Member

@jasnell jasnell Nov 8, 2017

Choose a reason for hiding this comment

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

unblocking nit: for consistency...

const fixtures = require('../common/fixtures');
/* ... */
strictEqual(internalModuleReadFile(fixtures.path('empty-with-bom.txt'));

@bnoordhuis
Copy link
Member Author

Updated test. New CI: https://ci.nodejs.org/job/node-test-pull-request/11322/

@bnoordhuis bnoordhuis closed this Nov 9, 2017
@bnoordhuis bnoordhuis deleted the fix-module-ub branch November 9, 2017 11:59
@bnoordhuis bnoordhuis merged commit f823d38 into nodejs:master Nov 9, 2017
evanlucas pushed a commit that referenced this pull request Nov 13, 2017
`&vec[0]` is undefined behavior when `vec.size() == 0`.

It is mostly academic because package.json files are not usually empty
and because with most STL implementations it decays to something that
is legal C++ as long as the result is not dereferenced, but better safe
than sorry.

Note that the tests don't actually fail because of that, I added them
as sanity checks.

PR-URL: #16871
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Franziska Hinkelmann <[email protected]>
Reviewed-By: James M Snell <[email protected]>
@evanlucas evanlucas mentioned this pull request Nov 13, 2017
@MylesBorins
Copy link
Contributor

Should this land on v8.x or v6.x?

It lands cleanly on 8.x, but will require a backport for v6.x

@addaleax
Copy link
Member

@MylesBorins it’s not worth the trouble of backporting I’d say (not that I’d want to stop @bnoordhuis from doing that), but if it lands cleanly it should probably go into v8.x

gibfahn pushed a commit that referenced this pull request Dec 13, 2017
`&vec[0]` is undefined behavior when `vec.size() == 0`.

It is mostly academic because package.json files are not usually empty
and because with most STL implementations it decays to something that
is legal C++ as long as the result is not dereferenced, but better safe
than sorry.

Note that the tests don't actually fail because of that, I added them
as sanity checks.

PR-URL: #16871
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Franziska Hinkelmann <[email protected]>
Reviewed-By: James M Snell <[email protected]>
This was referenced Dec 20, 2017
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++. fs Issues and PRs related to the fs subsystem / file system.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants