src: add method to compute storage in WriteWrap

`WriteWrap` instances may contain extra storage space.
`self_size()` returns the size of the *entire* struct, member fields as
well as storage space, so it is not an accurate measure for the
storage space available.

Add a method `ExtraSize()` (like the existing `Extra()` for accessing
the storage memory) that yields the wanted value, and use it
in the HTTP2 impl to fix a crash.

PR-URL: https://github.com/nodejs/node/pull/16727
Refs: https://github.com/nodejs/node/pull/16669
Reviewed-By: Anatoli Papirovski <apapirovski@mac.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
pull/16727/merge
Anna Henningsen 2017-11-03 20:13:18 +01:00 committed by James M Snell
parent 5d0436fbea
commit a5f3b3a6da
3 changed files with 7 additions and 2 deletions

View File

@ -503,7 +503,7 @@ inline void Nghttp2Session::SendPendingData() {
while ((srcLength = nghttp2_session_mem_send(session_, &src)) > 0) {
if (req == nullptr) {
req = AllocateSend();
destRemaining = req->self_size();
destRemaining = req->ExtraSize();
dest = req->Extra();
}
DEBUG_HTTP2("Nghttp2Session %s: nghttp2 has %d bytes to send\n",
@ -525,7 +525,7 @@ inline void Nghttp2Session::SendPendingData() {
srcRemaining -= destRemaining;
srcOffset += destRemaining;
req = AllocateSend();
destRemaining = req->self_size();
destRemaining = req->ExtraSize();
dest = req->Extra();
}

View File

@ -160,6 +160,10 @@ char* WriteWrap::Extra(size_t offset) {
offset;
}
size_t WriteWrap::ExtraSize() const {
return storage_size_ - ROUND_UP(sizeof(*this), kAlignSize);
}
} // namespace node
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS

View File

@ -77,6 +77,7 @@ class WriteWrap: public ReqWrap<uv_write_t>,
size_t extra = 0);
inline void Dispose();
inline char* Extra(size_t offset = 0);
inline size_t ExtraSize() const;
inline StreamBase* wrap() const { return wrap_; }