-
Notifications
You must be signed in to change notification settings - Fork 339
Conversation
Looks like windows arm CI is failing due to not finding |
Perhaps we should do this in 2 parts; first to update the calls to |
src/node_api_jsrt.cc
Outdated
|
||
size_t written = 0; | ||
size_t actualLength = 0; | ||
CHECK_JSRT(JsCopyString(key, *buffer, strLength, &written, &actualLength), napi_string_expected); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be CHECK_JSRT_EXPECTED
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it. Wondering why I didn't see these failures on my machine.
src/node_api_jsrt.cc
Outdated
inline napi_status JsCopyUtf8Bytes(JsValueRef key, | ||
char** buffer, size_t* copiedBytesCount) { | ||
int strLength = 0; | ||
CHECK_JSRT(JsGetStringLength(key, &strLength), napi_string_expected); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be CHECK_JSRT_EXPECTED
src/node_api_jsrt.cc
Outdated
*buffer = reinterpret_cast<char*>(malloc(actualLength + 1)); | ||
CHAKRA_VERIFY(buffer != nullptr); | ||
|
||
CHECK_JSRT(JsCopyString(key, *buffer, actualLength, &written, nullptr), napi_string_expected); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be CHECK_JSRT_EXPECTED
src/node_api_jsrt.cc
Outdated
CHECK_JSRT(JsGetStringLength(key, &strLength), napi_string_expected); | ||
|
||
// assume string contains ascii characters only | ||
*buffer = reinterpret_cast<char*>(malloc(strLength + 1)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Memory leak on errors. Can we just use std::vector
instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How is this different to the previous code which malloc
s a buffer in StringUtf8::From
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Exactly... Isn't it same pattern as before?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CHAKRA_VERIFY is a fast fail, CHECK_JSRT returns the error code. Memory leaks are less of an issue when the process is dying anyway.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, the function that calls JsCopyUtf8Bytes
is going to leak the allocation in the new scheme, since the old code used the value locally and never passed it back out, while the new code will just leak.
src/node_api_jsrt.cc
Outdated
// free previously allocated buffer | ||
free(*buffer); | ||
|
||
*buffer = reinterpret_cast<char*>(malloc(actualLength + 1)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here, possible memory leak unless you use RAII.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yep. I will re-work this a bit and update the PR. For now, I will send another PR that unblocks the build break because of signature change.
Updated the PR with RAII as @kfarnung suggested using |
src/node_api_jsrt.cc
Outdated
free(_str); | ||
|
||
_str = reinterpret_cast<char*>(malloc(actualLength + 1)); | ||
CHAKRA_VERIFY(_str != nullptr); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should not be using CHAKRA_VERIFY in N-API.
src/node_api_jsrt.cc
Outdated
CHAKRA_VERIFY(_str != nullptr); | ||
|
||
CHECK_JSRT_EXPECTED(JsCopyString(strRef, _str, actualLength, &written, nullptr), napi_string_expected); | ||
assert(actualLength == written); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we just have this return an error?
char* buffer = reinterpret_cast<char*>(malloc(len+1)); | ||
CHAKRA_VERIFY(buffer != nullptr); | ||
// assume string contains ascii characters only | ||
_str = reinterpret_cast<char*>(malloc(strLength + 1)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should probably check the malloc return value for null and bail out like the code previously did?
deps/chakrashim/src/jsrtutils.cc
Outdated
IfJsErrorRet(JsCopyString(strRef, buffer, len, &written, nullptr)); | ||
size_t actualLength = 0; | ||
IfJsErrorRet(JsCopyString(strRef, _str, strLength, &written, &actualLength)); | ||
CHAKRA_ASSERT(strLength == written); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't guaranteed to be true, if the buffer stops mid-multibyte character. E.g. if there is a 2 byte Utf8 character which would start at position strLength-1
, then JsCopyString
will omit all bytes for that codepoint from the buffer, I believe, so written
will be strLength-1
. Similarly if it ends at a 3 byte character, it can be strLength-2
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
makes sense. Moved it so this gets verified when actualLength == written
.
size_t written = 0; | ||
size_t actualLength = 0; | ||
CHECK_JSRT_EXPECTED( | ||
JsCopyString(strRef, _str, strLength, &written, &actualLength), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Can you indent continuation lines by four spaces here and below?
src/node_api_jsrt.cc
Outdated
|
||
int strLength = 0; | ||
CHECK_JSRT_EXPECTED(JsGetStringLength(strRef, &strLength), | ||
napi_string_expected); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Can you line this up with the previous parameter?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure.
Optimize calls to JsCopyString API PR-URL: nodejs#348 Reviewed-By: Kyle Farnung <[email protected]>
Optimize calls to JsCopyString API PR-URL: #348 Reviewed-By: Kyle Farnung <[email protected]>
Optimize calls to
JsCopyString
by assuming the string we get is ascii.Refs: chakra-core/ChakraCore#3433
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passesAffected core subsystem(s)
chakrashim