Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix memory leak and other improvements #925

Merged
merged 4 commits into from
Mar 8, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ SOURCES = \
constants.cpp \
context.cpp \
contextualize.cpp \
copy_c_str.cpp \
cssize.cpp \
error_handling.cpp \
eval.cpp \
Expand Down
1 change: 0 additions & 1 deletion Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ libsass_la_SOURCES = \
constants.cpp constants.hpp \
context.cpp context.hpp \
contextualize.cpp contextualize.hpp \
copy_c_str.cpp copy_c_str.hpp \
error_handling.cpp error_handling.hpp \
eval.cpp eval.hpp \
expand.cpp expand.hpp \
Expand Down
10 changes: 6 additions & 4 deletions context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#endif

#include "ast.hpp"
#include "util.hpp"
#include "sass.h"
#include "context.hpp"
#include "plugins.hpp"
Expand All @@ -19,7 +20,6 @@
#include "cssize.hpp"
#include "extend.hpp"
#include "remove_placeholders.hpp"
#include "copy_c_str.hpp"
#include "color_names.hpp"
#include "functions.hpp"
#include "backtrace.hpp"
Expand All @@ -37,6 +37,7 @@
namespace Sass {
using namespace Constants;
using namespace File;
using namespace Sass;
using std::cerr;
using std::endl;

Expand Down Expand Up @@ -282,7 +283,7 @@ namespace Sass {
if (source_map_file != "" && !omit_source_map_url) {
output += linefeed + format_source_mapping_url(source_map_file);
}
return copy_c_str(output.c_str());
return sass_strdup(output.c_str());
}

Block* Context::parse_file()
Expand Down Expand Up @@ -333,9 +334,10 @@ namespace Sass {
if(is_indented_syntax_src) {
char * contents = sass2scss(source_c_str, SASS2SCSS_PRETTIFY_1 | SASS2SCSS_KEEP_COMMENT);
add_source(input_path, input_path, contents);
delete [] source_c_str;
return parse_file();
}
add_source(input_path, input_path, copy_c_str(source_c_str));
add_source(input_path, input_path, source_c_str);
return parse_file();
}

Expand Down Expand Up @@ -371,7 +373,7 @@ namespace Sass {
if (source_map_file == "") return 0;
char* result = 0;
string map = emitter.generate_source_map(*this);
result = copy_c_str(map.c_str());
result = sass_strdup(map.c_str());
return result;
}

Expand Down
13 changes: 0 additions & 13 deletions copy_c_str.cpp

This file was deleted.

10 changes: 0 additions & 10 deletions copy_c_str.hpp

This file was deleted.

6 changes: 3 additions & 3 deletions cssize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,14 @@ namespace Sass {
// rr->tabs(r->block()->tabs());
p_stack.pop_back();

Block* props = new Block(rr->block()->pstate());
Block* props = new (ctx.mem) Block(rr->block()->pstate());
for (size_t i = 0, L = rr->block()->length(); i < L; i++)
{
Statement* s = (*rr->block())[i];
if (!bubblable(s)) *props << s;
}

Block* rules = new Block(rr->block()->pstate());
Block* rules = new (ctx.mem) Block(rr->block()->pstate());
for (size_t i = 0, L = rr->block()->length(); i < L; i++)
{
Statement* s = (*rr->block())[i];
Expand All @@ -117,7 +117,7 @@ namespace Sass {

if (props->length())
{
Block* bb = new Block(rr->block()->pstate());
Block* bb = new (ctx.mem) Block(rr->block()->pstate());
*bb += props;
rr->block(bb);

Expand Down
12 changes: 6 additions & 6 deletions parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1279,7 +1279,7 @@ namespace Sass {
{
const char* i = chunk.begin;
// see if there any interpolants
const char* p = find_first_in_interval< sequence< negate< exactly<'\\'> >, exactly<hash_lbrace> > >(i, chunk.end);
const char* p = find_first_in_interval< exactly<hash_lbrace> >(i, chunk.end);
if (!p) {
String_Quoted* str_quoted = new (ctx.mem) String_Quoted(pstate, string(i, chunk.end));
if (!constant && str_quoted->quote_mark()) str_quoted->quote_mark('*');
Expand All @@ -1289,7 +1289,7 @@ namespace Sass {

String_Schema* schema = new (ctx.mem) String_Schema(pstate);
while (i < chunk.end) {
p = find_first_in_interval< sequence< negate< exactly<'\\'> >, exactly<hash_lbrace> > >(i, chunk.end);
p = find_first_in_interval< exactly<hash_lbrace> >(i, chunk.end);
if (p) {
if (i < p) {
// accumulate the preceding segment if it's nonempty
Expand Down Expand Up @@ -1344,7 +1344,7 @@ namespace Sass {
Token str(lexed);
const char* i = str.begin;
// see if there any interpolants
const char* p = find_first_in_interval< sequence< negate< exactly<'\\'> >, exactly<hash_lbrace> > >(str.begin, str.end);
const char* p = find_first_in_interval< exactly<hash_lbrace> >(str.begin, str.end);
if (!p) {
String_Constant* str_node = new (ctx.mem) String_Constant(pstate, normalize_wspace(string(str.begin, str.end)));
str_node->is_delayed(true);
Expand All @@ -1353,7 +1353,7 @@ namespace Sass {

String_Schema* schema = new (ctx.mem) String_Schema(pstate);
while (i < str.end) {
p = find_first_in_interval< sequence< negate< exactly<'\\'> >, exactly<hash_lbrace> > >(i, str.end);
p = find_first_in_interval< exactly<hash_lbrace> >(i, str.end);
if (p) {
if (i < p) {
(*schema) << new (ctx.mem) String_Constant(pstate, string(i, p)); // accumulate the preceding segment if it's nonempty
Expand Down Expand Up @@ -1480,14 +1480,14 @@ namespace Sass {
Token id(lexed);
const char* i = id.begin;
// see if there any interpolants
const char* p = find_first_in_interval< sequence< negate< exactly<'\\'> >, exactly<hash_lbrace> > >(id.begin, id.end);
const char* p = find_first_in_interval< exactly<hash_lbrace> >(id.begin, id.end);
if (!p) {
return new (ctx.mem) String_Quoted(pstate, string(id.begin, id.end));
}

String_Schema* schema = new (ctx.mem) String_Schema(pstate);
while (i < id.end) {
p = find_first_in_interval< sequence< negate< exactly<'\\'> >, exactly<hash_lbrace> > >(i, id.end);
p = find_first_in_interval< exactly<hash_lbrace> >(i, id.end);
if (p) {
if (i < p) {
// accumulate the preceding segment if it's nonempty
Expand Down
4 changes: 2 additions & 2 deletions prelexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ namespace Sass {
zero_plus <
alternatives <
// skip all escaped chars first
sequence < exactly < '\\' >, any_char >,
backslash_something,
// skip interpolants
interpolant,
// skip non delimiters
Expand All @@ -188,7 +188,7 @@ namespace Sass {
zero_plus <
alternatives <
// skip all escaped chars first
sequence < exactly < '\\' >, any_char >,
backslash_something,
// skip interpolants
interpolant,
// skip non delimiters
Expand Down
19 changes: 16 additions & 3 deletions prelexer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -567,27 +567,40 @@ namespace Sass {
}
template<prelexer mx>
const char* find_first_in_interval(const char* beg, const char* end) {
bool esc = false;
while ((beg < end) && *beg) {
if (mx(beg)) return beg;
if (esc) esc = false;
else if (*beg == '\\') esc = true;
else if (mx(beg)) return beg;
++beg;
}
return 0;
}
template <char c>
unsigned int count_interval(const char* beg, const char* end) {
unsigned int counter = 0;
bool esc = false;
while (beg < end && *beg) {
if (*beg == c) ++counter;
if (esc) esc = false;
else if (*beg == '\\') esc = true;
else if (*beg == c) ++counter;
++beg;
}
return counter;
}
template <prelexer mx>
unsigned int count_interval(const char* beg, const char* end) {
unsigned int counter = 0;
bool esc = false;
while (beg < end && *beg) {
const char* p;
if ((p = mx(beg))) {
if (esc) {
esc = false;
++beg;
} else if (*beg == '\\') {
esc = true;
++beg;
} else if ((p = mx(beg))) {
++counter;
beg = p;
}
Expand Down
26 changes: 13 additions & 13 deletions sass_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <cstring>
#include <stdexcept>
#include "json.hpp"
#include "copy_c_str.hpp"
#include "util.hpp"
#include "context.hpp"
#include "sass_values.h"
#include "sass_context.h"
Expand Down Expand Up @@ -94,7 +94,7 @@ extern "C" {
// Used to create sourceMappingUrl
char* source_map_file;

// option for sourceRoot property
// Custom functions that can be called from sccs code
char* source_map_root;

// Custom functions that can be called from sccs code
Expand Down Expand Up @@ -176,7 +176,7 @@ extern "C" {
#define IMPLEMENT_SASS_OPTION_STRING_ACCESSOR(type, option) \
type ADDCALL sass_option_get_##option (struct Sass_Options* options) { return options->option; } \
void ADDCALL sass_option_set_##option (struct Sass_Options* options, type option) \
{ free(options->option); options->option = option ? copy_c_str(option) : 0; }
{ free(options->option); options->option = option ? sass_strdup(option) : 0; }

#define IMPLEMENT_SASS_CONTEXT_GETTER(type, option) \
type ADDCALL sass_context_get_##option (struct Sass_Context* ctx) { return ctx->option; }
Expand Down Expand Up @@ -232,10 +232,10 @@ extern "C" {
json_append_member(json_err, "message", json_mkstring(e.message.c_str()));
msg_stream << e.pstate.path << ":" << e.pstate.line+1 << ": " << e.message << endl;
c_ctx->error_json = json_stringify(json_err, " ");;
c_ctx->error_message = copy_c_str(msg_stream.str().c_str());
c_ctx->error_message = sass_strdup(msg_stream.str().c_str());
c_ctx->error_text = strdup(e.message.c_str());
c_ctx->error_status = 1;
c_ctx->error_file = copy_c_str(e.pstate.path.c_str());
c_ctx->error_file = sass_strdup(e.pstate.path.c_str());
c_ctx->error_line = e.pstate.line+1;
c_ctx->error_column = e.pstate.column+1;
c_ctx->output_string = 0;
Expand All @@ -249,7 +249,7 @@ extern "C" {
json_append_member(json_err, "status", json_mknumber(2));
json_append_member(json_err, "message", json_mkstring(ba.what()));
c_ctx->error_json = json_stringify(json_err, " ");;
c_ctx->error_message = copy_c_str(msg_stream.str().c_str());
c_ctx->error_message = sass_strdup(msg_stream.str().c_str());
c_ctx->error_text = strdup(ba.what());
c_ctx->error_status = 2;
c_ctx->output_string = 0;
Expand All @@ -263,7 +263,7 @@ extern "C" {
json_append_member(json_err, "status", json_mknumber(3));
json_append_member(json_err, "message", json_mkstring(e.what()));
c_ctx->error_json = json_stringify(json_err, " ");;
c_ctx->error_message = copy_c_str(msg_stream.str().c_str());
c_ctx->error_message = sass_strdup(msg_stream.str().c_str());
c_ctx->error_text = strdup(e.what());
c_ctx->error_status = 3;
c_ctx->output_string = 0;
Expand All @@ -277,7 +277,7 @@ extern "C" {
json_append_member(json_err, "status", json_mknumber(4));
json_append_member(json_err, "message", json_mkstring(e.c_str()));
c_ctx->error_json = json_stringify(json_err, " ");;
c_ctx->error_message = copy_c_str(msg_stream.str().c_str());
c_ctx->error_message = sass_strdup(msg_stream.str().c_str());
c_ctx->error_text = strdup(e.c_str());
c_ctx->error_status = 4;
c_ctx->output_string = 0;
Expand All @@ -291,7 +291,7 @@ extern "C" {
json_append_member(json_err, "status", json_mknumber(5));
json_append_member(json_err, "message", json_mkstring("unknown"));
c_ctx->error_json = json_stringify(json_err, " ");;
c_ctx->error_message = copy_c_str(msg_stream.str().c_str());
c_ctx->error_message = sass_strdup(msg_stream.str().c_str());
c_ctx->error_text = strdup("unknown");
c_ctx->error_status = 5;
c_ctx->output_string = 0;
Expand Down Expand Up @@ -428,7 +428,7 @@ extern "C" {
}

// copy the included files on to the context (dont forget to free)
copy_strings(cpp_ctx->get_included_files(skip), &c_ctx->included_files, skip);
if (root) copy_strings(cpp_ctx->get_included_files(skip), &c_ctx->included_files, skip);

// return parsed block
return root;
Expand Down Expand Up @@ -658,7 +658,7 @@ extern "C" {
static void sass_clear_context (struct Sass_Context* ctx)
{
if (ctx == 0) return;
// release the allocated memory (mostly via copy_c_str)
// release the allocated memory (mostly via sass_strdup)
if (ctx->output_string) free(ctx->output_string);
if (ctx->source_map_string) free(ctx->source_map_string);
if (ctx->error_message) free(ctx->error_message);
Expand Down Expand Up @@ -757,7 +757,7 @@ extern "C" {

struct string_list* include_path = (struct string_list*) calloc(1, sizeof(struct string_list));
if (include_path == 0) return;
include_path->string = path ? copy_c_str(path) : 0;
include_path->string = path ? sass_strdup(path) : 0;
struct string_list* last = options->include_paths;
if (!options->include_paths) {
options->include_paths = include_path;
Expand All @@ -775,7 +775,7 @@ extern "C" {

struct string_list* plugin_path = (struct string_list*) calloc(1, sizeof(struct string_list));
if (plugin_path == 0) return;
plugin_path->string = path ? copy_c_str(path) : 0;
plugin_path->string = path ? sass_strdup(path) : 0;
struct string_list* last = options->plugin_paths;
if (!options->plugin_paths) {
options->plugin_paths = plugin_path;
Expand Down
7 changes: 4 additions & 3 deletions sass_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
#endif

#include <cstring>
#include "copy_c_str.hpp"
#include "util.hpp"
#include "context.hpp"
#include "sass_functions.h"

extern "C" {
using namespace std;
using namespace Sass;

// Struct to hold custom function callback
struct Sass_C_Function_Descriptor {
Expand Down Expand Up @@ -90,8 +91,8 @@ extern "C" {
{
Sass_Import* v = (Sass_Import*) calloc(1, sizeof(Sass_Import));
if (v == 0) return 0;
v->path = path ? Sass::copy_c_str(path) : 0;
v->base = base ? Sass::copy_c_str(base) : 0;
v->path = path ? sass_strdup(path) : 0;
v->base = base ? sass_strdup(base) : 0;
v->source = source;
v->srcmap = srcmap;
v->error = 0;
Expand Down
Loading