summaryrefslogtreecommitdiff
path: root/src/util.cc
Commit message (Collapse)AuthorAgeFilesLines
* src: notify V8 for low memory when alloc failsAnna Henningsen2017-11-141-0/+9
| | | | | | | | | | | | | Call `v8::Isolate::GetCurrent()->LowMemoryNotification()` when an allocation fails to give V8 a chance to clean up and return memory before retrying (and possibly giving up). Backport-PR-URL: https://github.com/nodejs/node/pull/16587 PR-URL: https://github.com/nodejs/node/pull/8482 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com> Reviewed-By: Ilkka Myller <ilkka.myller@nodefield.com>
* src: provide allocation + nullptr check shortcutsAnna Henningsen2017-11-141-0/+1
| | | | | | | | | | | | | Provide shortcut `node::CheckedMalloc()` and friends that replace `node::Malloc()` + `CHECK_NE(·, nullptr);` combinations in a few places. Backport-PR-URL: https://github.com/nodejs/node/pull/16587 PR-URL: https://github.com/nodejs/node/pull/8482 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com> Reviewed-By: Ilkka Myller <ilkka.myller@nodefield.com>
* src: unify implementations of Utf8Value etc.Anna Henningsen2016-05-031-41/+34
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Unify the common code of `Utf8Value`, `TwoByteValue`, `BufferValue` and `StringBytes::InlineDecoder` into one class. Always make the result zero-terminated for the first three. This fixes two problems in passing: * When the conversion of the input value to String fails, make the buffer zero-terminated anyway. Previously, this would have resulted in possibly reading uninitialized data in multiple places in the code. An instance of that problem can be reproduced by running e.g. `valgrind node -e 'net.isIP({ toString() { throw Error() } })'`. * Previously, `BufferValue` copied one byte too much from the source, possibly resulting in an out-of-bounds memory access. This can be reproduced by running e.g. `valgrind node -e \ 'fs.openSync(Buffer.from("node".repeat(8192)), "r")'`. Further minor changes: * This lifts the `out()` method of `StringBytes::InlineDecoder` to the common class so that it can be used when using the overloaded `operator*` does not seem appropiate. * Hopefully clearer variable names. * Add checks to make sure the length of the data does not exceed the allocated storage size, including the possible null terminator. PR-URL: https://github.com/nodejs/node/pull/6357 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Trevor Norris <trev.norris@gmail.com>
* src: fix check-imports.py linter errorsSakthipriyan Vairamani2016-04-261-1/+1
| | | | | | | | | This patch fixes all the linter errors. PR-URL: https://github.com/nodejs/node/pull/6105 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Fedor Indutny <fedor.indutny@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
* src: fix out-of-bounds write in TwoByteValueAnna Henningsen2016-04-261-1/+3
| | | | | | | | | | | | | Plan 2 bytes instead of 1 byte for the final zero terminator for UTF-16. This is unlikely to cause real-world problems, but that ultimately depends on the `malloc` implementation. The issue can be uncovered by running e.g. `valgrind node -e "Buffer(65536).fill('a'.repeat(4096), 'utf16le')"` Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> PR-URL: https://github.com/nodejs/node/pull/6330
* fs: Buffer and encoding enhancements to fs APIJames M Snell2016-03-251-18/+51
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This makes several changes: 1. Allow path/filename to be passed in as a Buffer on fs methods 2. Add `options.encoding` to fs.readdir, fs.readdirSync, fs.readlink, fs.readlinkSync and fs.watch. 3. Documentation updates For 1... it's now possible to do: ```js fs.open(Buffer('/fs/foo/bar'), 'w+', (err, fd) => { }); ``` For 2... ```js fs.readdir('/fs/foo/bar', {encoding:'hex'}, (err,list) => { }); fs.readdir('/fs/foo/bar', {encoding:'buffer'}, (err, list) => { }); ``` encoding can also be passed as a string ```js fs.readdir('/fs/foo/bar', 'hex', (err,list) => { }); ``` The default encoding is set to UTF8 so this addresses the discrepency that existed previously between fs.readdir and fs.watch handling filenames differently. Fixes: https://github.com/nodejs/node/issues/2088 Refs: https://github.com/nodejs/node/issues/3519 PR-URL: https://github.com/nodejs/node/pull/5616 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Trevor Norris <trev.norris@gmail.com>
* buffer: add encoding parameter to fill()Trevor Norris2016-02-101-0/+23
| | | | | | | | | | | | | | | | | | | | | | | | | Can now call fill() using following parameters if value is a String: fill(string[, start[, end]][, encoding]) And with the following if value is a Buffer: fill(buffer[, start[, end]]) The encoding is ignored if value is not a String. All other non-Buffer values are coerced to a uint32. A multibyte strings will simply be copied into the Buffer until the number of bytes run out. Meaning partial strings can be left behind: Buffer(3).fill('\u0222'); // returns: <Buffer c8 a2 c8> In some encoding cases, such as 'hex', fill() will throw if the input string is not valid. PR-URL: https://github.com/nodejs/node/pull/4935 Reviewed-By: James M Snell <jasnell@gmail.com>
* src: replace usage of v8::Handle with v8::LocalMichaël Zasso2015-09-061-1/+1
| | | | | | | v8::Handle is deprecated: https://codereview.chromium.org/1224623004 PR-URL: https://github.com/nodejs/io.js/pull/2202 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
* src: fix deprecation warningsBen Noordhuis2015-04-301-1/+1
| | | | | | | | | The previous commit enables deprecation warnings, this commit fixes the handful of offending sites where the isolate was not explicitly being passed around. PR-URL: https://github.com/iojs/io.js/pull/1565 Reviewed-By: Trevor Norris <trev.norris@gmail.com>
* src: rename confusingly named local variableBen Noordhuis2015-03-051-4/+4
| | | | | | | | | Rename `val_` to `string`. The underscore suffix is normally reserved for data members, not locals, and it's not a great name in the first place. PR-URL: https://github.com/iojs/io.js/pull/1042 Reviewed-By: Trevor Norris <trev.norris@gmail.com>
* src: simplify node::Utf8Value()Ben Noordhuis2015-03-051-13/+8
| | | | | | | | | | | | * Remove kStorageSize constant. * Remove superfluous local variable and reinterpret_cast. * Reorder data members so the length field and data pointer (if not the data itself) fit in a single cache line. PR-URL: https://github.com/iojs/io.js/pull/1042 Reviewed-By: Trevor Norris <trev.norris@gmail.com>
* src: remove NODE_INVALID_UTF8 environment variableBen Noordhuis2015-03-051-6/+3
| | | | | | | | | Introduced in joyent/node v0.10 as a backwards compatibility measure. It's an ugly hack and allowing invalid UTF-8 is not a good idea in the first place, remove it. PR-URL: https://github.com/iojs/io.js/pull/1042 Reviewed-By: Trevor Norris <trev.norris@gmail.com>
* util: use on-stack buffer for Utf8ValueFedor Indutny2015-01-301-2/+7
| | | | | | | Improves `crypto.createHash().update().digest()` performance by 10%. PR-URL: https://github.com/iojs/io.js/pull/670 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
* Remove excessive copyright/license boilerplateisaacs2015-01-121-21/+0
| | | | | | | The copyright and license notice is already in the LICENSE file. There is no justifiable reason to also require that it be included in every file, since the individual files are not individually distributed except as part of the entire package.
* src: pass Isolate to node::Utf8Value constructorTrevor Norris2015-01-071-2/+2
| | | | | | | | Initial attempt to remove all uses of Isolate::GetCurrent(). Still exists a few locations, but this works out a heavy usage. PR-URL: https://github.com/iojs/io.js/pull/244 Reviewed-by: Ben Noordhuis <info@bnoordhuis.nl>
* src: pass Isolate to additional locationsTrevor Norris2015-01-071-1/+1
| | | | | | | Due to a recent V8 upgrade, more methods require Isolate as an argument. PR-URL: https://github.com/iojs/io.js/pull/244 Reviewed-by: Ben Noordhuis <info@bnoordhuis.nl>
* src: replace NULL with nullptrBen Noordhuis2014-10-231-1/+1
| | | | | | | | | | Now that we are building with C++11 features enabled, replace use of NULL with nullptr. The benefit of using nullptr is that it can never be confused for an integral type because it does not support implicit conversions to integral types except boolean - unlike NULL, which is defined as a literal `0`.
* src, test: fixup after v8 updateFedor Indutny2014-10-081-0/+2
| | | | | | | | | | | | | Because of behavior change of some V8 APIs (they mostly became more strict), following modules needed to be fixed: * crypto: duplicate prototype methods are not allowed anymore * contextify: some TryCatch trickery, the binding was using it incorrectly * util: maximum call stack error is now crashing in a different place Reviewed-By: Trevor Norris <trevnorris@gmail.com> PR-URL: https://github.com/joyent/node/pull/8476
* Merge remote-tracking branch 'upstream/v0.10'Timothy J Fontaine2014-06-101-0/+50
Conflicts: AUTHORS ChangeLog deps/v8/src/api.cc deps/v8/src/unicode-inl.h deps/v8/src/unicode.h lib/_stream_readable.js lib/http.js src/cares_wrap.cc src/node.cc src/node_crypto.cc src/node_dtrace.cc src/node_file.cc src/node_stat_watcher.cc src/node_version.h src/process_wrap.cc src/string_bytes.cc src/string_bytes.h src/udp_wrap.cc src/util.h test/simple/test-buffer.js test/simple/test-stream2-compatibility.js