Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bnoordhuis/1537077 to your computer and use it in GitHub Desktop.
Save bnoordhuis/1537077 to your computer and use it in GitHub Desktop.
From 61dbe66f45a86d5519bf25e403caf34f60ad9dd8 Mon Sep 17 00:00:00 2001
From: Ryan Dahl <ry@tinyclouds.org>
Date: Thu, 29 Dec 2011 16:06:14 -0800
Subject: [PATCH] Potential fix for #2438
---
src/node_http_parser.cc | 33 +++++++++++++++++++++++++++++++++
1 files changed, 33 insertions(+), 0 deletions(-)
diff --git a/src/node_http_parser.cc b/src/node_http_parser.cc
index 38251a1..1c39a12 100644
--- a/src/node_http_parser.cc
+++ b/src/node_http_parser.cc
@@ -179,6 +179,19 @@ struct StringPtr {
}
+ // If str_ does not point to a heap string yet, this function makes it do
+ // so. This is called at the end of each http_parser_execute() so as not
+ // to leak references. See issue #2438 and test-http-parser-bad-ref.js.
+ void Save() {
+ if (!on_heap_) {
+ char* s = new char[size_];
+ memcpy(s, str_, size_);
+ str_ = s;
+ on_heap_ = true;
+ }
+ }
+
+
void Reset() {
if (on_heap_) {
delete[] str_;
@@ -287,6 +300,8 @@ public:
HTTP_CB(on_headers_complete) {
Local<Value> cb = handle_->Get(on_headers_complete_sym);
+ headers_complete_ = true;
+
if (!cb->IsFunction())
return 0;
@@ -401,6 +416,19 @@ public:
}
+ void Save() {
+ url_.Save();
+
+ for (int i = 0; i < num_fields_; i++) {
+ fields_[i].Save();
+ }
+
+ for (int i = 0; i < num_values_; i++) {
+ values_[i].Save();
+ }
+ }
+
+
// var bytesParsed = parser->execute(buffer, off, len);
static Handle<Value> Execute(const Arguments& args) {
HandleScope scope;
@@ -447,6 +475,9 @@ public:
size_t nparsed =
http_parser_execute(&parser->parser_, &settings, buffer_data + off, len);
+ if (!parser->headers_complete_)
+ parser->Save();
+
// Unassign the 'buffer_' variable
assert(current_buffer);
current_buffer = NULL;
@@ -557,6 +588,7 @@ private:
num_values_ = -1;
have_flushed_ = false;
got_exception_ = false;
+ headers_complete_ = false;
}
@@ -568,6 +600,7 @@ private:
int num_values_;
bool have_flushed_;
bool got_exception_;
+ bool headers_complete_;
};
--
1.7.6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment