Skip to content
Browse files

Use ScopedMem to free optionally-gzdecoded data

This is robust against exceptions, compared to explicit free.
  • Loading branch information...
1 parent 630e61a commit 0dc522d02240a83f76e086b1d8a193030fb6c19e @edwinsmith edwinsmith committed with joelpob
Showing with 10 additions and 4 deletions.
  1. +3 −3 src/runtime/base/server/http_request_handler.cpp
  2. +7 −1 src/util/alloc.h
View
6 src/runtime/base/server/http_request_handler.cpp
@@ -31,6 +31,7 @@
#include <runtime/base/server/http_protocol.h>
#include <runtime/base/time/datetime.h>
#include <runtime/eval/debugger/debugger.h>
+#include <util/alloc.h>
namespace HPHP {
///////////////////////////////////////////////////////////////////////////////
@@ -173,7 +174,7 @@ void HttpRequestHandler::handleRequest(Transport *transport) {
bool original = compressed;
// check against static content cache
if (StaticContentCache::TheCache.find(path, data, len, compressed)) {
- bool free_data = false;
+ Util::ScopedMem decompressed_data;
// (qigao) not calling stat at this point because the timestamp of
// local cache file is not valuable, maybe misleading. This way
// the Last-Modified header will not show in response.
@@ -183,14 +184,13 @@ void HttpRequestHandler::handleRequest(Transport *transport) {
if (data == NULL) {
throw FatalErrorException("cannot unzip compressed data");
}
+ decompressed_data = const_cast<char*>(data);
compressed = false;
- free_data = true;
}
sendStaticContent(transport, data, len, 0, compressed, path, ext);
StaticContentCache::TheFileCache->adviseOutMemory();
ServerStats::LogPage(path, 200);
GetAccessLog().log(transport, vhost);
- if (free_data) free(const_cast<char*>(data));
return;
}
}
View
8 src/util/alloc.h
@@ -114,8 +114,14 @@ class ScopedMem {
ScopedMem(const ScopedMem&); // disable copying
ScopedMem& operator=(const ScopedMem&);
public:
- ScopedMem(void* ptr) : m_ptr(ptr) {}
+ ScopedMem() : m_ptr(0) {}
+ explicit ScopedMem(void* ptr) : m_ptr(ptr) {}
~ScopedMem() { free(m_ptr); }
+ ScopedMem& operator=(void* ptr) {
+ ASSERT(!m_ptr);
+ m_ptr = ptr;
+ return *this;
+ }
private:
void* m_ptr;
};

0 comments on commit 0dc522d

Please sign in to comment.
Something went wrong with that request. Please try again.